Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Collate:
'omnibus.R'
'simulations.R'
'resi_asymptotic.R'
'resi_pe_gam.R'
Imports:
aod,
boot,
Expand All @@ -51,6 +52,7 @@ Imports:
splines
Suggests:
gee,
mgcv,
geepack,
robustbase,
reformulas,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ S3method(print,resi)
S3method(print,summary_resi)
S3method(resi,coxph)
S3method(resi,default)
S3method(resi,gam)
S3method(resi,gee)
S3method(resi,geeglm)
S3method(resi,glm)
Expand All @@ -30,6 +31,7 @@ S3method(resi,zeroinfl)
S3method(resi_pe,coxph)
S3method(resi_pe,default)
S3method(resi_pe,emmGrid)
S3method(resi_pe,gam)
S3method(resi_pe,gee)
S3method(resi_pe,geeglm)
S3method(resi_pe,glm)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# RESI (development version)

- Added `resi_pe()` for `mgcv::gam` and `mgcv::bam` fits. RESI for smooth terms uses mgcv's Wald statistic (`Vp`) with `Ref.df` (`f2S`, or `chisq2S` when the scale is known). Parametric terms use `t2S`/`z2S`. Terms tested by mgcv's random-effect test (`bs = "re"`, shrinkage bases, `select = TRUE`) return `NA` with a warning. `resi()` for these models gives an informative error; confidence intervals are not yet implemented.

# RESI 1.5.1.9001

- Fixed `resi_pe.lmerMod()` handling for spline terms like `ns(Days, 3)` by reconstructing the auxiliary fixed-effects model data from the original model call and replacing deprecated `lme4::nobars` usage with `reformulas::nobars`.
Expand Down
96 changes: 96 additions & 0 deletions R/resi_pe_gam.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#' @describeIn resi_pe RESI point estimation for generalized additive models
#' (\code{mgcv::gam} and \code{mgcv::bam} fits)
#' @details For \code{gam} and \code{bam} models, the RESI is computed from
#' the Wald statistics reported by \code{mgcv::summary.gam}. These use the
#' Bayesian posterior covariance \code{Vp}, so \code{vcovfunc} is not used.
#' Parametric coefficients use \code{\link{t2S}} (or \code{\link{z2S}} when
#' the scale parameter is known). Smooth terms use \code{\link{f2S}} with the
#' reference degrees of freedom \code{Ref.df} and the residual degrees of
#' freedom \code{n - sum(edf)} (or \code{\link{chisq2S}} when the scale is
#' known). The smooth-term RESI estimates the RESI of the population
#' smooth function (net of the other terms); when that function is well
#' represented by the basis, it does not depend on the basis size or the
#' penalty. Penalized terms without an unpenalized null space
#' (\code{bs = "re"}, shrinkage bases, or \code{select = TRUE}) are tested by
#' mgcv with a different statistic, so their RESI is \code{NA}. The overall
#' test is not computed for these models.
#' @export
resi_pe.gam <- function(model.full, data, anova = TRUE, coefficients = TRUE,
unbiased = TRUE, ...) {
if (!requireNamespace("mgcv", quietly = TRUE))
stop("\nPackage 'mgcv' is required for gam and bam models")
s <- mgcv::summary.gam(model.full)
n <- length(model.full$y)
rdf <- model.full$df.residual
output <- list(model.full = list(call = model.full$call,
formula = formula(model.full)),
model.reduced = NULL)
est <- c()

if (coefficients) {
pt <- as.data.frame(s$p.table)
pt$RESI <- if ("t value" %in% colnames(pt)) {
suppressWarnings(t2S(pt[, "t value"], rdf, n, unbiased))
} else {
suppressWarnings(z2S(pt[, "z value"], n, unbiased))
}
output$coefficients <- pt
est <- c(est, stats::setNames(pt$RESI, rownames(pt)))
}

if (anova) {
stat_name <- if (isTRUE(model.full$scale.estimated)) "F" else "Chisq"
pv_name <- if (stat_name == "F") "Pr(>F)" else "Pr(>Chisq)"
stat_col <- if (stat_name == "F") "F" else "Chi.sq"
rows <- list()
if (!is.null(s$pTerms.table) && nrow(s$pTerms.table) > 0) {
p <- s$pTerms.table
rows$p <- data.frame(edf = NA_real_, Df = p[, "df"], stat = p[, stat_col],
pval = p[, "p-value"], row.names = rownames(p))
}
if (!is.null(s$s.table) && nrow(s$s.table) > 0) {
sm <- s$s.table
rows$s <- data.frame(edf = sm[, "edf"], Df = sm[, "Ref.df"], stat = sm[, stat_col],
pval = sm[, "p-value"], row.names = rownames(sm))
}
tab <- do.call(rbind, unname(rows))
if (!is.null(tab)) {
tab$RESI <- if (stat_name == "F") {
f2S(tab$stat, tab$Df, rdf, n)
} else {
chisq2S(tab$stat, tab$Df, n)
}
# mgcv tests smooths without an unpenalized null space with a
# different statistic (Wood, 2013b), which has no RESI conversion
# (bs = "re", shrinkage bases, select = TRUE), so these get NA.
re_test <- vapply(model.full$smooth, function(sm) {
!isTRUE(sm$fixed) && identical(as.integer(sm$null.space.dim), 0L)
}, logical(1L))
no_null <- vapply(model.full$smooth, `[[`, character(1L), "label")[re_test]
if (length(no_null)) {
tab[rownames(tab) %in% no_null, "RESI"] <- NA_real_
warning("\nRESI is not computed for smooth terms without an unpenalized ",
"null space: ", paste(no_null, collapse = ", "))
}
colnames(tab)[3:4] <- c(stat_name, pv_name)
attr(tab, "heading") <- "Analysis of Deviance Table (Wald tests from mgcv::summary.gam)\n"
class(tab) <- c("anova_resi", "anova", "data.frame")
output$anova <- tab
est <- c(est, stats::setNames(tab$RESI, rownames(tab)))
}
}

output$estimates <- est
output$naive.var <- TRUE
class(output) <- c("resi", "list")
output
}

#' @describeIn resi RESI point estimates for generalized additive models.
#' Confidence intervals are not yet available for \code{gam} and \code{bam}
#' fits; use \code{\link{resi_pe}}.
#' @export
resi.gam <- function(model.full, ...) {
stop("\nConfidence intervals are not yet implemented for gam and bam models. ",
"Use resi_pe() for point estimates.")
}
9 changes: 8 additions & 1 deletion man/resi.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion man/resi_pe.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions tests/testthat/test-resi_pe_gam.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
test_that("resi_pe.gam converts mgcv Wald statistics", {
skip_if_not_installed("mgcv")
ins <- RESI::insurance
ins$region <- factor(ins$region)
b <- mgcv::gam(log10(charges) ~ smoker + region + s(age) + s(bmi),
data = ins, method = "REML")
s <- mgcv::summary.gam(b)
r <- resi_pe(b)
n <- nrow(ins)
expect_equal(r$coefficients["smoker", "RESI"],
t2S(s$p.table["smoker", "t value"], b$df.residual, n))
expect_equal(r$anova["s(age)", "RESI"],
f2S(s$s.table["s(age)", "F"], s$s.table["s(age)", "Ref.df"],
b$df.residual, n))
expect_equal(r$anova["region", "RESI"],
f2S(s$pTerms.table["region", "F"], 3, b$df.residual, n))

bb <- mgcv::gam(I(charges > 15000) ~ smoker + s(age), family = binomial,
data = ins)
sb <- mgcv::summary.gam(bb)
rb <- resi_pe(bb)
expect_equal(rb$anova["s(age)", "RESI"],
chisq2S(sb$s.table["s(age)", "Chi.sq"],
sb$s.table["s(age)", "Ref.df"], n))
expect_true("z value" %in% colnames(rb$coefficients))
})

test_that("resi_pe.gam returns NA for smooths without a null space", {
skip_if_not_installed("mgcv")
ins <- RESI::insurance
ins$region <- factor(ins$region)
b <- mgcv::bam(log10(charges) ~ smoker + s(age) + s(region, bs = "re"),
data = ins)
expect_warning(r <- resi_pe(b), "null space")
expect_true(is.na(r$anova["s(region)", "RESI"]))
expect_false(is.na(r$anova["s(age)", "RESI"]))
expect_error(resi(b), "resi_pe")
})
Loading