Sorry, but this is what I got so far:
Stan Model
data {
int<lower=1> ncol;
int<lower=1> nrow;
vector[ncol] yH;
int x[nrow];
matrix[nrow, ncol] A;
vector[ncol] log_sigmay;
vector[nrow] epsilon;
vector[ncol] log_lambda;
vector[nrow] k;
}
parameters {
vector[ncol] targ;
}
model {
targ += log_sum_exp(log_weights + log_dens);
}
Fitting MCMC in R
library(rstan)
library(MASS)
library(truncnorm)
library(bayesplot)
library(bridgesampling)
library(ggplot2)
library(MCMCpack)
library(bayesplot)
library(clusterGeneration)
require(MASS)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
nrow = 16;
ncol = 24;
iterations = 1500;
A ā matrix(c(1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, #x_A^1 - Done
0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0, #x_A^2 - Done
0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, #x_B^1 - Done
1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0, #x_B^2 - Done
0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, #x_C^1 - Done
0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0, #x_C^2 - Done
0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0, #x_D^1 - Done
0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, #x_D^2 - Done
0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0, #x_E^1 - Done
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0, #x_E^2 - Done
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0, #x_F^1 - Done
0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0, #x_F^2 - Done
0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0, #x_G^2 - Done
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1, #x_G^1 - Done
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0, #x_H^1 - Done
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1), #x_H^2 - Done
nrow, ncol, byrow= TRUE);
for (i in 1:1000) # first for-loop
{
yH ā as.integer(rpois(ncol, 100));
x ā as.integer(A%%yH + rtruncnorm(nrow, a = 0, b = Inf, mean = 10, sd = 5))
sigmay ā array(12, ncol);
epsilon ā array(rtruncnorm(nrow, a = 3, b = 8, mean = 6, sd = 3), nrow);
lambda ā runif(ncol, 20, 100)
log_lambda ā log(lambda)
k ā A%%yH
log_sigmay ā log(sigmay)
stan_data = list(nrow = nrow, ncol = ncol, yH = yH, x = x, k = k, A = A, log_sigmay = log_sigmay, log_lambda = log_lambda, epsilon = epsilon);
stanmodel1 <- stan_model(file = "Poisson_Normal.stan", model_name = "stanmodel1");
}
stanfit ā sampling(stanmodel1, data = stan_data, iter=iterations, warmup = 1000, chains = 4, cores = 2, refresh = 250);
I keep getting this error (the one you wrote also informed this error). Could you please help me fix this model so that it could run? Although I am not even sure if it would do what i wantā¦
targ = targ + log_sum_exp(log_weights + log_dens);
PARSER EXPECTED: <expression assignable to left-hand side>
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model 'stanmodel1' due to the above error.
Another error tht I got, after creating a transformed parameter block like following:
transformed parameters{
vector[ncol] log_weights;
vector[ncol] log_dens;
for (i in 0:ncol){
log_weights[i] = poisson_log_lpmf(i|log_lambda[i]);
log_dens[i] = normal_log_lpdf(targ[i]|(log(k)+epsilon)[i], log_sigmay[i]);
}
}
is on the normal_log_lpdf()
line:
No matches for:
normal_log_lpdf(real, real, real)
Function normal_log_lpdf not found.
I am so sorry, but I really have no idea how to fix this issue.