Nothing
skip_if_not_installed("survival")
skip_if_not_installed("carData")
rossi <- carData::Rossi
cox1 <- survival::coxph(survival::Surv(week, arrest) ~ fin + age + prio, data = rossi)
cox2 <- survival::coxph(survival::Surv(week, arrest) ~ fin + age + prio + wexp + mar, data = rossi)
test_that("apa_parameters() shows hazard ratios for Cox models", {
tbl <- apa_parameters(cox1)
expect_s3_class(tbl, "apa7_tbl")
expect_equal(colnames(tbl), c("Variable", "*B*", "*SE*", "*HR*", "*z*", "*p*"))
expect_equal(nrow(tbl), 3)
hr <- exp(stats::coef(cox1))[["age"]]
expect_equal(trimws(tbl$`*HR*`[2]), sprintf("%.2f", hr))
expect_false("*HR*" %in% colnames(apa_parameters(cox1, exponentiate = FALSE)))
})
test_that("the note names the Cox model, the cases, and the events", {
note <- apa_note(apa_parameters(cox1))
expect_match(note, "Cox proportional hazards model (Cox, 1972)", fixed = TRUE)
expect_match(note, "*N*\u00A0=\u00A0432, with 114 events.", fixed = TRUE)
expect_match(note, "*HR*\u00A0=\u00A0hazard ratio", fixed = TRUE)
})
test_that("apa_performance() gives Nagelkerke's R2, the concordance, and AIC", {
perf <- apa_performance(cox1)
expect_equal(colnames(perf), c("*R*^2^~Nagelkerke~", "*C*", "*AIC*"))
expect_equal(
perf$`*C*`,
sub("^0", "", sprintf("%.2f", cox1$concordance[["concordance"]]))
)
})
test_that("nested Cox models are compared with likelihood ratio tests", {
comparison <- apa_performance_comparison(cox1, cox2)
expect_true(all(c("*χ*^2^", "*df*", "*p*") %in% colnames(comparison)))
lrt <- stats::anova(cox1, cox2)
expect_equal(trimws(comparison$`*χ*^2^`[2]), sprintf("%.2f", lrt$Chisq[2]))
expect_equal(trimws(comparison$`*df*`[2]), "2")
# Side by side in apa_parameters()
both <- apa_parameters(list(cox1, cox2))
expect_true("*HR*" %in% colnames(both))
})
test_that("variable names with spaces keep their names", {
d <- rossi
d$`Financial Aid` <- factor(d$fin, levels = c("no", "yes"), labels = c("No", "Yes"))
d$`Prior Convictions` <- d$prio
fit <- survival::coxph(survival::Surv(week, arrest) ~ `Financial Aid` + `Prior Convictions`, data = d)
expect_equal(apa_parameters(fit)$Variable, c("Financial Aid\u2013Yes", "Prior Convictions"))
})
# Parametric survival models ----
weibull <- survival::survreg(survival::Surv(week, arrest) ~ fin + age + prio, data = rossi)
strip <- function(x) trimws(x, whitespace = "[\\h\\v]")
test_that("apa_parameters() shows time ratios for parametric survival models", {
tbl <- apa_parameters(weibull)
expect_equal(colnames(tbl), c("Variable", "*B*", "*SE*", "*TR*", "*z*", "*p*"))
expect_equal(strip(tbl$Variable[5]), "Log(scale)")
expect_equal(strip(tbl$`*TR*`[3]), sprintf("%.2f", exp(stats::coef(weibull)[["age"]])))
# Neither the constant nor the scale is a time ratio
expect_equal(strip(tbl$`*TR*`[c(1, 5)]), c("", ""))
# The scale is tested only for Weibull models
expect_false(strip(tbl$`*z*`[5]) == "")
lognormal <- survival::survreg(survival::Surv(week, arrest) ~ fin + age, data = rossi, dist = "lognormal")
expect_equal(strip(apa_parameters(lognormal)$`*z*`[4]), "")
# The exponential distribution has no scale parameter
exponential <- survival::survreg(survival::Surv(week, arrest) ~ fin + age, data = rossi, dist = "exponential")
expect_false("Log(scale)" %in% strip(apa_parameters(exponential)$Variable))
})
test_that("the note names the distribution, the cases, and the events", {
note <- apa_note(apa_parameters(weibull))
expect_match(note, "accelerated failure time model (Wei, 1992) with a Weibull distribution.", fixed = TRUE)
expect_match(note, "*N*\u00A0=\u00A0432, with 114 events.", fixed = TRUE)
expect_match(note, "*TR*\u00A0=\u00A0time ratio", fixed = TRUE)
expect_match(note, "test of the exponential model", fixed = TRUE)
exponential <- survival::survreg(survival::Surv(week, arrest) ~ fin, data = rossi, dist = "exponential")
expect_match(apa_note(apa_parameters(exponential)), "with an exponential distribution.", fixed = TRUE)
})
test_that("parametric survival models get fit statistics and likelihood ratio tests", {
perf <- apa_performance(weibull)
expect_equal(colnames(perf), c("*R*^2^~Nagelkerke~", "*C*", "*AIC*"))
expect_equal(perf$`*C*`, sub("^0", "", sprintf("%.2f", survival::concordance(weibull)$concordance)))
smaller <- survival::survreg(survival::Surv(week, arrest) ~ fin, data = rossi)
comparison <- apa_performance_comparison(smaller, weibull)
lrt <- stats::anova(smaller, weibull)
expect_equal(strip(comparison$`*χ*^2^`[2]), sprintf("%.2f", lrt$Deviance[2]))
})
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.