Skip to content

Instantly share code, notes, and snippets.

@samclifford
Last active February 25, 2022 10:31
Show Gist options
  • Save samclifford/e3bf65c3c389aae21b4cfadda629748a to your computer and use it in GitHub Desktop.
Save samclifford/e3bf65c3c389aae21b4cfadda629748a to your computer and use it in GitHub Desktop.
A method for extracting confidence intervals for parametric terms from an mgcv gamObject and returning a tidy data frame
confint.gam <- function(mod, level = 0.95) {
# a method for extracting confidence intervals and returning a tidy data frame
require(mgcv)
require(dplyr)
mod.s <- summary(mod)
E <- data.frame(Estimate = mod.s$p.coeff) %>%
mutate(Term = row.names(.)) %>%
select(Term, Estimate)
SE <- data.frame(SE = mod.s$se) %>%
mutate(Term = row.names(.)) %>%
select(Term, SE)
nu <- mod.s$residual.df
inner_join(E, SE) %>%
mutate(L = Estimate +
SE * qt(df = nu,
p = (1 - level) / 2),
U = Estimate +
SE * qt(df = nu,
p = 1 - (1 - level) / 2)) %>%
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment