Pass item models in a list instead of an MxMatrix
[openmx:openmx.git] / models / passing / ifa-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 design <- mxMatrix(name="Design", nrow=maxDim, ncol=numItems,
43                   values=design)
44
45 ip.mat <- mxMatrix(name="ItemParam", nrow=maxParam, ncol=numItems,
46                    values=c(1, 1.4, 0, 0, 1),
47          free=c(TRUE, TRUE, FALSE, FALSE, FALSE,
48           rep(c(TRUE, TRUE, TRUE, FALSE, FALSE), 4)))
49 ip.mat@values[4,1] <- 1
50
51 eip.mat <- mxAlgebra(ItemParam, name="EItemParam")
52
53 m.mat <- mxMatrix(name="mean", nrow=1, ncol=2, values=0, free=FALSE)
54 cov.mat <- mxMatrix(name="cov", nrow=2, ncol=2, values=diag(2), free=FALSE)
55
56 m1 <- mxModel(model="2dim",
57               design,
58           ip.mat, m.mat, cov.mat, eip.mat,
59           mxData(observed=data, type="raw"),
60           mxExpectationBA81(mean="mean", cov="cov",
61              ItemSpec=items,
62                             EItemParam="EItemParam",
63              Design="Design",
64             qpoints=29,
65             scores="full"),
66           mxFitFunctionBA81(ItemParam="ItemParam"),
67               mxComputeIterate(steps=list(
68                 mxComputeOnce("EItemParam"),
69                 mxComputeOnce('expectation', context='EM'),
70                                  mxComputeNewtonRaphson(free.set='ItemParam'),
71 #                                mxComputeGradientDescent(free.set='ItemParam'),
72                 mxComputeOnce('expectation'),
73                 mxComputeOnce('fitfunction'))))
74
75         m1 <- mxOption(m1, "Analytic Gradients", 'Yes')
76         m1 <- mxOption(m1, "Verify level", '-1')
77 m1 <- mxOption(m1, "Function precision", '1.0E-5')
78
79 m1 <- mxRun(m1, silent=TRUE)
80
81                                         #print(m1@matrices$ItemParam@values)
82                                         #print(correct.mat)
83 # sometimes found as low as .89, maybe solution is unstable
84 omxCheckCloseEnough(cor(c(m1@matrices$ItemParam@values),
85                         c(correct.mat)), .9, .06)
86 scores.out <- m1@expectation@scores.out
87 max.se <- max(scores.out[,3:4])
88 omxCheckCloseEnough(sum(abs(scores.out[,1:2] - t(ability)) < max.se) / (numPeople*2), .797, .02)
89 omxCheckCloseEnough(.672, cor(c(scores.out[,1:2]), c(t(ability))), .01)