Skip to content

Commit

Permalink
Don't allow factors with <2 levels in regressions (jasp-stats#307)
Browse files Browse the repository at this point in the history
* check factors in log reg

* also for other regressions
  • Loading branch information
JohnnyDoorn authored May 7, 2024
1 parent 9d0e651 commit 929a7be
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion R/generalizedlinearmodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@ GeneralizedLinearModelInternal <- function(jaspResults, dataset = NULL, options,
limits.min = 0,
limits.max = Inf,
exitAnalysisIfErrors = TRUE)


if (length(options$factors) != 0)
.hasErrors(dataset,
type = "factorLevels",
factorLevels.target = options$factors,
factorLevels.amount = '< 2',
exitAnalysisIfErrors = TRUE)

if (options[["family"]] == "bernoulli") {

if (length(levels(dataset[, options[["dependent"]]])) != 2)
Expand Down
7 changes: 6 additions & 1 deletion R/regressionlinear.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ RegressionLinearInternal <- function(jaspResults, dataset = NULL, options) {
.hasErrors(dataset, type = c("infinity", "limits", "observations"),
all.target = options$weights, limits.min = 0, observations.amount = "< 2",
exitAnalysisIfErrors = TRUE)

if (length(options$factors) != 0)
.hasErrors(dataset,
type = "factorLevels",
factorLevels.target = options$factors,
factorLevels.amount = '< 2',
exitAnalysisIfErrors = TRUE)
covwt <- function(...) return(stats::cov.wt(..., wt = dataset[[.v(options[["weights"]])]])$cov)
.hasErrors(dataset[, -which(colnames(dataset) %in% c(.v(options$weights)))], type = "varCovData", varCovData.corFun = covwt,
exitAnalysisIfErrors = TRUE)
Expand Down
6 changes: 6 additions & 0 deletions R/regressionlogistic.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ RegressionLogisticInternal <- function(jaspResults, dataset = NULL, options, ...
all.target = options$covariates,
observations.amount = "< 2",
exitAnalysisIfErrors = TRUE)
if (length(options$factors) != 0)
.hasErrors(dataset,
type = "factorLevels",
factorLevels.target = options$factors,
factorLevels.amount = '< 2',
exitAnalysisIfErrors = TRUE)
}

# Performance Diagnostics Container
Expand Down

0 comments on commit 929a7be

Please sign in to comment.