[Please include Stan program and accompanying data if possible]
Hi everyone,
I am trying to run the following model in STAN, but I keep getting the following error:
SAMPLING FOR MODEL ‘0a1537c56d8525befcb90d37601c1368’ NOW (CHAIN 1).
[1] "Error in sampler$call_sampler(args_list[[i]]) : std::bad_alloc"
error occurred during calling the sampler; sampling not done
Please help.
Thank you.
Below is my code:
Model
library(rstan)
ModelNew <- "
data {
int<lower=1> N; // sample size
int<lower=1> n1;// n1 features in dat1
int<lower=1> n2;// n1 features in dat1
int<lower=1> d; // lower dimens.
int X[N,n1]; // dat1
int Y[N,n2]; // dat2
real<lower=0> scale_global ; // scale for the half -t prior for tau // ( tau0 = scale_global * sigma )
real<lower=1> nu_global; // degrees of freedom for the half-t prior for tau
real<lower=1> nu_local; // degrees of freedom for the half-t priors for lambdas // ( nu_local = 1 corresponds to the horseshoe )
}
parameters {
real<lower=0> sig_te; // sd errors
real<lower=0> sig_lb; // sd errors
matrix[d,N] Z;
matrix[N,n1] teta;
matrix[N,n2] lambd;
cholesky_factor_cov[d,n1] A;
cholesky_factor_cov[d,n2] B;
row_vector[n1] mux;
row_vector[n2] muy;
real<lower=0> taua[d]; // global shrinkage parameter
matrix<lower=0>[d,n1] lamba ; // local shrinkage parameters
real<lower=0> taub[d]; // global shrinkage parameter
matrix<lower=0>[d,n2] lambb; // local shrinkage parameters
}
// transformed param. parts
transformed parameters {
matrix[N,n1] mu_te;
matrix[N,n2] mu_lb;
mu_te = Z’*A;
mu_lb = Z’*B;
}
// Model Part
model{
// half-t prior for tau
taua ~ cauchy(0, scale_global);
taub ~ cauchy(0, scale_global);
sig_lb ~ cauchy(0, 2.5); //
sig_te ~ cauchy(0, 2.5); //
mux ~ normal(0, 10);
muy ~ normal(0, 10);
//lamba ~ cauchy(0, 1);
//lambb ~ cauchy(0, 1);
for(i in 1:d) {
Z[i] ~ normal(0, 1);
lamba[i] ~ cauchy(0, 1);
lambb[i] ~ cauchy(0, 1);
A[i] ~ normal(0, lamba[i] * taua[i]);
B[i] ~ normal(0, lambb[i] * taua[i]);
}
for(i in 1:N)
{
teta[i] ~ normal(mu_te[i] + mux, sig_te);
X[i] ~ poisson_log(teta[i]);
lambd[i] ~ normal(mu_lb[i] + muy, sig_lb);
Y[i] ~ poisson_log(lambd[i]);
}
}
"
compile the model
CompModel <- stan_model(model_code=ModelNew)
#Simulate data
N =n= 30 # sample size
p1 = n1= 10 # Number of features in data set 1
p2 = n2= 10 #Number of features in data set 2
d = 2 # lower dimension - projection dimension
We adopt a factorization of the joint covariance matrix
Only 2 component have high
A = matrix(0,ncol=d,nrow=p1)
A[2:3,] = rnorm(n=4,mean=4)
Only 2 component have high
B = matrix(0,ncol=d,nrow=p2)
B[c(2,4),] = rnorm(n=4,mean=4)
sigte=1
siglb=1
X_fac=rep(1,p1)
Y_fac=rep(1,p2)
Simulate count data
XY_Simulate <- function(n, d, A, B,sige,sige){
Z = matrix(rnorm(nd),nrow=d)
tet = 2 + A%%Z + matrix(rnorm(n=p1n,sd=sige),ncol=n)
lamb = 2+ B%%Z + matrix(rnorm(n=p2*n,sd=sige),ncol=n)
X=apply(exp(tet),c(1,2),rpois, n=1) #rpois(n=1,lambda = 10*exp(tet))
Y=apply(exp(lamb),c(1,2),rpois, n=1)
return(list(“X”=X, “Y”=Y))
}
fit the model to the data
set.seed(12134)
out <- XY_Simulate(n, d, A, B,sige,siglb)
X = out$X
Y = out$Y
cat("\n Make the data sets! \n")
bcca_dat<- list(“X”=t(X),“Y”=t(Y),“n1”=p1,“n2”=p2,“N”=N,“d”=d,“scale_global”=scale_global,“nu_global”=1,“nu_local”=1) ### correct_poi_scale4.stan 29 bet~ inv_gamma(2,1) – shrinking
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
fit = sampling(CompModel, data=bcca_dat, pars=c(“A”,“B”,“sig_te”,“sig_lb”),chains=1,iter=1000,control = list(adapt_delta = 0.99,max_treedepth = 15))