Add multigroup tests
[openmx:openmx.git] / models / passing / ifa-drm-mg.R
1 library(OpenMx)
2 library(rpf)
3
4 set.seed(9)
5
6 numItems <- 30
7 i1 <- rpf.drm(multidimensional=TRUE)
8 items <- list()
9 items[1:numItems] <- i1
10 correct <- matrix(NA, 4, numItems)
11 for (x in 1:numItems) correct[,x] <- rpf.rparam(i1)
12 correct[1,] <- 1
13 correct[3,] <- 0
14 correct[4,] <- 1
15
16 data <- rpf.sample(500, items, correct, cov=matrix(5,1,1))
17
18 if(1) {
19         spec <- mxMatrix(name="ItemSpec", nrow=3, ncol=numItems,
20                          values=sapply(items, function(m) slot(m,'spec')),
21                          free=FALSE, byrow=TRUE)
22         
23         ip.mat <- mxMatrix(name="itemParam", nrow=4, ncol=numItems,
24                            values=c(1,0,0, 1),
25                            free=c(FALSE, TRUE, FALSE, FALSE))
26         ip.mat@free.group <- 'param'
27         
28         eip.mat <- mxAlgebra(itemParam, name="EItemParam", fixed=TRUE)
29
30         m.mat <- mxMatrix(name="mean", nrow=1, ncol=1, values=0, free=FALSE)
31         cov.mat <- mxMatrix(name="cov", nrow=1, ncol=1, values=1, free=TRUE)
32
33         m2 <- mxModel(model="drmmg", ip.mat, spec, m.mat, cov.mat, eip.mat,
34                       mxData(observed=data, type="raw"),
35                       mxExpectationBA81(mean="mean", cov="cov",
36                                         ItemSpec="ItemSpec",
37                                         EItemParam="EItemParam"),
38                       mxFitFunctionBA81(ItemParam="itemParam"),
39                                         # integrate FitFuncBA81 into FitFuncML
40                       mxComputeIterate(steps=list(
41                                          mxComputeOnce("EItemParam"),
42                                          mxComputeOnce('expectation', context='E'),
43                                         #                                  mxComputeGradientDescent(free.group='param'),
44                                          mxComputeNewtonRaphson(free.group='param'),
45                                         # list=(matrix, free parameter, models)
46                                          mxComputeOnce('expectation', context='M'),
47                                          mxComputeOnce('fitfunction')
48                                          )))
49         
50         if (0) {
51                 fm <- read.flexmirt("/home/joshua/irt/ifa-drm-mg/ifa-drm-mg-prm.txt")  # fix TODO
52                 cModel <- m2
53                 cModel@matrices$itemParam@values[2,] <- fm$G1$param[2,]
54                 cModel@matrices$cov@values <- fm$G1$cov
55                 cModel <- mxModel(cModel,
56                                   mxExpectationBA81(mean="mean", cov="cov",
57                                                     ItemSpec="ItemSpec",
58                                                     EItemParam="EItemParam", scores="full"),
59                                   mxComputeSequence(steps=list(
60                                                       mxComputeOnce('expectation', context='M'),
61                                                       mxComputeOnce('fitfunction'))))
62                 cModel <- mxRun(cModel)
63                 cModel@matrices$cov@values - fm$G1$cov
64                 cModel@output$minimum
65         }
66
67         if(1) {
68                 m2 <- mxOption(m2, "Analytic Gradients", 'Yes')
69                 m2 <- mxOption(m2, "Verify level", '-1')
70                 m2 <- mxOption(m2, "Function precision", '1.0E-7')
71                 m2 <- mxRun(m2)
72                 
73                                         #  omxCheckCloseEnough(m2@output$minimum, 14130.2, 1) TODO
74                 omxCheckCloseEnough(m2@matrices$cov@values[1,1], 4.377, .01)
75                 
76                                         #print(m2@matrices$itemParam@values)
77                                         #print(correct.mat)
78                 got <- cor(c(m2@matrices$itemParam@values),
79                            c(correct))
80                 omxCheckCloseEnough(got, .994, .01)
81         }
82 }
83
84 if (0) {
85   library(mirt)
86   rdata <- sapply(data, unclass)-1
87   # for flexMIRT, write CSV
88   #write.table(rdata, file="ifa-drm-mg.csv", quote=FALSE, row.names=FALSE, col.names=FALSE)
89   pars <- mirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars='values')
90   pars[pars$name=="a1",'value'] <- 1
91   pars[pars$name=="a1",'est'] <- FALSE
92   pars[pars$name=="COV_11",'est'] <- TRUE
93   fit <- mirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars=pars)
94   # LL -7064.519 * -2 = 14129.04
95   got <- coef(fit)
96   print(got$GroupPars)
97   # COV 4.551
98   got$GroupPars <- NULL
99   round(m2@matrices$itemParam@values - simplify2array(got), 2)
100   
101   # MH-RM takes forever, not run
102   pars <- confmirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars='values')
103   pars[pars$name=="a1",'value'] <- 1
104   pars[pars$name=="a1",'est'] <- FALSE
105   pars[pars$name=="COV_11",'est'] <- TRUE
106   fit <- confmirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars=pars)
107   got <- coef(fit)
108   got$GroupPars <- NULL
109   round(m2@matrices$itemParam@values - sapply(got, function(l) l[1,]), 2)
110 }