Nothing
# Default statistics of lists of models in rows (CONVENTIONS.md, TAB-7)
strip <- function(x) trimws(x, whitespace = "[\\h\\v]")
# The statistic rows of each model: the labels after the model's parameters
statistic_rows <- function(tbl, statistics) {
labels <- strip(tbl$Variable)
model <- cumsum(tbl$Model != "")
split(labels[labels %in% statistics | grepl("^[*][F&]", labels)], model[labels %in% statistics | grepl("^[*][F&]", labels)])
}
test_that("linear models get R2, its change, and an F test of the change", {
m1 <- lm(mpg ~ wt, data = mtcars)
m2 <- lm(mpg ~ wt + hp, data = mtcars)
rows <- statistic_rows(apa_parameters(list(m1, m2)), c("*R*^2^", "Δ*R*^2^"))
expect_equal(rows[[1]], c("*R*^2^", "*F*(1, 30)"))
expect_equal(rows[[2]], c("*R*^2^", "Δ*R*^2^", "*F*(1, 29)"))
})
test_that("logistic regressions get a pseudo-R2, a likelihood ratio test, and AIC", {
g1 <- glm(am ~ wt, data = mtcars, family = binomial)
g2 <- glm(am ~ wt + hp, data = mtcars, family = binomial)
tbl <- apa_parameters(list(g1, g2))
rows <- statistic_rows(tbl, c("*R*^2^~Tjur~", "*AIC*"))
expect_equal(rows[[1]], c("*R*^2^~Tjur~", "*χ*^2^(1)", "*AIC*"))
expect_equal(rows[[2]], c("*R*^2^~Tjur~", "*χ*^2^(1)", "*AIC*"))
# Model 1's test is against the null model: its deviance change
chisq <- g1$null.deviance - g1$deviance
values <- strip(tbl[["*B*"]][strip(tbl$Variable) == "*χ*^2^(1)"])
expect_equal(sub("\\^.*$", "", values[1]), sprintf("%.2f", chisq))
})
test_that("Gaussian GLMs are tested like linear models", {
g1 <- glm(mpg ~ wt, data = mtcars)
g2 <- glm(mpg ~ wt + hp, data = mtcars)
labels <- strip(apa_parameters(list(g1, g2))$Variable)
expect_true(all(c("*F*(1, 30)", "*F*(1, 29)") %in% labels))
})
test_that("ordinal and multinomial models get likelihood ratio tests, model 1 included", {
skip_if_not_installed("MASS")
d <- mtcars
d$gear <- factor(d$gear)
p1 <- MASS::polr(gear ~ wt, data = d, Hess = TRUE)
p2 <- MASS::polr(gear ~ wt + hp, data = d, Hess = TRUE)
labels <- strip(apa_parameters(list(p1, p2))$Variable)
expect_equal(sum(labels == "*χ*^2^(1)"), 2)
skip_if_not_installed("nnet")
n1 <- nnet::multinom(gear ~ wt, data = d, trace = FALSE)
n2 <- nnet::multinom(gear ~ wt + hp, data = d, trace = FALSE)
labels <- strip(apa_parameters(list(n1, n2))$Variable)
expect_equal(sum(labels == "*χ*^2^(2)"), 2)
})
test_that("Cox models get Nagelkerke's R2, C, a likelihood ratio test, and AIC", {
skip_if_not_installed("survival")
skip_if_not_installed("carData")
d <- carData::Rossi
c1 <- survival::coxph(survival::Surv(week, arrest) ~ fin, data = d)
c2 <- survival::coxph(survival::Surv(week, arrest) ~ fin + age + prio, data = d)
rows <- statistic_rows(apa_parameters(list(c1, c2)), c("*R*^2^~Nagelkerke~", "*C*", "*AIC*"))
expect_equal(rows[[2]], c("*R*^2^~Nagelkerke~", "*C*", "*χ*^2^(2)", "*AIC*"))
# Model 1's test is the likelihood ratio test that coxph stores
expect_true("*χ*^2^(1)" %in% rows[[1]])
})
test_that("mixed models get both R2, a likelihood ratio test, and AIC", {
skip_if_not_installed("lme4")
d <- lme4::sleepstudy
m1 <- lme4::lmer(Reaction ~ Days + (1 | Subject), data = d)
m2 <- lme4::lmer(Reaction ~ Days + (Days | Subject), data = d)
tbl <- suppressMessages(apa_parameters(list(m1, m2), ci_method = "wald"))
rows <- statistic_rows(tbl, c("*R*^2^~marginal~", "*R*^2^~conditional~", "*AIC*"))
expect_equal(rows[[1]], c("*R*^2^~marginal~", "*R*^2^~conditional~", "*χ*^2^(1)", "*AIC*"))
expect_equal(rows[[2]], c("*R*^2^~marginal~", "*R*^2^~conditional~", "*χ*^2^(2)", "*AIC*"))
# Model 1 is tested against a model with only a constant and its random
# effects, by maximum likelihood
null <- lme4::lmer(Reaction ~ 1 + (1 | Subject), data = d, REML = FALSE)
chisq <- 2 * (as.numeric(stats::logLik(lme4::refitML(m1))) - as.numeric(stats::logLik(null)))
values <- strip(tbl[["*B*"]][strip(tbl$Variable) == "*χ*^2^(1)"])
expect_equal(sub("\\^.*$", "", values[1]), sprintf("%.2f", chisq))
expect_match(apa_note(tbl), "from a model with only a constant and the random effects", fixed = TRUE)
})
test_that("models without valid nested tests get no test row", {
skip_if_not_installed("MASS")
r1 <- MASS::rlm(mpg ~ wt, data = mtcars)
r2 <- MASS::rlm(mpg ~ wt + hp, data = mtcars)
labels <- strip(apa_parameters(list(r1, r2))$Variable)
expect_false(any(grepl("^[*](F|χ)", labels)))
expect_equal(sum(labels == "*s*~robust~"), 2)
skip_if_not_installed("mgcv")
g1 <- mgcv::gam(mpg ~ s(wt), data = mtcars)
g2 <- mgcv::gam(mpg ~ s(wt) + s(hp), data = mtcars)
labels <- strip(apa_parameters(list(g1, g2))$Variable)
expect_false(any(grepl("^[*](F|χ)", labels)))
expect_equal(sum(labels == "*AIC*"), 2)
})
test_that("lists of different kinds of models keep R2 and its change", {
m1 <- lm(mpg ~ wt, data = mtcars)
g1 <- glm(am ~ wt, data = mtcars, family = binomial)
expect_null(apa7:::list_row_defaults(list(m1, g1)))
})
test_that("the test row is starred and defined in the note", {
m1 <- lm(mpg ~ wt, data = mtcars)
m2 <- lm(mpg ~ wt + hp, data = mtcars)
tbl <- apa_parameters(list(m1, m2))
labels <- strip(tbl$Variable)
f_values <- tbl[["*B*"]][startsWith(labels, "*F*(")]
expect_true(all(grepl("^", f_values, fixed = TRUE)))
delta <- tbl[["*B*"]][labels == "Δ*R*^2^"]
expect_false(grepl("*", delta, fixed = TRUE))
expect_match(apa_note(tbl), "*F*\u00A0=\u00A0test of the change in fit from the previous model", fixed = TRUE)
# Stars only: no p rows
expect_false("*p*" %in% labels)
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.