Fix incorrect handling of missing data
[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         ip.mat <- mxMatrix(name="itemParam", nrow=4, ncol=numItems,
20                            values=c(1,0,0, 1),
21                            free=c(FALSE, TRUE, FALSE, FALSE))
22         
23         eip.mat <- mxAlgebra(itemParam, name="EItemParam")
24
25         m.mat <- mxMatrix(name="mean", nrow=1, ncol=1, values=0, free=FALSE)
26         cov.mat <- mxMatrix(name="cov", nrow=1, ncol=1, values=1, free=TRUE)
27
28         m2 <- mxModel(model="drmmg", ip.mat, m.mat, cov.mat, eip.mat,
29                       mxData(observed=data, type="raw"),
30                       mxExpectationBA81(mean="mean", cov="cov",
31                                         ItemSpec=items, ItemParam="itemParam",
32                                         EItemParam="EItemParam"),
33                       mxFitFunctionML(),
34                       mxComputeIterate(steps=list(
35                                          mxComputeOnce('expectation', context='EM'),
36                                          mxComputeNewtonRaphson(free.set='itemParam'),
37                                          mxComputeOnce('expectation'),
38            mxComputeOnce('fitfunction', adjustStart=TRUE, free.set=c("mean", "cov"))
39 #                                        mxComputeGradientDescent(adjustStart="expectation", useGradient=TRUE,
40 #                                    free.set=c("mean", "cov"))
41                                          )))
42         
43         if (0) {
44                 fm <- read.flexmirt("/home/joshua/irt/ifa-drm-mg/ifa-drm-mg-prm.txt")
45                 cModel <- m2
46                 cModel@matrices$itemParam@values[2,] <- fm$G1$param[2,]
47                 cModel@matrices$cov@values <- fm$G1$cov
48                 cModel <- mxModel(cModel,
49                                   mxExpectationBA81(mean="mean", cov="cov",
50                                                     ItemSpec="ItemSpec",
51                                                     EItemParam="EItemParam", scores="full"),
52                                   mxComputeSequence(steps=list(
53                                                       mxComputeOnce('expectation'),
54                                                       mxComputeOnce('fitfunction'))))
55                 cModel <- mxRun(cModel)
56                 cModel@matrices$cov@values - fm$G1$cov
57                 cModel@output$minimum
58         }
59
60         if(1) {
61                 m2 <- mxOption(m2, "Analytic Gradients", 'Yes')
62                 m2 <- mxOption(m2, "Verify level", '-1')
63                 m2 <- mxOption(m2, "Function precision", '1.0E-5')
64                 m2 <- mxRun(m2)
65                 
66                 omxCheckCloseEnough(m2@fitfunction@result, 14129.94, .01)
67                 omxCheckCloseEnough(m2@matrices$cov@values[1,1], 4.377, .01)
68                 
69                                         #print(m2@matrices$itemParam@values)
70                                         #print(correct.mat)
71                 got <- cor(c(m2@matrices$itemParam@values),
72                            c(correct))
73                 omxCheckCloseEnough(got, .994, .01)
74         }
75 }
76
77 if (1) {
78   ip.mat <- mxMatrix(name="itemParam", nrow=4, ncol=numItems,
79                      values=c(1,0,0, 1),
80                      free=c(TRUE, TRUE, FALSE, FALSE))
81   ip.mat@labels[1,] <- 'a1'
82   
83   eip.mat <- mxAlgebra(itemParam, name="EItemParam")
84   
85   m.mat <- mxMatrix(name="mean", nrow=1, ncol=1, values=0, free=FALSE)
86   cov.mat <- mxMatrix(name="cov", nrow=1, ncol=1, values=1, free=FALSE)
87   
88   m2 <- mxModel(model="drmmg", ip.mat, m.mat, cov.mat, eip.mat,
89                 mxData(observed=data, type="raw"),
90                 mxExpectationBA81(mean="mean", cov="cov",
91                                   ItemSpec=items, ItemParam="itemParam",
92                                   EItemParam="EItemParam"),
93                 mxFitFunctionML(),
94                 mxComputeIterate(steps=list(
95                   mxComputeOnce('expectation', context='EM'),
96                   mxComputeNewtonRaphson(free.set='itemParam'),
97                   mxComputeOnce('expectation'),
98                   mxComputeOnce('fitfunction', adjustStart=TRUE, free.set=c("mean", "cov"))
99                 )))
100   m2 <- mxRun(m2)
101   omxCheckCloseEnough(m2@fitfunction@result, 14129.04, .01)
102   omxCheckCloseEnough(m2@matrices$itemParam@values[1,], rep(2.133, numItems), .002)
103   # correct values are from flexMIRT
104   est <- c(-0.838622, -1.02653, -0.0868472, -0.251784, 0.953364,  0.735258, 0.606918,
105            1.04239, 0.466055, -2.05196, -0.0456446,  -0.320668, -0.362073, 2.02502,
106            0.635298, -0.0731132, -2.05196,  -0.0456446, -1.17429, 0.880002, -0.838622,
107            -0.838622, 1.02747,  0.424094, -0.584298, 0.663755, 0.663755, 0.064287, 1.38009,
108            1.01259 )
109   omxCheckCloseEnough(m2@matrices$itemParam@values[2,], est, .002)
110 }
111
112 if (0) {
113   library(mirt)
114   rdata <- sapply(data, unclass)-1
115   # for flexMIRT, write CSV
116   #write.table(rdata, file="ifa-drm-mg.csv", quote=FALSE, row.names=FALSE, col.names=FALSE)
117   pars <- mirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars='values')
118   pars[pars$name=="a1",'value'] <- 1
119   pars[pars$name=="a1",'est'] <- FALSE
120   pars[pars$name=="COV_11",'est'] <- TRUE
121   fit <- mirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars=pars)
122   # LL -7064.519 * -2 = 14129.04
123   got <- coef(fit)
124   print(got$GroupPars)
125   # COV 4.551
126   got$GroupPars <- NULL
127   round(m2@matrices$itemParam@values - simplify2array(got), 2)
128   
129   # MH-RM takes forever, not run
130   pars <- confmirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars='values')
131   pars[pars$name=="a1",'value'] <- 1
132   pars[pars$name=="a1",'est'] <- FALSE
133   pars[pars$name=="COV_11",'est'] <- TRUE
134   fit <- confmirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars=pars)
135   got <- coef(fit)
136   got$GroupPars <- NULL
137   round(m2@matrices$itemParam@values - sapply(got, function(l) l[1,]), 2)
138 }