-
Notifications
You must be signed in to change notification settings - Fork 21
Process Capability: Adjust parameter estimation precision settings #462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f0b6821
d4af53e
5cbceb9
715e9c6
7924e37
c8e7d29
0cfceb0
eb6a7b2
57a3417
6f155bd
02d6b13
0044e53
3502495
6601079
842bf61
12faef8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,5 @@ | |
| ^.*\.Rproj$ | ||
| ^\.Rproj\.user$ | ||
| ^\examples$ | ||
| ^\.positai$ | ||
| ^\.claude$ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,3 +64,4 @@ renv/ | |
| _processedLockFile.lock | ||
|
|
||
| .vscode/ | ||
| .positai | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,6 @@ Imports: | |
| jaspDescriptives, | ||
| jaspGraphs, | ||
| lubridate, | ||
| mle.tools, | ||
| psych, | ||
| qcc, | ||
| rsm, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,7 +59,7 @@ processCapabilityStudies <- function(jaspResults, dataset, options) { | |
| if ((!wideFormat && options[["subgroupSizeType"]] == "manual" && | ||
| any(lapply(split(dataset[[stages]], ceiling(seq_along(dataset[[stages]])/options[["manualSubgroupSizeValue"]])), FUN = function(x)length(unique(x))) > 1)) || | ||
| (!wideFormat && options[["subgroupSizeType"]] == "groupingVariable" && | ||
| any(table(dplyr::count_(dataset, vars = c(stages, subgroupVariable))[subgroupVariable]) > 1))) { | ||
| any(table(dplyr::count(dataset, dplyr::across(dplyr::all_of(c(stages, subgroupVariable))))[[subgroupVariable]]) > 1))) { | ||
| plotNotes <- paste0(plotNotes, gettext("One or more subgroups are assigned to more than one stage, only first stage is considered.<br>")) | ||
| } | ||
| if (anyNA(dataset[[stages]])) { | ||
|
|
@@ -856,9 +856,9 @@ processCapabilityStudies <- function(jaspResults, dataset, options) { | |
| # Addition to consider that if distributions are set historically, they may fall outside the usual limits | ||
| if (distribution == "normal" && options[["historicalMean"]]) | ||
| xLimits <- range(xLimits, options[["historicalMeanValue"]] - 1.5 * sdo, options[["historicalMeanValue"]] + 1.5 * sdo) | ||
| if (distribution == "weibull" || distribution == "3ParameterWeibull" && options[["historicalScale"]]) | ||
| if ((distribution == "weibull" || distribution == "3ParameterWeibull") && options[["historicalScale"]]) | ||
| xLimits <- range(xLimits, options[["historicalScaleValue"]] - 1.5 * sdo, options[["historicalScaleValue"]] + 1.5 * sdo) | ||
| if (distribution == "lognormal" || distribution == "3ParameterLognormal" && options[["historicalLogMean"]]) | ||
| if ((distribution == "lognormal" || distribution == "3ParameterLognormal") && options[["historicalLogMean"]]) | ||
| xLimits <- range(xLimits, exp(options[["historicalLogMeanValue"]]) - 1.5 * sdo, | ||
| exp(options[["historicalLogMeanValue"]]) + 1.5 * sdo) | ||
| if (distribution == "exponential") | ||
|
|
@@ -2579,14 +2579,14 @@ processCapabilityStudies <- function(jaspResults, dataset, options) { | |
| } | ||
| table$addColumnInfo(name = "n", title = gettext("N"), type = "integer") | ||
| if (options[["nullDistribution"]] == "normal") { | ||
| table$addColumnInfo(name = "mean", title = gettextf("Mean (%1$s)", "\u03BC"), type = "number") | ||
| table$addColumnInfo(name = "sd", title = gettextf("Std. dev. (%1$s)", "\u03C3"), type = "number") | ||
| table$addColumnInfo(name = "parameterEstimate1", title = gettextf("Mean (%1$s)", "\u03BC"), type = "number") | ||
| table$addColumnInfo(name = "parameterEstimate2", title = gettextf("Std. dev. (%1$s)", "\u03C3"), type = "number") | ||
| } else if (options[["nullDistribution"]] == "lognormal") { | ||
| table$addColumnInfo(name = "mean", title = gettextf("Log mean (%1$s)", "\u03BC"), type = "number") | ||
| table$addColumnInfo(name = "sd", title = gettextf("Log std.dev (%1$s)", "\u03C3"), type = "number") | ||
| table$addColumnInfo(name = "parameterEstimate1", title = gettextf("Log mean (%1$s)", "\u03BC"), type = "number") | ||
| table$addColumnInfo(name = "parameterEstimate2", title = gettextf("Log std.dev (%1$s)", "\u03C3"), type = "number") | ||
| } else if (options[["nullDistribution"]] == "weibull") { | ||
| table$addColumnInfo(name = "mean", title = gettextf("Shape (%1$s)", "\u03B2"), type = "number") | ||
| table$addColumnInfo(name = "sd", title = gettextf("Scale (%1$s)", "\u03B8"), type = "number") | ||
| table$addColumnInfo(name = "parameterEstimate1", title = gettextf("Shape (%1$s)", "\u03B2"), type = "number") | ||
| table$addColumnInfo(name = "parameterEstimate2", title = gettextf("Scale (%1$s)", "\u03B8"), type = "number") | ||
| } | ||
| table$addColumnInfo(name = "ad", title = gettext("AD"), type = "integer") | ||
| table$addColumnInfo(name = "p", title = gettext("<i>p</i>-value"), type = "pvalue") | ||
|
|
@@ -2601,51 +2601,50 @@ processCapabilityStudies <- function(jaspResults, dataset, options) { | |
| container[["probabilityTable"]] <- table | ||
| return() | ||
| } | ||
| tableColNames <- c("mean", "sd", "n", "ad", "p") | ||
| tableColNames <- c("parameterEstimate1", "parameterEstimate2", "n", "ad", "p") | ||
| if (nStages > 1) | ||
| tableColNames <- c("stage", tableColNames) | ||
| tableDf <- data.frame(matrix(ncol = length(tableColNames), nrow = 0)) | ||
| colnames(tableDf) <- tableColNames | ||
| for (i in seq_len(nStages)) { | ||
| stage <- unique(dataset[[stages]])[i] | ||
| dataCurrentStage <- dataset[which(dataset[[stages]] == stage), ][!names(dataset) %in% stages] | ||
| values <- as.vector(na.omit(unlist(dataCurrentStage[measurements]))) # distribution fitting function complains if this is not explicitly a vector | ||
| stageValues <- as.vector(na.omit(unlist(dataCurrentStage[measurements]))) # distribution fitting function complains if this is not explicitly a vector | ||
|
|
||
| if (options[["nullDistribution"]] == "normal") { | ||
| meanx <- mean(values) | ||
| sdx <- sd(values) | ||
| test <- goftest::ad.test(x = values, "norm", mean = meanx, sd = sdx) | ||
| parameterEstimate1 <- mean(stageValues) | ||
| parameterEstimate2 <- stats::sd(stageValues) | ||
| andersonDarlingTest <- goftest::ad.test(x = stageValues, "norm", mean = parameterEstimate1, sd = parameterEstimate2) | ||
| } else if (options[["nullDistribution"]] == "lognormal") { | ||
| fit <- EnvStats::elnorm(values) | ||
| meanx <- fit$parameters[1] | ||
| sdx <- fit$parameters[2] | ||
| test <- goftest::ad.test(x = values, "plnorm", meanlog = meanx, sdlog = sdx) | ||
| fitPars <- .distributionParameters(stageValues, distribution = "lognormal") | ||
| parameterEstimate1 <- fitPars$beta | ||
| parameterEstimate2 <- fitPars$theta | ||
| andersonDarlingTest <- goftest::ad.test(x = stageValues, "plnorm", meanlog = parameterEstimate1, sdlog = parameterEstimate2) | ||
| } else if (options[["nullDistribution"]] == "weibull") { | ||
| fit <- fitdistrplus::fitdist(values, 'weibull', | ||
| control = list( | ||
| maxit = 10000, | ||
| abstol = .Machine$double.eps^0.75, | ||
| reltol = .Machine$double.eps^0.75, | ||
| ndeps = rep(1e-8, 2))) | ||
| meanx <- fit$estimate[1] | ||
| sdx <- fit$estimate[2] | ||
| test <- goftest::ad.test(x = values, "pweibull", shape = meanx, scale = sdx) | ||
| } | ||
| n <- length(values) | ||
| ad <- test$statistic | ||
| adStar <- ad*(1 + (0.75/n) + (2.25/(n^2))) | ||
| if(ad >= 0.6) { | ||
| p <- exp(1.2937 - (5.709 * adStar) + 0.0186 * (adStar^2)) | ||
| } else if(adStar < 0.6 && adStar > 0.34) { | ||
| p <- exp(0.9177 - (4.279 * adStar) - 1.38 * (adStar^2)) | ||
| } else if(adStar < 0.34 && adStar > 0.2) { | ||
| p <- 1 - exp(-8.318 + (42.796 * adStar) - 59.938 * (adStar^2)) | ||
| } else if(adStar <= 0.2) { | ||
| p <- 1 - exp(-13.436 + (101.14 * adStar) - 223.73 * (adStar^2)) #Jaentschi & Bolboaca (2018) | ||
| fitPars <- .distributionParameters(stageValues, distribution = "weibull") | ||
| parameterEstimate1 <- fitPars$beta | ||
| parameterEstimate2 <- fitPars$theta | ||
| andersonDarlingTest <- goftest::ad.test(x = stageValues, "pweibull", shape = parameterEstimate1, scale = parameterEstimate2) | ||
| } | ||
| sampleSize <- length(stageValues) | ||
| adStatistic <- andersonDarlingTest$statistic | ||
| adStatisticAdjusted <- adStatistic * (1 + (0.75 / sampleSize) + (2.25 / (sampleSize^2))) | ||
| if (adStatistic >= 0.6) { | ||
| pValue <- exp(1.2937 - (5.709 * adStatisticAdjusted) + 0.0186 * (adStatisticAdjusted^2)) | ||
| } else if (adStatisticAdjusted < 0.6 && adStatisticAdjusted > 0.34) { | ||
| pValue <- exp(0.9177 - (4.279 * adStatisticAdjusted) - 1.38 * (adStatisticAdjusted^2)) | ||
| } else if (adStatisticAdjusted < 0.34 && adStatisticAdjusted > 0.2) { | ||
|
Comment on lines
+2630
to
+2636
|
||
| pValue <- 1 - exp(-8.318 + (42.796 * adStatisticAdjusted) - 59.938 * (adStatisticAdjusted^2)) | ||
| } else if (adStatisticAdjusted <= 0.2) { | ||
| pValue <- 1 - exp(-13.436 + (101.14 * adStatisticAdjusted) - 223.73 * (adStatisticAdjusted^2)) #Jaentschi & Bolboaca (2018) | ||
| } else { | ||
| p <- test$p.value | ||
| pValue <- andersonDarlingTest$p.value | ||
| } | ||
| tableDfCurrentStage <- data.frame(mean = meanx, sd = sdx, n = n, ad = round(ad, .numDecimals), p = round(p, .numDecimals)) | ||
| tableDfCurrentStage <- data.frame(parameterEstimate1 = parameterEstimate1, | ||
| parameterEstimate2 = parameterEstimate2, | ||
| n = sampleSize, | ||
| ad = round(adStatistic, .numDecimals), | ||
| p = round(pValue, .numDecimals)) | ||
| if (i == 1 && nStages > 1) | ||
| baseLineDf <- tableDfCurrentStage | ||
| if (i > 1) { | ||
|
|
@@ -2703,6 +2702,37 @@ processCapabilityStudies <- function(jaspResults, dataset, options) { | |
| return(plot) | ||
| } | ||
|
|
||
| .qcObservedVarcov <- function(logdensity, X, parms, mle) { | ||
| mle <- unlist(mle) | ||
|
|
||
| if (!all(parms %in% names(mle))) { | ||
| if (length(mle) != length(parms)) | ||
| stop(gettext("Could not compute variance-covariance matrix: parameter vector has unexpected length."), call. = FALSE) | ||
|
|
||
| names(mle) <- parms | ||
| } | ||
|
|
||
| mle <- as.numeric(mle[parms]) | ||
| names(mle) <- parms | ||
|
|
||
| negLogLik <- function(theta) { | ||
| names(theta) <- parms | ||
| env <- list2env(c(as.list(theta), list(x = X)), parent = baseenv()) | ||
| ll <- eval(logdensity, envir = env) | ||
| -sum(ll) | ||
| } | ||
|
|
||
| hessian <- stats::optimHess(par = mle, fn = negLogLik) | ||
|
|
||
| if (anyNA(hessian) || any(!is.finite(hessian))) | ||
| stop(gettext("Could not compute variance-covariance matrix: Hessian contains invalid values."), call. = FALSE) | ||
|
|
||
| varcov <- solve(hessian) | ||
| dimnames(varcov) <- list(parms, parms) | ||
|
|
||
| list(varcov = varcov) | ||
| } | ||
|
|
||
| .qcProbabilityPlotObject <- function(options, dataset, measurements, stages) { | ||
| if (identical(stages, "")) { | ||
| nStages <- 1 | ||
|
|
@@ -2749,8 +2779,8 @@ processCapabilityStudies <- function(jaspResults, dataset, options) { | |
| # Computing according to the distribution | ||
| if (options[["nullDistribution"]] == "normal") { | ||
| lpdf <- quote(-log(sigma) - 0.5 / sigma ^ 2 * (x - mu) ^ 2) | ||
| matrix <- try(mle.tools::observed.varcov(logdensity = lpdf, X = dataCurrentStage, parms = c("mu", "sigma"), | ||
| mle = c(mean(dataCurrentStage), sd(dataCurrentStage)))) | ||
| matrix <- try(.qcObservedVarcov(logdensity = lpdf, X = dataCurrentStage, parms = c("mu", "sigma"), | ||
| mle = c(mean(dataCurrentStage), sd(dataCurrentStage)))) | ||
| # Gracefully handle confidence band computation failure | ||
| if (jaspBase::isTryError(matrix)) { | ||
| hasConfidenceBands <- FALSE | ||
|
|
@@ -2781,7 +2811,7 @@ processCapabilityStudies <- function(jaspResults, dataset, options) { | |
| meanlog <- as.numeric(fit$parameters[1]) | ||
| sdlog <- as.numeric(fit$parameters[2]) | ||
| lpdf <- quote(log(1/(sqrt(2*pi)*x*sdlog) * exp(-(log(x)- meanlog)^2/(2*sdlog^2)))) | ||
| matrix <- try(mle.tools::observed.varcov(logdensity = lpdf, X = dataCurrentStage, parms = c("meanlog", "sdlog"), mle = fit$parameters)) | ||
| matrix <- try(.qcObservedVarcov(logdensity = lpdf, X = dataCurrentStage, parms = c("meanlog", "sdlog"), mle = fit$parameters)) | ||
| # Gracefully handle confidence band computation failure | ||
| if (jaspBase::isTryError(matrix)) { | ||
| hasConfidenceBands <- FALSE | ||
|
|
@@ -2819,7 +2849,7 @@ processCapabilityStudies <- function(jaspResults, dataset, options) { | |
| shape <- as.numeric(fit_Weibull[["beta"]]) | ||
| scale <- as.numeric(fit_Weibull[["theta"]]) | ||
| lpdf <- quote(log(shape) - shape * log(scale) + shape * log(x) - (x/scale)^shape) | ||
| matrix <- try(mle.tools::observed.varcov(logdensity = lpdf, X = dataCurrentStage, parms = c("shape", "scale"), mle = c("shape" = shape, "scale" = scale))) | ||
| matrix <- try(.qcObservedVarcov(logdensity = lpdf, X = dataCurrentStage, parms = c("shape", "scale"), mle = c("shape" = shape, "scale" = scale))) | ||
| # Gracefully handle confidence band computation failure | ||
| if (jaspBase::isTryError(matrix)) { | ||
| hasConfidenceBands <- FALSE | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
.qcProbabilityTable(), the AD column is declared as typeinteger, but the code computes and stores non-integer Anderson–Darling statistics (rounded to.numDecimals). This will likely display incorrectly in the UI (e.g., 0.43 shown as 0). Change the column type tonumber(and optionally add a numeric format) to match the actual values.