Skip to content

Commit aec9b7b

Browse files
committed
Error message for time weighted charts
1 parent 6f155bd commit aec9b7b

1 file changed

Lines changed: 41 additions & 2 deletions

File tree

R/TimeWeightedCharts.R

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,21 @@ timeWeightedCharts <- function(jaspResults, dataset, options) {
194194
columnsToPass <- c(measurements, stages)
195195
columnsToPass <- columnsToPass[columnsToPass != ""]
196196
phase2 <- (options[["cumulativeSumChartSdSource"]] == "historical")
197+
movingRangeLength <- options[["cumulativeSumChartAverageMovingRangeLength"]]
198+
if (!phase2 && .stageHasTooFewObservationsForMovingRange(dataset, stages, movingRangeLength)) {
199+
plot$setError(.timeWeightedStagesMovingRangeErrorMessage(
200+
chartTitle = gettext("Cumulative sum chart"),
201+
dataset = dataset,
202+
stages = stages,
203+
movingRangeLength = movingRangeLength
204+
))
205+
return(list("plot" = plot))
206+
}
207+
197208
cusumChart <- .controlChart(dataset[columnsToPass], plotType = "cusum", stages = stages, xBarSdType = options[["cumulativeSumChartSdMethod"]],
198209
nSigmasControlLimits = options[["cumulativeSumChartNumberSd"]], xAxisLabels = axisLabels,
199210
cusumShiftSize = options[["cumulativeSumChartShiftSize"]], cusumTarget = options[["cumulativeSumChartTarget"]],
200-
movingRangeLength = options[["cumulativeSumChartAverageMovingRangeLength"]], phase2 = phase2,
211+
movingRangeLength = movingRangeLength, phase2 = phase2,
201212
phase2Sd = options[["cumulativeSumChartSdValue"]], tableLabels = axisLabels, ruleList = ruleList)
202213
table <- cusumChart$table
203214
plot$plotObject <- cusumChart$plotObject
@@ -221,9 +232,20 @@ timeWeightedCharts <- function(jaspResults, dataset, options) {
221232
columnsToPass <- c(measurements, stages)
222233
columnsToPass <- columnsToPass[columnsToPass != ""]
223234
phase2 <- (options[["exponentiallyWeightedMovingAverageChartSdSource"]] == "historical")
235+
movingRangeLength <- options[["exponentiallyWeightedMovingAverageChartMovingRangeLength"]]
236+
if (!phase2 && .stageHasTooFewObservationsForMovingRange(dataset, stages, movingRangeLength)) {
237+
plot$setError(.timeWeightedStagesMovingRangeErrorMessage(
238+
chartTitle = gettext("Exponentially weighted moving average chart"),
239+
dataset = dataset,
240+
stages = stages,
241+
movingRangeLength = movingRangeLength
242+
))
243+
return(list("plot" = plot))
244+
}
245+
224246
ewmaChart <- .controlChart(dataset[columnsToPass], plotType = "ewma", stages = stages, xBarSdType = options[["exponentiallyWeightedMovingAverageChartSdMethod"]],
225247
nSigmasControlLimits = options[["exponentiallyWeightedMovingAverageChartSigmaControlLimits"]],
226-
xAxisLabels = axisLabels, movingRangeLength = options[["exponentiallyWeightedMovingAverageChartMovingRangeLength"]],
248+
xAxisLabels = axisLabels, movingRangeLength = movingRangeLength,
227249
ewmaLambda = options[["exponentiallyWeightedMovingAverageChartLambda"]], phase2 = phase2,
228250
phase2Sd = options[["exponentiallyWeightedMovingAverageChartSdValue"]], tableLabels = axisLabels,
229251
ruleList = ruleList)
@@ -247,3 +269,20 @@ timeWeightedCharts <- function(jaspResults, dataset, options) {
247269
return(ruleList)
248270
}
249271

272+
.stageHasTooFewObservationsForMovingRange <- function(dataset, stages, movingRangeLength) {
273+
if (identical(stages, "") || !stages %in% colnames(dataset))
274+
return(FALSE)
275+
276+
stageCounts <- table(dataset[[stages]])
277+
any(stageCounts < movingRangeLength)
278+
}
279+
280+
.timeWeightedStagesMovingRangeErrorMessage <- function(chartTitle, dataset, stages, movingRangeLength) {
281+
stageCounts <- table(dataset[[stages]])
282+
minObservations <- min(stageCounts)
283+
gettextf(paste0(
284+
"At least one stage contains only %1$s observation(s). %2$s requires at least %3$s observations per stage ",
285+
"for the selected moving range length. This can happen when too many stages are defined (for example one point ",
286+
"per stage). Reduce the number of stages or lower the moving range length."
287+
), minObservations, chartTitle, movingRangeLength)
288+
}

0 commit comments

Comments
 (0)