I am running a Bayesian latent variable model in Stan
. Here is my code for the model:
# run the Stan model
mod.dyn <- stan(file="DynamicLVMmin.stan",
data=stan.data, seed=570175513, thin = 5,
iter=10000)
The model runs with no issue. I then go to save the mod.dyn
as follows:
# save stan fit
saveRDS(mod.dyn, "dynamic_fit.rds")
Now I am reading dynamic_fit.rds
. I want to assess model convergence using the Gelman-Rubin R-hat diagnostic. Here is the code:
readRDS("dynamic_fit.rds")
params <- c("innov", "c_n_4_v2exdfdshs", "c_n_5_v2exdfvths", "c_n_3_v2exdfpphs", "c_n_5_v2lginvstp",
"c_n_3_v2lgoppart", "c_n_2_v2lgfunds", "c_n_4_v2lgcomslo", "c_n_2_v2lgintbup", "c_n_2_v2lgintblo",
paste("beta[", 1:9, "]", sep=""))
params.all <- names(mod.dyn)
However, I receive the following error:
Error: object 'mod.dyn' not found
Execution halted
What am I missing here? Shouldn't mod.dyn
have been saved when I used saveRDS(mod.dyn, "dynamic_fit.rds")
. Any feedback would be appreciated.
readRDS
saves a single object without a name. So you want to use
mod.dyn <- readRDS("dynamic_fit.rds")
to capture that value after you read it in. This is different than save()/load()
which store one or more variables with their name+value in a serialized format. If you did
save(mod.dyn, file="dynamic_fit.rdata")
then
load("dynamic_fit.rdata")
the object mod.dyn
would be loaded back into your workspace.