Here is the program.
set.seed(123)
AF <- readRDS("AF_clean.rds")
N= nrow(AF)
AF[] <- lapply(AF, function(x) if(is.numeric(x)) as.integer(x) else x)
#################################################################################
x2<-AF$x2
x3<-AF$x3
x4<-AF$x4
x5<-AF$x5
eta1<-AF$eta1
eta2<-AF$eta2
eta3<-AF$eta3
eta4<-AF$eta4
eta5<-AF$eta5
eta6<-AF$eta6
eta7<-AF$eta7
sim_ordinal <- function(n, probs) {
cumprobs <- cumsum(probs)
u <- runif(n)
sapply(u, function(x) sum(x > cumprobs) + 1)
}
eta8 <- as.integer(sim_ordinal(N, c(0.35, 0.35, 0.3)))
group <- rbinom(N, 1, 0.5)
x6 <- as.numeric(ifelse(group == 0,
rlnorm(N, meanlog = 4.24, sdlog = 0.32),
rlnorm(N, meanlog = 4.77, sdlog = 0.18)))
logit <- -2 + 0.5*AF$eta1 + 0.3*eta8 + 0.4*AF$eta7
xi <- rbinom(N, 1, plogis(logit))
stan_data_AF <- list(
N = nrow(AF),
x2 = x2,
x3 = x3,
x4 = x4,
x5 = x5,
x6 = x6,
xi = xi,
eta1 = eta1,
eta2 = eta2,
eta3 = eta3,
eta4 = eta4,
eta5 = eta5,
eta6 = eta6,
eta7 = eta7,
eta8 = eta8
)
stan_code_AF <- "
data {
int<lower=0> N; // number of observations
int<lower=1,upper=5> eta1[N];
int<lower=1,upper=5> eta2[N];
int<lower=1,upper=3> eta3[N];
int<lower=1,upper=5> eta4[N];
int<lower=1,upper=5> eta5[N];
int<lower=1,upper=5> eta6[N];
int<lower=1,upper=5> eta7[N];
int<lower=1,upper=3> eta8[N];
int<lower=1,upper=1> x2[N];
int<lower=0> x3[N];
int<lower=0> x4[N];
int<lower=0,upper=1> x5[N];
real<lower=0> x6[N];
int<lower=0,upper=1> xi[N];
}
parameters {
real beta_eta1_eta2;
real beta_eta2_eta3;
real beta_eta3_eta4;
real beta_eta1_xi;
real beta_eta1_eta4;
real beta_x2_eta2;
real beta_x3_eta4;
real beta_eta5_eta3;
real beta_eta6_eta3;
real beta_eta3_eta8;
real beta_x6_eta8;
real beta_x4_eta8;
real beta_x5_eta8;
real beta_eta8_xi;
real beta_eta7_xi;
real alpha_eta2;
real alpha_eta3;
real alpha_eta4;
real alpha_eta8;
real alpha_xi;
real<lower=0> sigma_eta2;
real<lower=0> sigma_eta3;
real<lower=0> sigma_eta4;
real<lower=0> sigma_eta8;
}
model {
beta_eta1_eta2 ~ normal(0, 1);
beta_eta2_eta3 ~ normal(0, 1);
beta_eta3_eta4 ~ normal(0, 1);
beta_eta1_xi ~ normal(0, 1);
beta_eta1_eta4 ~ normal(0, 1);
beta_x2_eta2 ~ normal(0, 1);
beta_x3_eta4 ~ normal(0, 1);
beta_eta5_eta3 ~ normal(0, 1);
beta_eta6_eta3 ~ normal(0, 1);
beta_eta3_eta8 ~ normal(0, 1);
beta_x6_eta8 ~ normal(0, 1);
beta_x4_eta8 ~ normal(0, 1);
beta_x5_eta8 ~ normal(0, 1);
beta_eta8_xi ~ normal(0, 1);
beta_eta7_xi ~ normal(0, 1);
alpha_eta2 ~ normal(0, 2);
alpha_eta3 ~ normal(0, 2);
alpha_eta4 ~ normal(0, 2);
alpha_eta8 ~ normal(0, 1);
alpha_xi ~ normal(logit(0.29), 2.5);
sigma_eta2 ~ cauchy(0, 2.5);
sigma_eta3 ~ cauchy(0, 2.5);
sigma_eta4 ~ cauchy(0, 2.5);
sigma_eta8 ~ cauchy(0, 2.5);
// Likelihood
for (n in 1:N) {
// Structural equations
real mu_eta2;
real mu_eta3;
real mu_eta4;
real mu_eta8;
real logit_xi;
mu_eta2 = alpha_eta2 + beta_eta1_eta2 * eta1[n] + beta_x2_eta2 * x2[n];
mu_eta3 = alpha_eta3 + beta_eta2_eta3 * eta2[n] + beta_eta5_eta3 * eta5[n] +
beta_eta6_eta3 * eta6[n];
mu_eta4 = alpha_eta4 + beta_eta3_eta4 * eta3[n] + beta_eta1_eta4 * eta1[n] +
beta_x3_eta4 * x3[n];
mu_eta8 = alpha_eta8 + beta_eta3_eta8 * eta3[n] + beta_x6_eta8 * x6[n] +
beta_x4_eta8 * x4[n] + beta_x5_eta8 * x5[n];
// Binary outcome
logit_xi = alpha_xi + beta_eta1_xi * eta1[n] + beta_eta8_xi * eta8[n] +
beta_eta7_xi * eta7[n];
// Likelihood contributions
eta2[n] ~ normal(mu_eta2, sigma_eta2);
eta3[n] ~ normal(mu_eta3, sigma_eta3);
eta4[n] ~ normal(mu_eta4, sigma_eta4);
eta8[n] ~ normal(mu_eta8, sigma_eta8);
xi[n] ~ bernoulli_logit(logit_xi);
}
}
generated quantities {
vector[N] log_lik;
for (n in 1:N) {
real mu_eta2 = alpha_eta2 + beta_eta1_eta2 * eta1[n] + beta_x2_eta2 * x2[n];
real mu_eta3 = alpha_eta3 + beta_eta2_eta3 * eta2[n] + beta_eta5_eta3 * eta5[n] +
beta_eta6_eta3 * eta6[n];
real mu_eta4 = alpha_eta4 + beta_eta3_eta4 * eta3[n] + beta_eta1_eta4 * eta1[n] +
beta_x3_eta4 * x3[n];
real mu_eta8 = alpha_eta8 + beta_eta3_eta8 * eta3[n] + beta_x6_eta8 * x6[n] +
beta_x4_eta8 * x4[n] + beta_x5_eta8 * x5[n];
real logit_xi = alpha_xi + beta_eta1_xi * eta1[n] + beta_eta8_xi * eta8[n] +
beta_eta7_xi * eta7[n];
log_lik[n] = normal_lpdf(eta2[n] | mu_eta2, sigma_eta2) +
normal_lpdf(eta3[n] | mu_eta3, sigma_eta3) +
normal_lpdf(eta4[n] | mu_eta4, sigma_eta4) +
normal_lpdf(eta8[n] | mu_eta8, sigma_eta8) +
bernoulli_logit_lpmf(xi[n] | logit_xi);
}
}
"
fit_AF <- stan(model_code = stan_code_AF, data = stan_data_AF,
+ chains = 4,
+ iter = 5000, # Increased from 2000
+ warmup = 1500, # Increased from 500
+ control = list(adapt_delta = 0.99))
0,248,Js_of_ocaml__Js.Error,18,TypeError: not a function
Error in model_cppcode$errors : $ operator is invalid for atomic vectors
> class(model_cppcode)
Error: object 'model_cppcode' not found
[edit: escaped code and run]