data {
int P; // # Individus
int Q; // # Items
real logTwNA[P,Q]; // Log responses times
real IndwNA[P,Q]; // NA index (1 if NA, 0 ifelse)
int RwNA[P,Q]; // Responses
}
parameters {
vector[2] paramP[P]; // Parameters of persons (theta,tau)
vector[2] paramQ[Q]; // Parameters of items (betaR,betaT)
// Parameters's distributions of parameters of persons
vector[2] muP;
corr_matrix[2] rhoP;
vector<lower=0,upper=100>[2] sigmaP;
// Parameters's distributions of parameters of items
vector[2] muQ;
corr_matrix[2] rhoQ;
vector<lower=0,upper=100>[2] sigmaQ;
// Sd of log response time of each item
vector<lower=0,upper=100>[Q] sigmaT;
}
transformed parameters {
cov_matrix[2] omegaP;
cov_matrix[2] omegaQ;
// Covariance matrix defined for parameters of persons
for(i in 1:2)
{
omegaP[i,i] <- sigmaP[i]*sigmaP[i]*rhoP[i,i];
}
omegaP[1,2] <- sigmaP[1]*sigmaP[2]*rhoP[1,2];
omegaP[2,1] <- omegaP[1,2];
// Covariance matrix defined for parameters of items
for(i in 1:2)
{
omegaQ[i,i] <- sigmaQ[i]*sigmaQ[i]*rhoQ[i,i];
}
omegaQ[1,2] <- sigmaQ[1]*sigmaQ[2]*rhoQ[1,2];
omegaQ[2,1] <- omegaQ[1,2];
}
model {
// Priors omegaP ~ inversewishart(2,matrix(c(1,0,0,1,ncol=2)), omegaQ ~ inversewishart(2,matrix(c(10,1,1,10,ncol=2))
// muP ~ MVN(c(0,0),omegaP), muQ ~ MVN(c(0,0),omegaQ)
matrix[2,2] omegaQ0;
vector[2] muP0;
vector[2] muQ0;
muP0 <- rep_vector(0,2);
mu00 <- rep_vector(0,2);
omegaQ0 <- diag_matrix(rep_vector(10,2));
omegaQ0[1,2] <- 1;
omegaQ0[2,1] <- 1;
omegaP ~ inv_wishart(2,diag_matrix(rep_vector(1,2)));
omegaQ ~ inv_wishart(2,omegaQ0);
muP ~ multi_normal(muP0,omegaP);
muQ ~ multi_normal(muQ0,omegaQ);
// 1st level
for(p in 1:P)
{
for(q in 1:Q)
{
if(IndwNA[p,q]==0)
{
RwNA[p,q] ~ bernoulli_logit(paramP[p,1]-paramQ[q,1]);
logTwNA[p,q] ~ normal(paramQ[q,2] - paramP[p,2],sigmaT[q]);
}
}
}
// 2nd level
for(p in 1:P)
{
paramP[p] ~ multi_normal(muP,omegaP);
}
for(q in 1:Q)
{
paramQ[q] ~ multi_normal(muQ,omegaQ);
}
}
data {
int P; // # Individus
int Q; // # Items
real logTwNA[P,Q]; // Log responses times
real IndwNA[P,Q]; // NA index (1 if NA, 0 ifelse)
int RwNA[P,Q]; // Responses
}
parameters {
matrix[2,P] zP;
vector[2] muP;
cholesky_factor_corr[2] L_rhoP;
vector<lower=0>[2] sigmaP;
matrix[2,Q] zQ;
vector[2] muQ;
cholesky_factor_corr[2] L_rhoQ;
vector<lower=0>[2] sigmaQ;
vector<lower=0>[Q] sigmaT;
}
transformed parameters{
// to define second level - constraints of parameters (In looking in manuel)
matrix[2,P] paramP; // Parameters of persons (theta,tau)
matrix[2,Q] paramQ; // Parameters of items (betaR,betaT)
paramP <- to_matrix(muP) + (diag_pre_multiply(sigmaP,L_rhoP) * zP);
paramQ <- to_matrix(muQ) + (diag_pre_multiply(sigmaQ,L_rhoQ) * zQ) ;
}
model {
// Priors
sigmaT ~ cauchy(0,2.5);
muP ~ normal(0,1);
sigmaP ~ cauchy(0,2.5);
L_rhoP ~ lkj_corr_cholesky(2);
to_vector(zP) ~ normal(0,1);
muQ ~ normal(0,1);
sigmaQ ~ cauchy(0,2.5);
L_rhoQ ~ lkj_corr_cholesky(2);
to_vector(zQ) ~ normal(0,1);
for(p in 1:P)
{
for(q in 1:Q)
{
if(IndwNA[p,q]==0)
{
RwNA[p,q] ~ bernoulli_logit(paramP[1,p]-paramQ[1,q]);
logTwNA[p,q] ~ normal(paramQ[2,q] - paramP[2,p],sigmaT[q]);
}
}
}
// 2nd level
//paramP ~ multi_normal(muP,(diag_pre_multiply(sigmaP,L_rhoP)*zP));
//paramQ ~ multi_normal(muQ,(diag_pre_multiply(sigmaQ,L_rhoQ)*zQ));
}
So, there is an error when I run this code.
Best regards
data {
int P; // # Individus
int Q; // # Items
real logTwNA[P,Q]; // Log responses times
real IndwNA[P,Q]; // NA index (1 if NA, 0 ifelse)
int RwNA[P,Q]; // Responses
}
parameters {
matrix[2,P] zP;
for(p in 1:P)
{
for(q in 1:Q)
{
if(IndwNA[p,q]==0)
{
RwNA[p,q] ~ bernoulli_logit(paramP[1,p]-paramQ[1,q]);
logTwNA[p,q] ~ normal(paramQ[2,q] - paramP[2,p],sigmaT[q]);
}
}
}
// 2nd level
//paramP ~ multi_normal(muP,(diag_pre_multiply(sigmaP,L_rhoP)*zP));
transformed parameters {
matrix[J,K] beta;
beta <- u * gamma + (diag_pre_multiply(tau,L_Omega) * z)';
}
u is a matrix JxL
gamma is a matrix LxK
(diag_pre_multiply(tau,L_Omega) * z)' is a JxK because of transpose '
You explained it means beta ~ MultiNormal(ul*gamm, Sigma)
I want to write the same.
Theta is matrix 2xN ~ MultiNormal (u,Sigma) (A)
Sigma is matrix 2*2
u is vector with length equal 2
So Theta <- u + (diag_pre_multiply(tau,L_Omega) * z); where
vector[2] tau;
data {
int P; // # Individus
int Q; // # Items
real logTwNA[P,Q]; // Log responses times
real IndwNA[P,Q]; // NA index (1 if NA, 0 ifelse)
int RwNA[P,Q]; // Responses
}
parameters {
matrix[2,P] zP; // using example in page 75
vector[2] muP; // parameters of persons (theta and tau)
cholesky_factor_corr[2] L_rhoP; (Inferior matrix)
vector<lower=0>[2] sigmaP;
matrix[2,Q] zQ;
vector[2] muQ;
cholesky_factor_corr[2] L_rhoQ;
vector<lower=0>[2] sigmaQ;
vector<lower=0>[Q] sigmaT;
}
// I'm using transformed parameters because of explication in page 75 to obtain parmaP ~ MultiNormal(muP,sigmaP)
and paramI ~ MultiNormal(muI,sigmaI).
transformed parameters{
// to define second level - constraints of parameters (In looking in manuel)
matrix[2,P] paramP; // Parameters of persons (theta,tau)
matrix[2,Q] paramQ; // Parameters of items (betaR,betaT)
paramP <- to_matrix(muP) + (diag_pre_multiply(sigmaP,L_rhoP) * zP);
paramQ <- to_matrix(muQ) + (diag_pre_multiply(sigmaQ,L_rhoQ) * zQ) ;
}
model {
// Priors
sigmaT ~ cauchy(0,2.5);
muP ~ normal(0,1);
sigmaP ~ cauchy(0,2.5);
L_rhoP ~ lkj_corr_cholesky(2);
to_vector(zP) ~ normal(0,1);
muQ ~ normal(0,1);
sigmaQ ~ cauchy(0,2.5);
L_rhoQ ~ lkj_corr_cholesky(2);
to_vector(zQ) ~ normal(0,1);
for(p in 1:P)
{
for(q in 1:Q)
{
if(IndwNA[p,q]==0)
{
RwNA[p,q] ~ bernoulli_logit(paramP[1,p]-paramQ[1,q]);
logTwNA[p,q] ~ normal(paramQ[2,q] - paramP[2,p],sigmaT[q]);
}
}
}
}
In R, it is possible to do :
a = 1:2
b = matrix(1:6, ncol=3)
a+b is possible to appear the result. In Rstan, it is not possible because, a is vector and b is matrix.
So how to define a and b to do a+b in Rstan.
Hi Ratan's Users ?Is it normal to my model run after 13h. (Files joined)
Also, all parameters are estimated very good except the matrix L_rhoP defined cholesky_factor_corr[2] L_rhoP