Skip to content

Commit

Permalink
bug in summary when multiple random effects are used
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Clark committed Nov 15, 2023
1 parent 0cf0567 commit b4eeb55
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
90 changes: 66 additions & 24 deletions R/add_MACor.R
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,17 @@ add_MaCor = function(model_file,

if(add_cor){
if(trend_model %in% c('AR1', 'RW')){
model_file[max(grep('= b_raw[',
model_file, fixed = TRUE))] <-
paste0(model_file[max(grep('= b_raw[',
model_file, fixed = TRUE))],
if(any(grepl('= mu_raw[',
model_file, fixed = TRUE))){
insert_line <- max(grep('= mu_raw[',
model_file, fixed = TRUE))
} else if(any(grepl('= b_raw[',
model_file, fixed = TRUE))){
insert_line <- max(grep('= b_raw[',
model_file, fixed = TRUE))
}
model_file[insert_line] <-
paste0(model_file[insert_line],
'\n// derived latent states\n',
'trend_raw[1] = ',
if(drift){ 'drift + '} else {NULL},
Expand Down Expand Up @@ -328,10 +335,17 @@ add_MaCor = function(model_file,
}

if(trend_model == 'AR2'){
model_file[max(grep('= b_raw[',
model_file, fixed = TRUE))] <-
paste0(model_file[max(grep('= b_raw[',
model_file, fixed = TRUE))],
if(any(grepl('= mu_raw[',
model_file, fixed = TRUE))){
insert_line <- max(grep('= mu_raw[',
model_file, fixed = TRUE))
} else if(any(grepl('= b_raw[',
model_file, fixed = TRUE))){
insert_line <- max(grep('= b_raw[',
model_file, fixed = TRUE))
}
model_file[insert_line] <-
paste0(model_file[insert_line],
'\n// derived latent states\n',
'trend_raw[1] = ',
if(drift){ 'drift + '} else {NULL},
Expand Down Expand Up @@ -371,10 +385,17 @@ add_MaCor = function(model_file,
}

if(trend_model == 'AR3'){
model_file[max(grep('= b_raw[',
model_file, fixed = TRUE))] <-
paste0(model_file[max(grep('= b_raw[',
model_file, fixed = TRUE))],
if(any(grepl('= mu_raw[',
model_file, fixed = TRUE))){
insert_line <- max(grep('= mu_raw[',
model_file, fixed = TRUE))
} else if(any(grepl('= b_raw[',
model_file, fixed = TRUE))){
insert_line <- max(grep('= b_raw[',
model_file, fixed = TRUE))
}
model_file[insert_line] <-
paste0(model_file[insert_line],
'\n// derived latent states\n',
'trend_raw[1] = ',
if(drift){ 'drift + '} else {NULL},
Expand Down Expand Up @@ -426,10 +447,17 @@ add_MaCor = function(model_file,

} else {
if(trend_model %in% c('AR1', 'RW')){
model_file[max(grep('= b_raw[',
model_file, fixed = TRUE))] <-
paste0(model_file[max(grep('= b_raw[',
model_file, fixed = TRUE))],
if(any(grepl('= mu_raw[',
model_file, fixed = TRUE))){
insert_line <- max(grep('= mu_raw[',
model_file, fixed = TRUE))
} else if(any(grepl('= b_raw[',
model_file, fixed = TRUE))){
insert_line <- max(grep('= b_raw[',
model_file, fixed = TRUE))
}
model_file[insert_line] <-
paste0(model_file[insert_line],
'for(j in 1:n_series){\n',
'trend[1, j] = ',
if(drift){ 'drift[j] + '} else {NULL},
Expand All @@ -448,10 +476,17 @@ add_MaCor = function(model_file,
}

if(trend_model == 'AR2'){
model_file[max(grep('= b_raw[',
model_file, fixed = TRUE))] <-
paste0(model_file[max(grep('= b_raw[',
model_file, fixed = TRUE))],
if(any(grepl('= mu_raw[',
model_file, fixed = TRUE))){
insert_line <- max(grep('= mu_raw[',
model_file, fixed = TRUE))
} else if(any(grepl('= b_raw[',
model_file, fixed = TRUE))){
insert_line <- max(grep('= b_raw[',
model_file, fixed = TRUE))
}
model_file[insert_line] <-
paste0(model_file[insert_line],
'for(j in 1:n_series){\n',
'trend[1, j] = ',
if(drift){ 'drift[j] + '} else {NULL},
Expand All @@ -475,10 +510,17 @@ add_MaCor = function(model_file,
}

if(trend_model == 'AR3'){
model_file[max(grep('= b_raw[',
model_file, fixed = TRUE))] <-
paste0(model_file[max(grep('= b_raw[',
model_file, fixed = TRUE))],
if(any(grepl('= mu_raw[',
model_file, fixed = TRUE))){
insert_line <- max(grep('= mu_raw[',
model_file, fixed = TRUE))
} else if(any(grepl('= b_raw[',
model_file, fixed = TRUE))){
insert_line <- max(grep('= b_raw[',
model_file, fixed = TRUE))
}
model_file[insert_line] <-
paste0(model_file[insert_line],
'for(j in 1:n_series){\n',
'trend[1, j] = ',
if(drift){ 'drift[j] + '} else {NULL},
Expand Down
4 changes: 2 additions & 2 deletions R/summary.mvgam.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ if(all(is.na(object$sp_names))){

} else {
if(any(unlist(purrr::map(object$mgcv_model$smooth, inherits, 'random.effect')))){
re_labs <- unlist(lapply(purrr::map(object$mgcv_model$smooth, 'term'),
re_labs <- unlist(lapply(purrr::map(object$mgcv_model$smooth, 'label'),
paste, collapse = ','))[
unlist(purrr::map(object$mgcv_model$smooth, inherits, 'random.effect'))]
re_sds <- mcmc_summary(object$model_output, 'sigma_raw',
Expand Down Expand Up @@ -601,7 +601,7 @@ if(!is.null(object$trend_call)){

} else {
if(any(unlist(purrr::map(object$trend_mgcv_model$smooth, inherits, 'random.effect')))){
re_labs <- unlist(lapply(purrr::map(object$trend_mgcv_model$smooth, 'term'),
re_labs <- unlist(lapply(purrr::map(object$trend_mgcv_model$smooth, 'label'),
paste, collapse = ','))[
unlist(purrr::map(object$trend_mgcv_model$smooth, inherits, 'random.effect'))]
re_labs <- gsub('series', 'trend', re_labs)
Expand Down
Binary file modified src/mvgam.dll
Binary file not shown.
Binary file modified tests/testthat/Rplots.pdf
Binary file not shown.

0 comments on commit b4eeb55

Please sign in to comment.