data(speed)
# 2-state model on the RTs of the speed data with random
# starting values for the transition pars (without those EM does not get off the ground)
set.seed(1)
mod <- depmix(rt~1,data=speed,nstates=2,trstart=runif(4))
# fit the model
mod1 <- fit(mod)
mod1 # to see the logLik and optimization information
# to see the parameters
summary(mod1)
data(balance)
# four binary items on the balance scale task
# now fit some latent class models
trstart=c(1,0,0,1) # as this is a latent class model, the transition are not optimized
instart=c(0.5,0.5)
set.seed(1)
respstart=runif(16)
# note that ntimes argument is used to make this a mixture model
mod <- depmix(list(d1~1,d2~1,d3~1,d4~1), data=balance, nstates=2,
family=list(multinomial(),multinomial(),multinomial(),multinomial()),
respstart=respstart,trstart=trstart,instart=instart,
ntimes=rep(1,nrow(balance)))
mod1 <- fit(mod)
# add age as covariate on class membership by using the prior argument
trstart=c(1,0,0,1) # as this is a latent class model, the transition are not optimized
instart=c(0.5,0.5,0,0) # we need the initial probs and the coefficients of age
set.seed(2)
respstart=c(runif(16))
trstart=c(1,0,0,1)
mod2 <- depmix(list(d1~1,d2~1,d3~1,d4~1), data=balance, nstates=2,
family=list(multinomial(),multinomial(),multinomial(),multinomial()),
trstart=trstart, instart=instart, respstart=respstart,
ntimes=rep(1,nrow(balance)), prior=~age, initdata=balance)
mod3 <- fit(mod2)
# check the likelihood ratio; adding age significantly improves the goodness-of-fit
llratio(mod3,mod1)
Run the code above in your browser using DataLab