Update regression tests
[openmx:openmx.git] / models / failing / irt-2d.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 #library(mvtnorm)
9
10 set.seed(7)
11
12 numItems <- 5
13 numPeople <- 500
14 maxDim <- 2
15
16 items <- vector("list", numItems)
17 correct <- vector("list", numItems)
18 for (ix in 1:numItems) {
19         items[[ix]] <- rpf.drm(factors=min(ix,maxDim),
20                                multidimensional=TRUE)
21         correct[[ix]] <- rpf.rparam(items[[ix]])
22         if (ix>1) correct[[ix]][[4]] <- 0   # no guessing, for now
23 }
24 correct[[1]][[5]] <- 1   # make all vectors the same length
25 correct.mat <- simplify2array(correct)
26 correct.mat[5,] <- 1
27 correct.mat[4,1] <- 1
28 correct.mat[3,1] <- 0
29
30 maxParam <- max(vapply(items, function(i) rpf.numParam(i), 0))
31 maxOutcomes <- max(vapply(items, function(i) i@outcomes, 0))
32
33 design <- matrix(c(1, 1,1,1,2,
34                   NA,2,2,2,1), byrow=TRUE, nrow=2)
35
36 ability <- array(rnorm(numPeople * 2), dim=c(2, numPeople))
37 #cov <- matrix(c(1, .68, .68, 1), nrow=2)
38 #ability <- rmvnorm(numPeople, sigma=cov)
39 data <- rpf.sample(ability, items, correct.mat, design)
40 write.csv(data, file="2d-new.csv")
41
42 spec <- mxMatrix(name="ItemSpec", nrow=3, ncol=numItems,
43          values=sapply(items, function(m) slot(m,'spec')),
44          free=FALSE, byrow=TRUE)
45
46 design <- mxMatrix(name="Design", nrow=maxDim, ncol=numItems,
47                   values=design)
48
49 ip.mat <- mxMatrix(name="ItemParam", nrow=maxParam, ncol=numItems,
50                    values=c(1, 1.4, 0, 0, 1),
51                   lbound=c(1e-6, -1e6, 0,  0, 0,
52                     rep(c(1e-6, 1e-6, -1e6, 0, 0), 4)),
53          free=c(TRUE, TRUE, FALSE, FALSE, FALSE,
54           rep(c(TRUE, TRUE, TRUE, FALSE, FALSE), 4)))
55 ip.mat@values[4,1] <- 1
56 ip.mat@free.group <- "param"
57
58 eip.mat <- mxMatrix(name="EItemParam", nrow=maxParam, ncol=numItems,
59                    values=ip.mat@values, free=ip.mat@free)
60
61 m.mat <- mxMatrix(name="mean", nrow=1, ncol=2, values=0, free=FALSE)
62 cov.mat <- mxMatrix(name="cov", nrow=2, ncol=2, values=diag(2), free=FALSE)
63
64 m1 <- mxModel(model="2dim",
65           spec, design,
66           ip.mat, m.mat, cov.mat, eip.mat,
67           mxData(observed=data, type="raw"),
68           mxExpectationBA81(mean="mean", cov="cov",
69              ItemSpec="ItemSpec",
70                             EItemParam="EItemParam",
71              Design="Design",
72             qpoints=29,
73             scores="full"),
74           mxFitFunctionBA81(ItemParam="ItemParam"),
75               mxComputeIterate(steps=list(
76                 mxComputeAssign(from="ItemParam", to="EItemParam"),
77                 mxComputeOnce(expectation='expectation', context='E'),
78                 mxComputeGradientDescent(free.group='param'),
79                 mxComputeOnce(expectation='expectation', context='M'),
80                 mxComputeOnce(fitfunction='fitfunction'))))
81
82         m1 <- mxOption(m1, "Analytic Gradients", 'Yes')
83         m1 <- mxOption(m1, "Verify level", '-1')
84 m1 <- mxOption(m1, "Function precision", '1.0E-5')
85
86 m1 <- mxRun(m1, silent=TRUE)
87
88                                         #print(m1@matrices$ItemParam@values)
89                                         #print(correct.mat)
90 # sometimes found as low as .89, maybe solution is unstable
91 omxCheckCloseEnough(cor(c(m1@matrices$ItemParam@values),
92                         c(correct.mat)), .91, .04)
93 max.se <- max(m1@output$ability[c(2,4),])
94 omxCheckCloseEnough(sum(abs(m1@output$ability[c(1,3),] - ability) < max.se) / (numPeople*2), .797, .02)
95 omxCheckCloseEnough(.672, cor(c(m1@output$ability[c(1,3),]), c(ability)), .01)