Hi, everyone. I tried to use only non-NA y values (the response has several missing values, but we want to skip those index). I changed the index in Jags code. It can get the MCMC result with no error. However, the estimated parameter is very different than the true parameter I set. In my opinion, the method that directly see the missing response as latent variable is similar to the method that skip the index of missing response(we did right now, only consider observed response in the MCMC). I give the code and data below and I am not sure if I made any mistake here. Thanks!
library(data.table)
library(scales)
library(MCMCvis)
library(nimble)
library(MASS)
library(writexl)
library(readxl)
library(grid)
library(dplyr)
set.seed(1994)
theta=c()
theta[n]=rnorm(1,0,1)
}
alpha=rep(1.5,7)
lamda=c(-1.5,-1,-0.5,0,0.5,1,1.5)
beta_zeta <- as.data.frame(mvrnorm(n=30,
mu=c(-2.197, 4.394),
Sigma=matrix(c(1, -0.8, -0.8, 1), ncol=2)))
parameter_DINA_alpha_lamda<-cbind(alpha,lamda)
parameter_DINA_beta_zeta<-beta_zeta
colnames(parameter_DINA_beta_zeta) <- c('beta','zeta')
parameter_DINA_theta<-as.data.frame(theta)
parameter_DINA_alpha_lamda<-as.data.frame(parameter_DINA_alpha_lamda)
lamdaD<-lamda
thetaD<-theta
betaD<-parameter_DINA_beta_zeta$beta
zetaD<-parameter_DINA_beta_zeta$zeta
gammaD<-alpha
y=fread("D:/try_new/response_ex.csv")
setDF(y)
#good method
#DINA model
numNonNAs <- apply(y, 1, function(x) sum(!
is.na(x)))
nonNAindices <- array(NA, dim(y))
for(i in 1:nrow(y)) nonNAindices[i,1:numNonNAs[i]] <- which(!
is.na(y[i,]))
#model we fit
code=nimbleCode({
for (n in 1:1000) {
for(i in 1:numNonNAs[n]) {
for (k in 1:7) {w[n, nonNAindices[n,i], k] <- pow(attribute[n, k], Q[nonNAindices[n,i], k])}
logit(prob[n, nonNAindices[n,i]]) <- beta[nonNAindices[n,i]] + delta[nonNAindices[n,i]] * prod(w[n, nonNAindices[n,i], 1:7])
y[n, nonNAindices[n,i]] ~ dbern(prob[n, nonNAindices[n,i]]) ## you'll need to complete this
}}
for (n in 1:1000) {
for (k in 1:7) {
logit(att_prob[n, k]) <- gamma[k] * theta[n] - lambda[k]
attribute[n, k] ~ dbern(att_prob[n, k])
}}
for (n in 1:1000) {theta[n]~ dnorm(0,1)}
for (i in 1:30) {
item_parameter[i, 1:2] ~ dmnorm(item_mu[1:2], item_den[1:2, 1:2])
beta[i] <- item_parameter[i, 1]
delta[i] <- item_parameter[i, 2]
}
for (k in 1:7) {
lambda[k] ~ dnorm(0, 1)
gamma[k] ~ T(dnorm(0, 1),0,)
}
item_mu[1] ~ dnorm(0, 1)
item_mu[2] ~ T(dnorm(0, 1),0,)
R[1, 1] <- 1
R[2, 2] <- 1
R[1, 2] <- 0
R[2, 1] <- 0
item_den[1:2, 1:2] ~ dwish(R[1:2, 1:2], 2)
})
data <- list(y=y,Q=Q)
inits <- list(item_den = diag(2))
Consts <- list(numNonNAs = numNonNAs,
nonNAindices=nonNAindices)
monitors=c("gamma","lambda","beta","delta")
model<-nimbleModel(code,data = data,inits=inits,constants = Consts)
cModel_DINA <-compileNimble(model)
conf_DINA <- configureMCMC(model, monitors = monitors)
model_DINAMCMC <- buildMCMC(conf_DINA)
cModel_DINAMCMC <- compileNimble(model_DINAMCMC, project = model)
system.time(samples_DINA<-runMCMC(cModel_DINAMCMC,
nchains=2,niter = 80000, nburnin = 10000))