# \donttest{
myocardial.lca1 <- randomLCA(myocardial[, 1:4], freq = myocardial$freq, nclass = 1, cores = 1)
myocardial.lca2 <- randomLCA(myocardial[, 1:4], freq = myocardial$freq, cores = 1)
# calculate observed lrt
obslrt <- 2*(logLik(myocardial.lca2)-logLik(myocardial.lca1))
print(obslrt)
nsims <- 999
# generate the simulations
thesims <- simulate(myocardial.lca1, nsims)
# for each simulation determin lrt
simlrt <- rep(NA, nsims)
for (isim in 1:nsims) {
submodel <- refit(myocardial.lca1, newpatterns = thesims[[isim]])
fullmodel <- refit(myocardial.lca2, newpatterns = thesims[[isim]])
simlrt[isim] <- 2*(logLik(fullmodel)-logLik(submodel))
print(c(isim, simlrt[isim]))
}
# calculate p value as proportion of simulated lrt greater than observed,
# corrected by adding one to numerator and denominator
print((sum(simlrt >= obslrt)+1)/(nsims+1))# }
Run the code above in your browser using DataLab