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