bridgesampling icon indicating copy to clipboard operation
bridgesampling copied to clipboard

Error in .local(object, ...) : the model object is not created or not valid

Open zhengxiaoUVic opened this issue 9 months ago • 0 comments

An error still occurs when using saved models. #7

R version 4.4.0 RStudio version 2024.04.1+748 macOS 13.6.7, M2 rstan version 2.32.6 (Stan version 2.32.2) bridgesampling GitHub version 1.1-5

library(rstan)

stan_model_code <- "
data {
  int<lower=0> N; // number of observations
  vector[N] x; // predictor
  vector[N] y; // outcome
}

parameters {
  real alpha; // intercept
  real beta; // slope
  real<lower=0> sigma; // error sd
}

model {
  target += normal_lpdf(y | alpha + beta * x, sigma); // likelihood
}
"

set.seed(123)
N <- 100; x <- rnorm(N)
alpha <- 2; beta <- 3; sigma <- 1
y <- alpha + beta * x + rnorm(N, sd=sigma)
data_list <- list("N"=N, "x"=x, "y"=y)


rstan_options(auto_write=T)
stan_model <- stan_model(model_code=stan_model_code)
fit <- sampling(stan_model, data=data_list, seed=123)


library(bridgesampling)
bridge_sampler(fit)
# Bridge sampling estimate of the log marginal likelihood: -50.49597
# Estimate obtained in 5 iteration(s) via method "normal".


save.image("~/Downloads/reg.RData")
rm(list=ls())
load("~/Downloads/reg.RData")
new_mod <- stan_model(model_code=stan_model_code)
bridge_sampler(fit)
# Error in .local(object, ...) : 
#   the model object is not created or not valid

summary(fit) # this works

zhengxiaoUVic avatar May 17 '24 09:05 zhengxiaoUVic