Hi all,
I am fitting an IRT model with three binary variables (K=3).
The Rcodes to generate the data is as follows:
# Rcodes to generate and fit IRT models
# NORMAL distribution
rm(list = ls())
# load libraries
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
library(xlsx)
#####################################################
### CASE 1: BINARY ###
#####################################################
#####################################################
### Generating data ###
#####################################################
# Number of items
K=3
# Number of subjects
N=1000
# Parameters
# thres and slope
thres=runif(n=K, min=-1, max=1)
lambda=runif(n=K, min=0.4, max=2)
para=c(thres, lambda)
# latent traits
xi=rnorm(n=N, mean=0, sd=1)
# matrix to store probabilities
prob=matrix(data=NA, nrow = N, ncol = K)
# matrix to store the outcome
Y=matrix(data=NA, nrow = N, ncol = K)
for (i in 1 : N){
for (k in 1: K){
prob[i, k]=pnorm(thres[k] + lambda[k] * xi[i])
Y[i, k]=ifelse(runif(1)<prob[i, k], 1, 0)
}
}
write.table(x=Y, file = 'outcome.txt', quote = F, sep = ' ',
row.names = F, col.names = T)
#####################################################
### Fitting models ###
#####################################################
#################################
### model1a ###
#################################
data1a = list(N=N, K=K, Y=Y)
model1a = stan(file="model1a.stan", data = data1a,
iter=5000, chains = 4,
pars = c('thres', 'lambda',
'mu_thres', 'sigma_thres', 'sigma_lambda'))
# trace plots
pdf('thres1a.pdf')
stan_trace(model1a, pars = 'thres', inc_warmup = F)
dev.off()
pdf('lambda1a.pdf')
stan_trace(model1a, pars = 'lambda', inc_warmup = F)
dev.off()
pdf('mu_thres1a.pdf')
stan_trace(model1a, pars = 'mu_thres', inc_warmup = F)
dev.off()
pdf('sigma_thres1a.pdf')
stan_trace(model1a, pars = 'sigma_thres', inc_warmup = F)
dev.off()
pdf('sigma_lambda1a.pdf')
stan_trace(model1a, pars = 'sigma_lambda', inc_warmup = F)
dev.off()
# summary
summ1a=summary(model1a)$summary
write.xlsx(summ1a, file = 'summ1a.xlsx')
and below is stan model
data {
int N;
int K;
int Y[N, K];
}
parameters {
vector[K] lambda;
real sigma_lambda;
vector[K] thres;
real mu_thres;
real sigma_thres;
vector[N] xi;
}
model{
lambda ~ lognormal(0, sigma_lambda);
sigma_lambda ~ cauchy(0, 5);
thres ~ normal(mu_thres, sigma_thres);
mu_thres ~ cauchy(0, 5);
sigma_thres ~ cauchy(0, 5);
xi ~ normal(0, 1);
for (i in 1: N){
for (k in 1 : K){
Y[i, k] ~ bernoulli(Phi(thres[k] + lambda[k] * xi[i]));
}
}
}
The trace plot does not show convergence for lambda2
It seems that lambda[2] is not identified from the model and data.
Do you have any idea? Thank you for your help!
Tran.
model1a.stan (552 Bytes)
No missing.R (2.2 KB)
outcome.txt (6.8 KB)