Skip to content

Commit c697034

Browse files
committed
Fix printing/tidying with random intercepts + time-varying #426
Since new ranef implementation
1 parent 9132de4 commit c697034

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: sdmTMB
33
Title: Spatial and Spatiotemporal SPDE-Based GLMMs with 'TMB'
4-
Version: 0.6.0.9033
4+
Version: 0.6.0.9034
55
Authors@R: c(
66
person(c("Sean", "C."), "Anderson", , "sean@seananderson.ca",
77
role = c("aut", "cre"),

R/methods.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ ranef.sdmTMB <- function(object, ...) {
186186
for (j in 1:length(groups)) {
187187
sub <- .t[which(.t$group_name == groups[j]), ]
188188
level_ids <- unique(sub$level_ids)
189-
sub <- sub[, c("group_name", "par_name", "estimate")]
189+
sub <- sub[, c("group_name", "term", "estimate")]
190190
if (nrow(sub) > 0) {
191191
# convert long to wide, storing just estimates
192-
split_data <- split(sub$estimate, sub$par_name)
192+
split_data <- split(sub$estimate, sub$term)
193193
wide_df <- as.data.frame(split_data) # Convert to wide format
194-
names(wide_df) <- unique(sub$par_name) # rename, fix .X issue
194+
names(wide_df) <- unique(sub$term) # rename, fix .X issue
195195
rownames(wide_df) <- level_ids # add rownames, like lmer does
196196
# Create a list with the dataframe as an element named 'Dog'
197197
group_list[[j]] <- wide_df

R/tidy.R

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,13 @@ tidy.sdmTMB <- function(x, effects = c("fixed", "ran_pars", "ran_vals", "ran_vco
305305
}
306306

307307
# optional time-varying random components
308-
if(!is.null(x$time_varying)) {
308+
if (!is.null(x$time_varying)) {
309309
tv_names <- colnames(model.matrix(x$time_varying, x$data))
310310
time_slices <- x$time_lu$time_from_data
311311
yrs <- rep(time_slices, times = length(tv_names))
312312

313313
out_ranef_tv <- data.frame(
314+
model = model,
314315
term = paste0(rep(tv_names, each = length(time_slices)), ":", yrs),
315316
estimate = c(est$b_rw_t),
316317
std.error = c(se$b_rw_t),
@@ -322,6 +323,8 @@ tidy.sdmTMB <- function(x, effects = c("fixed", "ran_pars", "ran_vals", "ran_vco
322323
if(is.null(out_ranef)) {
323324
out_ranef <- out_ranef_tv
324325
} else {
326+
out_ranef_tv$group_name <- NA
327+
out_ranef_tv$level_ids <- NA
325328
out_ranef <- rbind(out_ranef, out_ranef_tv)
326329
}
327330
}
@@ -395,23 +398,23 @@ get_re_tidy_list <- function(x, crit) {
395398
re_b_df$estimate <- x$sd_report$value[re_indx][non_nas]
396399
re_b_df$std.error <- x$sd_report$sd[re_indx][non_nas]
397400
re_b_df$conf.low <- re_b_df$estimate - crit * re_b_df$std.error
398-
re_b_df$conf.hi <- re_b_df$estimate + crit * re_b_df$std.error
401+
re_b_df$conf.high <- re_b_df$estimate + crit * re_b_df$std.error
399402
re_b_df$index <- NULL
400403
re_b_df$group_name <- NA
401-
re_b_df$par_name <- NA
404+
re_b_df$term <- NA
402405
for (i in seq_len(length(x$split_formula))) {
403406
groupnames <- names(x$split_formula[[i]]$re_cov_terms$cnms)
404407
for (j in seq_len(length(x$split_formula[[i]]$barnames))) {
405408
model_grp <- which(re_b_df$model == i & re_b_df$group_id == j)
406409
re_b_df$group_name[model_grp] <- groupnames[j]
407-
re_b_df$par_name[model_grp] <- rep(x$split_formula[[i]]$re_cov_terms$cnms[[j]], length.out = length(model_grp))
410+
re_b_df$term[model_grp] <- rep(x$split_formula[[i]]$re_cov_terms$cnms[[j]], length.out = length(model_grp))
408411
}
409412
}
410413
group_key <- stats::aggregate(group_name ~ model + group_id, data = re_b_df, FUN = function(x) x[1])
411414

412415
# more sensible re-ordering
413416
re_b_df$group_id <- NULL
414-
re_b_df <- re_b_df[, c("model", "group_name", "par_name", "level_ids", "estimate", "std.error", "conf.low", "conf.hi")]
417+
re_b_df <- re_b_df[, c("model", "group_name", "term", "level_ids", "estimate", "std.error", "conf.low", "conf.high")]
415418
# remove ":" in the level_ids
416419
re_b_df$level_ids <- sapply(strsplit(re_b_df$level_ids, ":"), function(x) x[2])
417420
out_ranef <- re_b_df
@@ -439,21 +442,21 @@ get_re_tidy_list <- function(x, crit) {
439442
re_cov_df$estimate <- x$sd_report$value[re_indx][non_nas]
440443
re_cov_df$std.error <- x$sd_report$sd[re_indx][non_nas]
441444
re_cov_df$conf.low <- re_cov_df$estimate - crit * re_cov_df$std.error
442-
re_cov_df$conf.hi <- re_cov_df$estimate + crit * re_cov_df$std.error
445+
re_cov_df$conf.high <- re_cov_df$estimate + crit * re_cov_df$std.error
443446
# the SD parameters are returned in log space -- use delta method to generate CIs
444447
est <- exp(re_cov_df$estimate) # estimate in normal space
445448
sd_est <- est * re_cov_df$std.error # SE in normal space
446449
sds <- which(re_cov_df$is_sd == 1) # index which elements are SDs
447450
re_cov_df$estimate[sds] <- est[sds]
448451
re_cov_df$conf.low[sds] <- est[sds] - crit * sd_est[sds]
449-
re_cov_df$conf.hi[sds] <- est[sds] + crit * sd_est[sds]
452+
re_cov_df$conf.high[sds] <- est[sds] + crit * sd_est[sds]
450453

451-
re_cov_df <- re_cov_df[, c("rows", "cols", "model", "group", "estimate", "std.error", "conf.low", "conf.hi")]
454+
re_cov_df <- re_cov_df[, c("rows", "cols", "model", "group", "estimate", "std.error", "conf.low", "conf.high")]
452455
cov_matrices_lo = create_cov_matrices(re_cov_df, col_name = "conf.low")
453-
cov_matrices_hi = create_cov_matrices(re_cov_df, col_name = "conf.hi")
456+
cov_matrices_hi = create_cov_matrices(re_cov_df, col_name = "conf.high")
454457
list(out_ranef = out_ranef, cov_matrices = create_cov_matrices(re_cov_df),
455458
cov_matrices_lo = create_cov_matrices(re_cov_df, col_name = "conf.low"),
456-
cov_matrices_hi = create_cov_matrices(re_cov_df, col_name = "conf.hi"))
459+
cov_matrices_hi = create_cov_matrices(re_cov_df, col_name = "conf.high"))
457460
}
458461

459462
create_cov_matrices <- function(df, col_name = "estimate") {

tests/testthat/test-tidy.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,33 @@ test_that("tidy works", {
8686
expect_true("cv_split" %in% names(allmodels))
8787
expect_equal(allmodels$cv_split, sort(rep(1:2,2)))
8888
})
89+
90+
test_that("printing/tidying works with a delta model that has random intercepts + an AR1 time series #426", {
91+
skip_on_cran()
92+
d <- pcod
93+
d$fake <- rep(c("a", "b", "c"), 9999)[1:nrow(d)]
94+
fit <- sdmTMB(
95+
density ~ breakpt(depth_scaled) + (1|fake),
96+
data = d,
97+
time = "year",
98+
extra_time = c(2006, 2008, 2010, 2012, 2014, 2016),
99+
time_varying = ~1,
100+
time_varying_type = "ar1",
101+
mesh = mesh,
102+
spatial = "off",
103+
spatiotemporal = "off",
104+
family = delta_gamma(type = "poisson-link")
105+
)
106+
b <- tidy(fit, effects = "ran_pars")
107+
b <- tidy(fit, effects = "ran_vals")
108+
expect_identical(b$term, c("(Intercept)", "(Intercept)", "(Intercept)", "(Intercept)",
109+
"(Intercept)", "(Intercept)", "(Intercept):2003", "(Intercept):2004",
110+
"(Intercept):2005", "(Intercept):2006", "(Intercept):2007", "(Intercept):2008",
111+
"(Intercept):2009", "(Intercept):2010", "(Intercept):2011", "(Intercept):2012",
112+
"(Intercept):2013", "(Intercept):2014", "(Intercept):2015", "(Intercept):2016",
113+
"(Intercept):2017", "(Intercept):2003", "(Intercept):2004", "(Intercept):2005",
114+
"(Intercept):2006", "(Intercept):2007", "(Intercept):2008", "(Intercept):2009",
115+
"(Intercept):2010", "(Intercept):2011", "(Intercept):2012", "(Intercept):2013",
116+
"(Intercept):2014", "(Intercept):2015", "(Intercept):2016", "(Intercept):2017"
117+
))
118+
})

0 commit comments

Comments
 (0)