File: mplus-ex9.12.R

package info (click to toggle)
r-cran-openmx 2.21.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,412 kB
  • sloc: cpp: 36,577; ansic: 13,811; fortran: 2,001; sh: 1,440; python: 350; perl: 21; makefile: 5
file content (38 lines) | stat: -rw-r--r-- 1,601 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# MPlus: Two-level growth model for a continuous outcome (three-level analysis)
# https://www.statmodel.com/usersguide/chapter9.shtml

library(OpenMx)

options(width=120)
ex912 <- suppressWarnings(try(read.table("models/nightly/data/ex9.12.dat")))
if (is(ex912, "try-error")) ex912 <- read.table("data/ex9.12.dat")
colnames(ex912) <- c(paste0('y',1:4), 'x', 'w', 'clus')
ex912$clus <- as.integer(ex912$clus)

betweenModel <- mxModel(
    'betweenModel', type='RAM',
    latentVars=c(paste0('y',1:4), 'ib', 'sb', 'w'),
    mxData(ex912[!duplicated(ex912$clus),], 'raw', primaryKey='clus'),
    mxPath('ib', paste0('y',1:4), free=FALSE, values=1),
    mxPath('sb', paste0('y',1:4), free=FALSE, values=0:3),
    mxPath(c('ib','sb'), arrows=2, connect="unique.pairs", values=c(1,0,1)),
    mxPath('one', 'w', free=FALSE, labels="data.w"),
    mxPath('w', c('ib','sb')),
    mxPath('one', c('ib','sb')))

withinModel <- mxModel(
    'withinModel', type='RAM', betweenModel,
    manifestVars=paste0('y',1:4), latentVars=c('iw','sw','x'),
    mxData(ex912, 'raw'),
    mxPath('iw', paste0('y',1:4), free=FALSE, values=1),
    mxPath('sw', paste0('y',1:4), free=FALSE, values=0:3),
    mxPath(paste0('y',1:4), arrows=2, labels="yVar"),
    mxPath(c('iw','sw'), arrows=2, connect="unique.pairs", values=c(1,0,1)),
    mxPath('one', 'x', free=FALSE, labels="data.x"),
    mxPath('x', c('iw','sw')),
    mxPath(paste0('betweenModel.y', 1:4), paste0('y',1:4), free=FALSE, values=1,
	   joinKey="clus"))

withinModel <- mxRun(withinModel)

omxCheckCloseEnough(logLik(withinModel), -6531.52, 1e-2)  # matches Mplus