ifa: Cai 2010 two-tier
[openmx:openmx.git] / models / failing / irt-bifactor.R
1 # echo 0 > /proc/self/coredump_filter  # normally 023
2 # R --vanilla --no-save -f models/failing/bock-aitkin-1981.R
3 # R -d gdb --vanilla --no-save -f models/failing/bock-aitkin-1981.R
4
5 #options(error = browser)
6 require(OpenMx)
7 require(rpf)
8
9 #set.seed(5)
10
11 numItems <- 5
12 numPersons <- 1000
13 maxDim <- 2
14
15 items <- vector("list", numItems)
16 correct <- vector("list", numItems)
17 for (ix in 1:numItems) {
18         items[[ix]] <- rpf.drm(dimensions=maxDim)
19         correct[[ix]] <- rpf.rparam(items[[ix]])
20         correct[[ix]][[4]] <- 0   # no guessing, for now
21 }
22 correct.mat <- simplify2array(correct)
23
24 maxParam <- max(vapply(items, function(i) i@numParam, 0))
25 maxOutcomes <- max(vapply(items, function(i) i@numOutcomes, 0))
26
27 design <- matrix(c(rep(1,numItems),
28                    2,2,2,3,3), byrow=TRUE, nrow=2)
29
30 data <- rpf.sample(numPersons, items, correct, design)
31
32 spec <- mxMatrix(name="ItemSpec", nrow=3, ncol=numItems,
33          values=c(rep(mxLookupIRTItemModelID("drm"), numItems),
34                  rep(2, numItems),
35                  rep(2, numItems)),
36          free=FALSE, byrow=TRUE)
37
38 design <- mxMatrix(name="Design", nrow=maxDim, ncol=numItems,
39                    values=design)
40
41 ip.mat <- mxMatrix(name="ItemParam", nrow=maxParam, ncol=numItems,
42                    values=c(
43                      rep(1.414,numItems),
44                      rep(1,numItems),
45                      rep(0,numItems),
46                      rep(0,numItems)),
47          free=c(rep(TRUE, numItems*3),
48                  rep(FALSE, numItems*1)),
49          byrow=TRUE)
50
51 #ip.mat@values[2,1] <- correct.mat[2,1]
52 #ip.mat@free[2,1] <- FALSE
53
54 m1 <- mxModel(model="bifactor",
55           spec, design,
56           ip.mat,
57           mxMatrix(name="A", values=c(1,1,0,0), nrow=1, ncol=maxParam, free=FALSE),
58           mxAlgebra(name="prior",
59                     sum(dlnorm(omxSelectRows(ItemParam, A), 0, .5, 1))),
60           mxData(observed=data, type="raw"),
61           mxExpectationBA81(
62              ItemSpec="ItemSpec",
63              Design="Design",
64              ItemParam="ItemParam",
65              ItemPrior="prior"),
66           mxFitFunctionBA81()
67 )
68
69 m1 <- mxOption(m1, "Calculate Hessian", "No")
70 m1 <- mxOption(m1, "Standard Errors", "No")
71
72 m1 <- mxRun(m1, silent=TRUE)
73 print(correct.mat)
74 print(m1@matrices$ItemParam@values)
75 print(cor(c(m1@matrices$ItemParam@values), c(correct.mat)))