diff --git a/R/add_MACor.R b/R/add_MACor.R index 984db65d..10ff6a23 100644 --- a/R/add_MACor.R +++ b/R/add_MACor.R @@ -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}, @@ -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}, @@ -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}, @@ -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}, @@ -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}, @@ -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}, diff --git a/R/summary.mvgam.R b/R/summary.mvgam.R index 34b8d30f..67c6acc4 100644 --- a/R/summary.mvgam.R +++ b/R/summary.mvgam.R @@ -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', @@ -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) diff --git a/src/mvgam.dll b/src/mvgam.dll index 2161ca82..a8ea9649 100644 Binary files a/src/mvgam.dll and b/src/mvgam.dll differ diff --git a/tests/testthat/Rplots.pdf b/tests/testthat/Rplots.pdf index fa7aa8e7..16ddf518 100644 Binary files a/tests/testthat/Rplots.pdf and b/tests/testthat/Rplots.pdf differ