-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfun_gamm.R
143 lines (106 loc) · 3.46 KB
/
fun_gamm.R
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
### Model fitting
gam_fit <- function(data,
formula,
list.vars = list.vars.gam,
family = NULL,
fun = gam,
method = "GCV.Cp",
print.var = TRUE,
print.summary = FALSE,
...) {
if (is.null(family)) {family <- gaussian}
list.gam <- list()
for (vv in list.vars) { #
if (print.var) {cat(paste0("\n Fitting ", vv))}
# Fit GAM
f <- paste(vv, formula)
if (is.logical(data[[vv]])) {
data[[vv]] <- as.factor(data[[vv]])
family <- binomial()
} else {
family <- gaussian()
}
gam.m <- fun(as.formula(f),
data = data,
family = family,
method = method,
...)
# discrete = T,
# nthreads = 4)
if (print.summary) {print(summary(gam.m))}
list.gam[[vv]] <- gam.m
}
return(list.gam)
}
### Extract smooths
gam_emmeans <- function(models, smooths = c(1), terms = list(day = 1:10)) {
# Enlist if necessary
if (any(class(models) != "list")) {models <- list("model" = models)}
# Initialize outcome dataframe
df.out <- tibble()
# Loop over models and requested smooths
for (m in names(models)) {
cat(paste0("\n Extracting from model ", m))
for (s in smooths) {
df.out %<>% bind_rows(
ggemmeans(models[[m]], terms = terms, type = "fixed") %>%
as.data.frame() %>%
mutate(variable = insight::find_response(models[[m]]),
smooth = insight::find_smooth(models[[m]])$smooth_terms[s],
smooth.idx = s,
model = m)
)
}
}
return(df.out)
}
### Extract smooths significance
p.to.sym <- function(p, ns = "n.s.") {
cut(p, breaks = c(-Inf, 0.001, 0.01, 0.05, Inf),
labels = c("***", "**", "*", ns), right = FALSE)
}
gam_signif <- function(models, smooths = c(1), method = "holm", family) {
# Enlist if necessary
if (any(class(models) != "list")) {models <- list("model" = models)}
# Flatten list if necessary
if (any(class(models[[1]]) == "list")) {models <- unlist(models, recursive = F)}
# Initialize outcome dataframe
df.out <- tibble()
# Loop over models and requested smooths
for (m in names(models)) {
smry <- summary(models[[m]], re.test=F)
for (s in smooths) {
df.out %<>% bind_rows(
tibble(variable = insight::find_response(models[[m]]),
smooth = insight::find_smooth(models[[m]])$smooth_terms[s],
smooth.idx = s,
p = smry$s.pv[s],
model = m))
}
}
# Apply Holm's correction for FWER & convert p-values to symbols
df.out <- df.out %>%
group_by(!!sym(family)) %>%
mutate(p.corr = p.adjust(p, method = method),
method = method) %>%
ungroup() %>%
mutate(sym = p.to.sym(p),
sym.corr = p.to.sym(p.corr))
return(df.out)
}
### Extract deviance explained
gam_devexpl <- function(models) {
# Enlist if necessary
if (any(class(models) != "list")) {models <- list("model" = models)}
# Initialize outcome dataframe
df.out <- tibble()
for (m in names(models)) {
f <- as.character(insight::find_formula(models[[m]])$conditional)
df.out %<>% bind_rows(
tibble(variable = f[2],
formula = f[3],
deviance = summary(models[[m]], re.test = F)$dev.expl)
)
}
return(df.out)
}