knitr::opts_chunk$set(
  collapse = TRUE,
  comment = NA,
  fig.path = "man/figures/README-",
  out.width = "100%"
)

gammit

This package was specifically for mixed models using mgcv. At present it has been almost entirely superseded by the mixedup package, which provides all the same functionality and more, while the visualizations were originally from the visibly package anyway. That only leaves the prediction functionality as unique to this package, so I leave it here as I may still use it for that, and I'm contemplating moving the GAM specific visuals from visibly at some point.

Package Description

Travis build status AppVeyor build status Codecov test coverage Lifecycle: superseded

Introduction

The goal of gammit is to provide a set of functions to aid using mgcv (possibly solely) for mixed models. Lately I've been using it in lieu of lme4, especially the bam function, for GLMM with millions of observations and multiple random effects. It's turning out very useful in this sense (see this post for details), but I'd like some more/different functionality with the results. Furthermore, mgcv just has some nice things going on for such models anyway, like the ability to add other smooth terms, alternative distributions for the target variable, etc., so I'm looking to make it easier for me to get some things I want when I use it.

At present there are four functions: extract_vc, extract_ranef, extract_fixed, summary_gamm, and predict_gamm.

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("m-clark/gammit")

Example

This example demonstrates the summary_gamm function with comparison to the corresponding lme4 model.

library(mgcv)
library(lme4)
library(gammit)

lmer_model = lmer(Reaction ~  Days + (Days || Subject), data=sleepstudy)

ga_model = gam(Reaction ~  Days + s(Subject, bs='re') + s(Days, Subject, bs='re'),
               data=sleepstudy,
               method = 'REML')

summary(lmer_model)


summary_gamm(ga_model)

Extract the variance components with extract_vc.

data.frame(VarCorr(lmer_model))


extract_vc(ga_model)

Extract the random effects with extract_ranef.

ranef(lmer_model)


extract_ranef(ga_model)

Extract the fixed effects extract_fixef.

fixef(lmer_model)


extract_fixed(ga_model)

Prediction

There are a couple of ways to do prediction, and the main goal for gammit was to make it easy to use the lme4 style to include random effects or not. mgcv already has this functionality as well, so the functionality of predict_gamm is mostly cosmetic. One benefit here is to provide standard errors for the prediction also.

head(predict_gamm(ga_model))

Add standard errors.

head(data.frame(predict_gamm(ga_model, se=T)))
compare = data.frame(
  gam_original  = predict_gamm(ga_model)$prediction,
  gam_fe_only   = predict_gamm(ga_model, re_form = NA)$prediction,
  gam_fe_only2  = predict_gamm(ga_model, 
                               exclude =  c('s(Subject)', "s(Days,Subject)"))$prediction,
  lme4_fe_only  = predict(lmer_model, re.form = NA))

head(compare)

Along with that, one can still use include/exclude for other smooth terms as above. Unfortunately, some options do not yet work with bam objects, but this is to due to the functionality in predict.gam from mgcv and should change in the near future.



m-clark/gammit documentation built on Oct. 22, 2020, 6:53 p.m.