ifa: Basic EAP working, regression tests checked & updated
[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           mxData(observed=data, type="raw"),
58           mxExpectationBA81(
59              ItemSpec="ItemSpec",
60              Design="Design",
61              ItemParam="ItemParam"),
62           mxFitFunctionBA81()
63 )
64
65 m1 <- mxOption(m1, "Analytic Gradients", 'no')
66 if (0) {
67         m1 <- mxOption(m1, "Analytic Gradients", 'yes')
68         m1 <- mxOption(m1, "Verify level", '-1')
69 }
70 m1 <- mxOption(m1, "Calculate Hessian", "No")
71 m1 <- mxOption(m1, "Standard Errors", "No")
72
73 m1 <- mxRun(m1, silent=TRUE)
74 print(correct.mat)
75 print(m1@matrices$ItemParam@values)
76 got <- cor(c(m1@matrices$ItemParam@values), c(correct.mat))
77 omxCheckCloseEnough(got, .942, .01) # caution, not converged yet