File: MultigroupWLS.R

package info (click to toggle)
r-cran-openmx 2.21.13%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,716 kB
  • sloc: cpp: 36,559; ansic: 13,821; fortran: 2,001; sh: 1,440; python: 350; perl: 21; makefile: 11
file content (75 lines) | stat: -rw-r--r-- 2,276 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
library(OpenMx)

if (mxOption(NULL, "Default optimizer") == 'NPSOL') stop('SKIP')

nContPerFactor <- 4
nOrdPerFactor <- 1
nVarPerFactor <- nContPerFactor + nOrdPerFactor
nFact <- 1
latents <- rawToChar(as.raw(as.integer(charToRaw("A")) + 1:nFact - 1), multiple=T)
manifests <- apply(expand.grid(prefix=latents, 1:(nContPerFactor + nOrdPerFactor)),
                   1, paste, collapse="")
ordinals <- apply(expand.grid(prefix=latents, 1:nOrdPerFactor),
                  1, paste, collapse="")
nGroups <- 3L

mkGroup <- function(name) {
  big <- mxModel(
    name, type="RAM",
    latentVars = latents,
    manifestVars = manifests,
    mxMatrix(nrow=2, ncol=nOrdPerFactor*nFact, values=1:2/3,
             free=FALSE, dimnames=list(NULL, ordinals), name="thresholds"))
  
  for (fx in latents) {
    big <- mxModel(
      big,
      mxPath("one", paste0(fx, 1:nVarPerFactor), values=rnorm(nVarPerFactor, sd = .2)),
      mxPath(paste0(fx, 1:nVarPerFactor), arrows=2, values=1, labels="err"),
      mxPath(fx, paste0(fx, 1:nVarPerFactor),
             values=runif(nVarPerFactor, .25,.5),
             labels=paste0("l",1:nVarPerFactor)),
      mxPath(fx, arrows=2, free=FALSE, values=1))
    if (fx == 'A') next
    big <- mxModel(
      big,
      mxPath('A', fx, values=runif(1,.25,.5)))
  }
  
  big$expectation$thresholds <- 'thresholds'
  big
}

container <- mxModel("mg", mxFitFunctionMultigroup(paste0('g', 1:nGroups)))
for (gx in 1:nGroups) {
  container <- mxModel(container,
                       mkGroup(paste0("g",gx)))
}
container <- omxAssignFirstParameters(container)
trueCoef <- coef(container)

set.seed(123)
container <- mxGenerateData(container, nrows=300, returnModel = TRUE)

ml <- mxModel(name="ml", container)
ml <- mxRun(ml)

omxCheckCloseEnough(max(abs(coef(ml) - trueCoef)), 0, .21)

r1 <- mxRefModels(ml)
omxCheckEquals(length(coef(r1$Saturated)), 56)
r2 <- mxRefModels(ml, equateThresholds=FALSE)
omxCheckEquals(length(coef(r2$Saturated)), 60)

wls <- mxModel(name="wls", container)

for (gx in 1:nGroups) {
  grp <- wls[[paste0("g",gx)]]
  grp <- mxModel(grp, mxFitFunctionWLS())
  wls <- mxModel(wls, grp)
}
wls <- mxRun(wls)

omxCheckCloseEnough(max(abs(coef(wls) - trueCoef)), 0, .25)

omxCheckCloseEnough(cor(coef(ml), coef(wls)), 1, .003)