Learn R Programming

randomLCA (version 1.1-4)

simulate: Simulate

Description

Simulate data from a fitted randomLCA model

Usage

# S3 method for randomLCA
simulate(object, nsim, seed, ...)

Value

A simulated data frame or a list of simulated data frames.

Arguments

object

randomLCA object

nsim

number of data sets to be simulated

seed

random seed

...

additional optional arguments.

Author

Ken Beath

Details

Generates random data from the supplied object.

Examples

Run this code
# \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