I am new to using Stan and I am trying to model a simple ancestry problem but keep encountering an initialisation error. My model and code is provided below.
data {
int<lower = 1> K;
int<lower = 1> N;
matrix[K,N] beta;
vector[N] x;
}
parameters {
vector[K] f;
}
transformed parameters {
vector[N] alpha;
for(i in 1:N){
alpha[i]=0;
for(j in 1:K){
alpha[i] = alpha[i] + f[j]*beta[j,i];
}
}
}
model {
f~dirichlet(rep_vector(1, K));
x~dirichlet(alpha);
}
The above runs fine with no errors. However, below produces the error :
[1] “Error in sampler$call_sampler(args_list[[i]]) : Initialization failed.”
[1] “error occurred during calling the sampler; sampling not done”
# Firstly, prepare the data for stan
beta = matrix(
c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0.5, 0.5, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0.3, 0.4, 0.3),nrow=3,ncol=10,byrow=T)+0.05
beta=beta/rowSums(beta)
# This is the actual frequencies of the populations
ftrue=c(0.5,0.5,0)
# This function produces a sample child, based on the parent pop profiles
# and the true frequencies of the populations, sampled from multinomial
# distribution.
make_population_data<-function(beta,ftrue,N=100){
xi=rmultinom(1,N,ftrue)[,1]
x=rowSums(do.call("cbind",sapply(1:length(xi),function(i){
rmultinom(xi[i],1,beta[i,])
})))
list(K=dim(beta)[1],N=dim(beta)[2],beta=beta,x=x)
}
# Here we assign the output of the above function to the pop data we
# will be feeding into our stan model.
population_data=make_population_data(beta,ftrue)
# Next, we need to call stan function to draw posterior
# samples
#my_file <- file.path("C:", "Users", "Joach", "Desktop", "my_file.csv")
#"C:\Users\Team Knowhow\Documents\YEAR 4\Project\STAN models\01-script.R"
# use forward slashes in file path otherwise leads to an error
library(rstan)
fit1 <- stan(
file = "C:/Users/Team Knowhow/Documents/YEAR 4/Project/STAN models/01-model.stan", # Stan program
data = population_data, # named list of data
chains = 4, # number of Markov chains
warmup = 1000, # number of warmup iterations per chain
iter = 2000, # total number of iterations per chain
cores = 1, # number of cores (could use one per chain)
refresh = 0 # no progress shown
)
Please advise.