Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions R/TimeWeightedCharts.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,21 @@ timeWeightedCharts <- function(jaspResults, dataset, options) {
columnsToPass <- c(measurements, stages)
columnsToPass <- columnsToPass[columnsToPass != ""]
phase2 <- (options[["cumulativeSumChartSdSource"]] == "historical")
movingRangeLength <- options[["cumulativeSumChartAverageMovingRangeLength"]]
if (!phase2 && .stageHasTooFewObservationsForMovingRange(dataset, stages, movingRangeLength)) {
plot$setError(.timeWeightedStagesMovingRangeErrorMessage(
chartTitle = gettext("Cumulative sum chart"),
dataset = dataset,
stages = stages,
movingRangeLength = movingRangeLength
))
return(list("plot" = plot))
Comment on lines +198 to +205
}

cusumChart <- .controlChart(dataset[columnsToPass], plotType = "cusum", stages = stages, xBarSdType = options[["cumulativeSumChartSdMethod"]],
nSigmasControlLimits = options[["cumulativeSumChartNumberSd"]], xAxisLabels = axisLabels,
cusumShiftSize = options[["cumulativeSumChartShiftSize"]], cusumTarget = options[["cumulativeSumChartTarget"]],
movingRangeLength = options[["cumulativeSumChartAverageMovingRangeLength"]], phase2 = phase2,
movingRangeLength = movingRangeLength, phase2 = phase2,
phase2Sd = options[["cumulativeSumChartSdValue"]], tableLabels = axisLabels, ruleList = ruleList)
table <- cusumChart$table
plot$plotObject <- cusumChart$plotObject
Expand All @@ -221,9 +232,20 @@ timeWeightedCharts <- function(jaspResults, dataset, options) {
columnsToPass <- c(measurements, stages)
columnsToPass <- columnsToPass[columnsToPass != ""]
phase2 <- (options[["exponentiallyWeightedMovingAverageChartSdSource"]] == "historical")
movingRangeLength <- options[["exponentiallyWeightedMovingAverageChartMovingRangeLength"]]
if (!phase2 && .stageHasTooFewObservationsForMovingRange(dataset, stages, movingRangeLength)) {
plot$setError(.timeWeightedStagesMovingRangeErrorMessage(
chartTitle = gettext("Exponentially weighted moving average chart"),
dataset = dataset,
stages = stages,
movingRangeLength = movingRangeLength
))
return(list("plot" = plot))
}

ewmaChart <- .controlChart(dataset[columnsToPass], plotType = "ewma", stages = stages, xBarSdType = options[["exponentiallyWeightedMovingAverageChartSdMethod"]],
nSigmasControlLimits = options[["exponentiallyWeightedMovingAverageChartSigmaControlLimits"]],
xAxisLabels = axisLabels, movingRangeLength = options[["exponentiallyWeightedMovingAverageChartMovingRangeLength"]],
xAxisLabels = axisLabels, movingRangeLength = movingRangeLength,
ewmaLambda = options[["exponentiallyWeightedMovingAverageChartLambda"]], phase2 = phase2,
phase2Sd = options[["exponentiallyWeightedMovingAverageChartSdValue"]], tableLabels = axisLabels,
ruleList = ruleList)
Expand All @@ -247,3 +269,20 @@ timeWeightedCharts <- function(jaspResults, dataset, options) {
return(ruleList)
}

.stageHasTooFewObservationsForMovingRange <- function(dataset, stages, movingRangeLength) {
if (identical(stages, "") || !stages %in% colnames(dataset))
return(FALSE)

stageCounts <- table(dataset[[stages]])
any(stageCounts < movingRangeLength)
}
Comment on lines +272 to +278

.timeWeightedStagesMovingRangeErrorMessage <- function(chartTitle, dataset, stages, movingRangeLength) {
stageCounts <- table(dataset[[stages]])
minObservations <- min(stageCounts)
gettextf(paste0(
"At least one stage contains only %1$s observation(s). %2$s requires at least %3$s observations per stage ",
"for the selected moving range length. This can happen when too many stages are defined (for example one point ",
"per stage). Reduce the number of stages or lower the moving range length."
), minObservations, chartTitle, movingRangeLength)
Comment on lines +283 to +287
}
Loading