Skip to content

Commit

Permalink
added check that formula is response ~ predictor; for post hoc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Konrad1991 committed Nov 11, 2024
1 parent e4e7c08 commit 3a5d3e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions bs/R/statisticalTests.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ testsServer <- function(id, data, listResults) {
})
if (inherits(e, "try-error")) {
err <- conditionMessage(attr(e, "condition"))
err <- paste0(err, "\n", "Could not use Formula")
output$test_error <- renderText(err)
}
if (is.null(err)) {
Expand All @@ -255,9 +256,10 @@ testsServer <- function(id, data, listResults) {
fit <- broom::tidy(aov(formula, data = df))
},
kruskal = {
fit <- broom::tidy(kruskal.test(formula, data = df))
fit <- broom::tidy(kruskal.test(formula, data = df)) # Keep here the restriction for respone ~ predictor
},
HSD = {
check_formula(formula)
aov_res <- aov(formula, data = df)
bal <- input$design
req(bal)
Expand All @@ -272,31 +274,37 @@ testsServer <- function(id, data, listResults) {
)$groups
},
kruskalTest = {
check_formula(formula)
fit <- with(df, kruskal(df[, dep], df[, indep]),
alpha = input$pval, p.adj = input$padj, group = TRUE
)$groups
},
LSD = {
check_formula(formula)
aov_res <- aov(formula, data = df)
fit <- agricolae::LSD.test(aov_res,
trt = indep,
alpha = input$pval, p.adj = input$padj, group = TRUE
)$groups
},
scheffe = {
check_formula(formula)
aov_res <- aov(formula, data = df)
fit <- agricolae::scheffe.test(aov_res, trt = indep, alpha = input$pval, group = TRUE)$groups
},
REGW = {
check_formula(formula)
aov_res <- aov(formula, data = df)
fit <- agricolae::REGW.test(aov_res, trt = indep, alpha = input$pval, group = TRUE)$groups
}
)
})
}, silent = TRUE)
if (inherits(e, "try-error")) {
err <- conditionMessage(attr(e, "condition"))
err <- paste0(err, "\n", "Test did not run successfully")
output$test_error <- renderText(err)
} else if (is.null(fit)) {
err <- paste0(err, "\n", "Test did not run successfully")
output$test_error <- renderText("Result is NULL")
} else {
fit <- cbind(fit, row.names(fit))
Expand Down
12 changes: 12 additions & 0 deletions bs/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,15 @@ check_type_res <- function(res) {
stop(paste0("Found result with unallowed type: ", class(res)))
}
}

# Check that formula is of type response ~ predictor
check_formula <- function(formula) {
if (!inherits(formula, "formula")) {
stop("Input must be a formula of the type response ~ predictor")
}
terms <- all.vars(formula)
if (length(terms) != 2) {
stop("Formula must have exactly two terms: response ~ predictor")
}
return(TRUE)
}

0 comments on commit 3a5d3e1

Please sign in to comment.