-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmethods.R
322 lines (297 loc) · 8.78 KB
/
methods.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#' @import methods
#' @export
summary.sdmTMB <- function(object, ..., digits) {
print(object, ...)
}
mround <- function(x, digits) {
sprintf(paste0("%.", digits, "f"), round(x, digits))
}
#' Extract the number of observations of an sdmTMB model
#'
#' @param object The fitted sdmTMB model object
#' @importFrom stats nobs
#' @export
#' @noRd
nobs.sdmTMB <- function(object, ...) {
sum(!is.na(object$data[all.vars(object$formula[[1]])[1]]))
}
#' Get fitted values from an sdmTMB model
#'
#' @param object The fitted sdmTMB model object
#' @importFrom stats predict
#' @export
#' @noRd
fitted.sdmTMB <- function(object, ...) {
if (!"offset" %in% names(object))
cli_abort("It looks like this was fit with an older version of sdmTMB. Try sdmTMB:::update_version(fit).")
if (isTRUE(object$family$delta)) {
inv1 <- object$family[[1]]$linkinv
p <- predict(object, type = "link", offset = object$offset)
p1 <- inv1(p$est1)
inv2 <- object$family[[2]]$linkinv
p2 <- inv2(p$est2)
p1 * p2
} else {
inv <- object$family$linkinv
inv(predict(object, type = "link", offset = object$offset)$est)
}
}
#' Get fixed-effect coefficients
#'
#' @param object The fitted sdmTMB model object
#' @param complete Currently ignored
#' @param model Linear predictor for delta models. Defaults to the first
#' linear predictor.
#' @param ... Currently ignored
#' @importFrom stats coef
#' @export
coef.sdmTMB <- function(object, complete = FALSE, model = 1, ...) {
if (is_delta(object)) {
assert_that(length(model) == 1L)
model <- as.integer(model)
assert_that(model %in% c(1L, 2L))
msg <- paste0("Returning coefficients from linear predictor ", model, " based on the `model` argument.")
cli_inform(msg)
}
x <- tidy(object, model = model)
out <- x$estimate
names(out) <- x$term
out
}
#' Get variance-covariance matrix
#'
#' @param object The fitted sdmTMB model object
#' @param complete Currently ignored
#' @param ... Currently ignored
#' @importFrom stats vcov
#' @export
#' @noRd
vcov.sdmTMB <- function(object, complete = FALSE, ...) {
sdr <- object$sd_report
v <- sdr$cov.fixed
fe <- tidy(object)$term
nm <- colnames(v)
i <- grepl("^b_j$", nm)
if (sum(i)) {
if (sum(i) == length(fe)) { # should always be true
nm[i] <- fe
}
}
colnames(v) <- nm
rownames(v) <- nm
if (isTRUE(complete)) {
return(v)
} else {
return(v[i,i,drop=FALSE])
}
}
#' Get CIs
#'
#' @param object The fitted sdmTMB model object
#' @param parm Parameters to return CIs
#' @param level CI level
#' @param ... Ignored
#' @importFrom stats confint
#' @export
#' @noRd
confint.sdmTMB <- function(object, parm, level = 0.95, ...) {
td <- tidy(object, conf.int = TRUE, conf.level = level)
x <- matrix(nrow = nrow(td), ncol = 3L)
x[,3L] <- td$estimate
x[,2L] <- td$conf.high
x[,1L] <- td$conf.low
p <- ((1 - level) / 2) * 100
pn <- paste(c(p, 100 - p), "%")
colnames(x) <- c(pn, "Estimate")
rownames(x) <- td$term
x
}
#' Extract the log likelihood of a sdmTMB model
#'
#' @param object The fitted sdmTMB model object
#' @importFrom stats logLik
#' @export
#' @noRd
logLik.sdmTMB <- function(object, ...) {
val <- -object$model$objective
nobs <- nobs.sdmTMB(object)
lpb <- names(object$tmb_obj$env$last.par.best)
ran <- c("omega_s", "epsilon_st", "zeta_s", "b_rw_t", "epsilon_re", "RE", "b_smooth", "re_b_pars")
df <- sum(!lpb %in% ran)
structure(val,
nobs = nobs, nall = nobs, df = df,
class = "logLik"
)
}
#' Extract the AIC of a sdmTMB model
#'
#' @param fit The fitted sdmTMB model
#' @param scale The scale (note used)
#' @param k Penalization parameter, defaults to 2
#' @param ... Anything else
#' @noRd
#'
#' @export
extractAIC.sdmTMB <- function(fit, scale, k = 2, ...) {
L <- logLik(fit)
edf <- attr(L, "df")
return(c(edf, c(-2 * L + k * edf)))
}
#' @importFrom stats family
#' @export
family.sdmTMB <- function (object, ...) {
if (.has_delta_attr(object)) {
which_model <- attr(object, "delta_model_predict")
if (is.na(which_model)) which_model <- 2L # combined; for link
return(object$family[[which_model]])
}
if ("visreg_model" %in% names(object)) {
return(object$family[[object$visreg_model]])
} else {
return(object$family)
}
}
#' @importFrom nlme fixef
#' @method fixef sdmTMB
#' @export
fixef.sdmTMB <- function(object, ...) {
.t <- tidy(object, silent = TRUE)
bhat <- .t$estimate
names(bhat) <- .t$term
bhat
}
##' @importFrom nlme ranef
#' @method ranef sdmTMB
#' @export
ranef.sdmTMB <- function(object, ...) {
.t <- tidy(object, "ran_vals", conf.int = FALSE, silent = TRUE)
model_list <- list()
for (i in seq_len(max(.t$model))) { # loop through models
.t <- .t[which(.t$model == i), ]
groups <- unique(.t$group_name) # names of groups for this model
group_list <- vector("list", length = length(groups)) # create empty named list
names(group_list) <- groups
for (j in 1:length(groups)) {
sub <- .t[which(.t$group_name == groups[j]), ]
level_ids <- unique(sub$level_ids)
sub <- sub[, c("group_name", "term", "estimate")]
if (nrow(sub) > 0) {
# convert long to wide, storing just estimates
split_data <- split(sub$estimate, sub$term)
wide_df <- as.data.frame(split_data) # Convert to wide format
names(wide_df) <- unique(sub$term) # rename, fix .X issue
rownames(wide_df) <- level_ids # add rownames, like lmer does
# Create a list with the dataframe as an element named 'Dog'
group_list[[j]] <- wide_df
# names(group_list[[j]]) <- sub$group_name[1]
} # end if
} # end for j
model_list[[i]] <- group_list
}
model_list
}
#' @importFrom stats df.residual
#' @method df.residual sdmTMB
#' @export
df.residual.sdmTMB <- function(object, ...) {
nobs(object) - length(object$model$par)
}
.has_delta_attr <- function(x) {
"delta_model_predict" %in% names(attributes(x))
}
#' @export
formula.sdmTMB <- function (x, ...) {
if (.has_delta_attr(x)) {
which_model <- attr(x, "delta_model_predict")
if (!identical(x$formula[[1]], x$formula[[2]]) && is.na(which_model)) {
cli_abort("Delta component formulas are not the same but ggeffects::ggpredict() is trying to predict on the combined model. For now, predict on one or the other component, or keep the formulas the same, or write your own prediction and plot code.")
}
if (is.na(which_model)) which_model <- 1L # combined take 1!?
return(x$formula[[which_model]])
}
if (length(x$formula) > 1L) {
if ("visreg_model" %in% names(x)) {
return(x$formula[[x$visreg_model]])
} else {
return(x$formula)
}
} else {
return(x$formula[[1]])
}
}
#' @importFrom stats terms
#' @export
terms.sdmTMB <- function(x, ...) {
# DELTA FIXME: hardcoded to model 1!
class(x) <- "glm" # fake
out <- stats::terms(x)
out[[1]]
}
#' Calculate effects
#'
#' Used by effects package
#'
#' @inheritParams effects::Effect
#'
#' @importFrom stats formula poisson
#'
#' @return
#' Output from [effects::effect()]. Can then be plotted with with associated
#' `plot()` method.
#'
#' @rawNamespace if(getRversion() >= "3.6.0") {
#' S3method(effects::Effect, sdmTMB)
#' } else {
#' export(Effect.sdmTMB)
#' }
#' @examplesIf require("effects", quietly = TRUE)
#' fit <- sdmTMB(present ~ depth_scaled, data = pcod_2011, family = binomial(),
#' spatial = "off")
#' effects::effect("depth_scaled", fit)
#' plot(effects::effect("depth_scaled", fit))
Effect.sdmTMB <- function(focal.predictors, mod, ...) {
if (!requireNamespace("effects", quietly = TRUE)) {
cli_abort("Please install the effects package")
}
if (is_delta(mod)) {
msg <- paste0("Effect() and ggeffects::ggeffect() do not yet work with ",
"sdmTMB delta/hurdle models. Please use ggeffects::ggpredict() instead.")
cli_abort(msg)
}
vc <- vcov(mod)
b <- tidy(mod, silent = TRUE)
dummyfuns <- list(
variance = function(mu) mu,
initialize = expression(mustart = y + 0.1),
dev.resids = function(...) stats::poisson()$dev.res(...)
)
fam <- family(mod)
# from glmmTMB:
dummyfuns <- list(
variance = function(mu) mu,
initialize = expression(mustart <- y + 0.1),
dev.resids = function(...) poisson()$dev.res(...)
)
for (i in names(dummyfuns)) {
if (is.null(fam[[i]])) fam[[i]] <- dummyfuns[[i]]
}
coefs <- b$estimate
names(coefs) <- b$term
args <- list(
call = mod$call,
coefficients = coefs,
vcov = vc,
family = fam,
# formula = formula(mod) # includes random intercepts...
formula = remove_s_and_t2(mod$split_formula[[1]]$form_no_bars)
)
effects::Effect.default(focal.predictors, mod, ..., sources = args)
}
# get_term_names <- function(model) {
# .names <- gsub(" ", "", labels(terms(model)))
# .names
# }
#' @export
model.frame.sdmTMB <- function(formula, ...) {
as.data.frame(formula$data) # no tibbles!
}