Dear Stan folks,
I have errors such that the fitted Stan object is shown as <Object with null pointer> in the Environment tab after I update the rstan
and R
. The subsequent problem is that if I use other packages that exploit the Stan object fitted from the stan() function, the R session is aborted, immediately. Therefore, I would like to get some help to solve this issue.
As an example, I used the bridgesampling
package that uses the Stan fit object. The R code to first fit the Stan object comes below.
library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
library(bridgesampling)
data("turtles")
### H0 (model without random intercepts) ###
H0_code <-
"data {
int<lower = 1> N;
int<lower = 0, upper = 1> y[N];
real<lower = 0> x[N];
}
parameters {
real alpha0_raw;
real alpha1_raw;
}
transformed parameters {
real alpha0 = sqrt(10.0) * alpha0_raw;
real alpha1 = sqrt(10.0) * alpha1_raw;
}
model {
// priors
target += normal_lpdf(alpha0_raw | 0, 1);
target += normal_lpdf(alpha1_raw | 0, 1);
// likelihood
for (i in 1:N) {
target += bernoulli_lpmf(y[i] | Phi(alpha0 + alpha1 * x[i]));
}
}"
### H1 (model with random intercepts) ###
H1_code <-
"data {
int<lower = 1> N;
int<lower = 0, upper = 1> y[N];
real<lower = 0> x[N];
int<lower = 1> C;
int<lower = 1, upper = C> clutch[N];
}
parameters {
real alpha0_raw;
real alpha1_raw;
vector[C] b_raw;
real<lower = 0> sigma2;
}
transformed parameters {
vector[C] b;
real<lower = 0> sigma = sqrt(sigma2);
real alpha0 = sqrt(10.0) * alpha0_raw;
real alpha1 = sqrt(10.0) * alpha1_raw;
b = sigma * b_raw;
}
model {
// priors
target += - 2 * log(1 + sigma2); // p(sigma2) = 1 / (1 + sigma2) ^ 2
target += normal_lpdf(alpha0_raw | 0, 1);
target += normal_lpdf(alpha1_raw | 0, 1);
// random effects
target += normal_lpdf(b_raw | 0, 1);
// likelihood
for (i in 1:N) {
target += bernoulli_lpmf(y[i] | Phi(alpha0 + alpha1 * x[i] + b[clutch[i]]));
}
}"
set.seed(1)
### fit models ###
stanfit_H0 <- stan(model_code = H0_code,
data = list(y = turtles$y, x = turtles$x, N = nrow(turtles)),
iter = 15500, warmup = 500, chains = 4, seed = 1)
stanfit_H1 <- stan(model_code = H1_code,
data = list(y = turtles$y, x = turtles$x, N = nrow(turtles),
C = max(turtles$clutch), clutch = turtles$clutch),
iter = 15500, warmup = 500, chains = 4, seed = 1)
If I finish this part, there appear two Stan fit objects in the Environment tab. For me, they are shown as <Object with null pointer>. I then used the bridge_sampler() function to calculate the log marginal likelihoods, which uses the Stan fit object.
set.seed(1)
### compute (log) marginal likelihoods ###
bridge_H0 <- bridge_sampler(stanfit_H0)
bridge_H1 <- bridge_sampler(stanfit_H1)
Immediately, the R session is aborted. Therefore, could anyone help me get out of this error swamp? I attach the session information of my machine.
sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
Matrix products: default
locale:
[1] LC_COLLATE=Korean_Korea.949 LC_CTYPE=Korean_Korea.949
[3] LC_MONETARY=Korean_Korea.949 LC_NUMERIC=C
[5] LC_TIME=Korean_Korea.949
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
loaded via a namespace (and not attached):
[1] compiler_4.0.3 parallel_4.0.3 tools_4.0.3
Thanks for your time and consideration!