From c8cd4dabc9879381f524e50f27bce0dc08fa8ede Mon Sep 17 00:00:00 2001 From: Eric Ward Date: Tue, 2 Feb 2021 07:39:04 -0800 Subject: [PATCH] vignettes --- R/fit_stan.R | 12 ++++++------ vignettes/dlm.Rmd | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/R/fit_stan.R b/R/fit_stan.R index 90bf3b4..6ffe5bd 100644 --- a/R/fit_stan.R +++ b/R/fit_stan.R @@ -10,7 +10,7 @@ #' @param mcmc_list A list of MCMC control parameters. These include the number of 'iterations' (default = 1000), burn in or warmup (default = 500), chains (default = 3), and thinning (default = 1) #' @param family A named distribution for the observation model, defaults to gaussian #' @param marss A named list containing the following elements for specifying marss models: (states=NULL, obsVariances=NULL, proVariances=NULL, trends=NULL) -#' +#' @param ... Any other arguments passed to [rstan::sampling()]. #' @return an object of class 'rstan' #' @importFrom rstan sampling #' @export @@ -49,13 +49,13 @@ fit_stan <- function(y, x=NA, model_name = NA, data <- NA if(model_name == "regression") { - if(class(x)!="matrix") x = matrix(x,ncol=1) + if(is.matrix(x)==FALSE) x = matrix(x,ncol=1) object <- stanmodels$regression data <- list("N"=length(y),"K"=dim(x)[2],"x"=x,"y"=y,"y_int"=round(y), "family"=family) pars <- c("beta","sigma","pred","log_lik") } if(model_name == "regression_cor") { - if(class(x)!="matrix") x = matrix(x,ncol=1) + if(is.matrix(x)==FALSE) x = matrix(x,ncol=1) object <- stanmodels$regression_cor data <- list("N"=length(y),"K"=dim(x)[2],"x"=x,"y"=y,"y_int"=round(y), "family"=family) pars <- c("beta","sigma","pred","phi","sigma_cor","log_lik") @@ -126,21 +126,21 @@ fit_stan <- function(y, x=NA, model_name = NA, if(is.na(x)) { x <- matrix(0, nrow=length(y), ncol=1) } - if(class(x)!="matrix") x <- matrix(x,ncol=1) + if(is.matrix(x)==FALSE) x <- matrix(x,ncol=1) data <- list("N"=length(y),"K"=dim(x)[2],"x"=x,"y"=y,"y_int"=round(y), "family"=family,"n_pos"=n_pos,"pos_indx"=pos_indx) pars <- c("beta","sigma_obs","sigma_process","pred","intercept","log_lik") } if(model_name == "dlm-slope") { object = stanmodels$dlm_slope # constant estimated intercept, and time varying slopes - if(class(x)!="matrix") x <- matrix(x,ncol=1) + if(is.matrix(x)==FALSE) x <- matrix(x,ncol=1) data <- list("N"=length(y),"K"=dim(x)[2],"x"=x,"y"=y,"y_int"=round(y), "family"=family,"n_pos"=n_pos,"pos_indx"=pos_indx) pars <- c("beta","sigma_obs","sigma_process","pred","log_lik") } if(model_name == "dlm") { object = stanmodels$dlm # this is just a time-varying model with time varying intercept and slopes - if(class(x)!="matrix") x <- matrix(x,ncol=1) + if(is.matrix(x)==FALSE) x <- matrix(x,ncol=1) data <- list("N"=length(y),"K"=dim(x)[2],"x"=x,"y"=y,"y_int"=round(y), "family"=family,"n_pos"=n_pos,"pos_indx"=pos_indx) pars <- c("beta","sigma_obs","sigma_process","pred","log_lik") } diff --git a/vignettes/dlm.Rmd b/vignettes/dlm.Rmd index d4738e1..ae3e389 100644 --- a/vignettes/dlm.Rmd +++ b/vignettes/dlm.Rmd @@ -18,7 +18,7 @@ knitr::opts_chunk$set(echo = TRUE) ```{r install, eval=TRUE, warning=FALSE, message=FALSE, results='hide'} library(rstan) library(atsar) -library(MARSS) + # for optimizing stan on your machine, #rstan_options(auto_write = TRUE) #options(mc.cores = parallel::detectCores()) @@ -30,6 +30,7 @@ mcmc_list = list(n_mcmc = 1000, n_burn = 500, n_chain = 1, n_thin = 1) We'll use the same data in the MARSS manual, from Mark's example in the Columbia River. The data are accessed with ```{r,eval=FALSE} +library(MARSS) data(SalmonSurvCUI) ```