-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathREADME.Rmd
123 lines (79 loc) · 5.09 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
---
output:
github_document:
toc: false # doesn't show well on website, where it isn't really needed
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
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](https://m-clark.github.io/mixedup/) package, which provides all the same functionality and more, while the visualizations were originally from the [visibly](https://m-clark.github.io/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
<!-- badges: start -->
[](https://travis-ci.org/m-clark/gammit)
[](https://ci.appveyor.com/project/m-clark/gammit)
[](https://codecov.io/gh/m-clark/gammit?branch=master)
[](https://www.tidyverse.org/lifecycle/#superseded)
<!-- badges: end -->
## Introduction
The goal of gammit is to provide a set of functions to aid using <span class="pack" style = "">mgcv</span> (possibly solely) for mixed models. Lately I've been using it in lieu of <span class="pack" style = "">lme4</span>, especially the <span class="func" style = "">bam</span> function, for GLMM with millions of observations and multiple random effects. It's turning out very useful in this sense (see [this post](https://m-clark.github.io/posts/2019-10-20-big-mixed-models/) for details), but I'd like some more/different functionality with the results. Furthermore, <span class="pack" style = "">mgcv</span> 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: <span class="func" style = "">extract_vc</span>, <span class="func" style = "">extract_ranef</span>, <span class="func" style = "">extract_fixed</span>, <span class="func" style = "">summary_gamm</span>, and <span class="func" style = "">predict_gamm.</span>
## Installation
You can install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("m-clark/gammit")
```
## Example
This example demonstrates the <span class="func" style = "">summary_gamm</span> function with comparison to the corresponding <span class="pack" style = "">lme4</span> model.
```{r summary}
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 <span class="func" style = "">extract_vc</span>.
```{r extract_vc}
data.frame(VarCorr(lmer_model))
extract_vc(ga_model)
```
Extract the random effects with <span class="func" style = "">extract_ranef</span>.
```{r extract_ranef}
ranef(lmer_model)
extract_ranef(ga_model)
```
Extract the fixed effects <span class="func" style = "">extract_fixef</span>.
```{r extract_fixed}
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 <span class="pack" style = "">lme4</span> style to include random effects or not. <span class="pack" style = "">mgcv</span> already has this functionality as well, so the functionality of <span class="func" style = "">predict_gamm</span> is mostly cosmetic. One benefit here is to provide standard errors for the prediction also.
```{r predict_gamm}
head(predict_gamm(ga_model))
```
Add standard errors.
```{r predict_gamm_se}
head(data.frame(predict_gamm(ga_model, se=T)))
```
```{r predict_gamm_fe}
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 <span class="objclass" style = "">bam</span> objects, but this is to due to the functionality in <span class="func" style = "">predict.gam</span> from <span class="pack" style = "">mgcv</span> and should change in the near future.