@@ -305,12 +305,13 @@ tidy.sdmTMB <- function(x, effects = c("fixed", "ran_pars", "ran_vals", "ran_vco
305
305
}
306
306
307
307
# optional time-varying random components
308
- if (! is.null(x $ time_varying )) {
308
+ if (! is.null(x $ time_varying )) {
309
309
tv_names <- colnames(model.matrix(x $ time_varying , x $ data ))
310
310
time_slices <- x $ time_lu $ time_from_data
311
311
yrs <- rep(time_slices , times = length(tv_names ))
312
312
313
313
out_ranef_tv <- data.frame (
314
+ model = model ,
314
315
term = paste0(rep(tv_names , each = length(time_slices )), " :" , yrs ),
315
316
estimate = c(est $ b_rw_t ),
316
317
std.error = c(se $ b_rw_t ),
@@ -322,6 +323,8 @@ tidy.sdmTMB <- function(x, effects = c("fixed", "ran_pars", "ran_vals", "ran_vco
322
323
if (is.null(out_ranef )) {
323
324
out_ranef <- out_ranef_tv
324
325
} else {
326
+ out_ranef_tv $ group_name <- NA
327
+ out_ranef_tv $ level_ids <- NA
325
328
out_ranef <- rbind(out_ranef , out_ranef_tv )
326
329
}
327
330
}
@@ -395,23 +398,23 @@ get_re_tidy_list <- function(x, crit) {
395
398
re_b_df $ estimate <- x $ sd_report $ value [re_indx ][non_nas ]
396
399
re_b_df $ std.error <- x $ sd_report $ sd [re_indx ][non_nas ]
397
400
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
399
402
re_b_df $ index <- NULL
400
403
re_b_df $ group_name <- NA
401
- re_b_df $ par_name <- NA
404
+ re_b_df $ term <- NA
402
405
for (i in seq_len(length(x $ split_formula ))) {
403
406
groupnames <- names(x $ split_formula [[i ]]$ re_cov_terms $ cnms )
404
407
for (j in seq_len(length(x $ split_formula [[i ]]$ barnames ))) {
405
408
model_grp <- which(re_b_df $ model == i & re_b_df $ group_id == j )
406
409
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 ))
408
411
}
409
412
}
410
413
group_key <- stats :: aggregate(group_name ~ model + group_id , data = re_b_df , FUN = function (x ) x [1 ])
411
414
412
415
# more sensible re-ordering
413
416
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 " )]
415
418
# remove ":" in the level_ids
416
419
re_b_df $ level_ids <- sapply(strsplit(re_b_df $ level_ids , " :" ), function (x ) x [2 ])
417
420
out_ranef <- re_b_df
@@ -439,21 +442,21 @@ get_re_tidy_list <- function(x, crit) {
439
442
re_cov_df $ estimate <- x $ sd_report $ value [re_indx ][non_nas ]
440
443
re_cov_df $ std.error <- x $ sd_report $ sd [re_indx ][non_nas ]
441
444
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
443
446
# the SD parameters are returned in log space -- use delta method to generate CIs
444
447
est <- exp(re_cov_df $ estimate ) # estimate in normal space
445
448
sd_est <- est * re_cov_df $ std.error # SE in normal space
446
449
sds <- which(re_cov_df $ is_sd == 1 ) # index which elements are SDs
447
450
re_cov_df $ estimate [sds ] <- est [sds ]
448
451
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 ]
450
453
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 " )]
452
455
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 " )
454
457
list (out_ranef = out_ranef , cov_matrices = create_cov_matrices(re_cov_df ),
455
458
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 " ))
457
460
}
458
461
459
462
create_cov_matrices <- function (df , col_name = " estimate" ) {
0 commit comments