Move all fitfunction args to expectation
[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", fixed=TRUE)
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                       mxFitFunctionBA81(),
34                                         # integrate FitFuncBA81 into FitFuncML
35                       mxComputeIterate(steps=list(
36                                          mxComputeOnce("EItemParam"),
37                                          mxComputeOnce('expectation', context='EM'),
38                                         #                                  mxComputeGradientDescent(free.set='itemParam'),
39                                          mxComputeNewtonRaphson(free.set='itemParam'),
40                                          mxComputeOnce('expectation'),
41            mxComputeOnce('fitfunction', start=TRUE, free.set=c("mean", "cov"))
42 #                                        mxComputeGradientDescent(start="expectation", useGradient=TRUE,
43 #                                    free.set=c("mean", "cov"))
44                                          )))
45         
46         if (0) {
47                 fm <- read.flexmirt("/home/joshua/irt/ifa-drm-mg/ifa-drm-mg-prm.txt")  # fix TODO
48                 cModel <- m2
49                 cModel@matrices$itemParam@values[2,] <- fm$G1$param[2,]
50                 cModel@matrices$cov@values <- fm$G1$cov
51                 cModel <- mxModel(cModel,
52                                   mxExpectationBA81(mean="mean", cov="cov",
53                                                     ItemSpec="ItemSpec",
54                                                     EItemParam="EItemParam", scores="full"),
55                                   mxComputeSequence(steps=list(
56                                                       mxComputeOnce('expectation'),
57                                                       mxComputeOnce('fitfunction'))))
58                 cModel <- mxRun(cModel)
59                 cModel@matrices$cov@values - fm$G1$cov
60                 cModel@output$minimum
61         }
62
63         if(1) {
64                 m2 <- mxOption(m2, "Analytic Gradients", 'Yes')
65                 m2 <- mxOption(m2, "Verify level", '-1')
66                 m2 <- mxOption(m2, "Function precision", '1.0E-5')
67                 m2 <- mxRun(m2)
68                 
69                 omxCheckCloseEnough(m2@fitfunction@result, 14129.94, .01)
70                 omxCheckCloseEnough(m2@matrices$cov@values[1,1], 4.377, .01)
71                 
72                                         #print(m2@matrices$itemParam@values)
73                                         #print(correct.mat)
74                 got <- cor(c(m2@matrices$itemParam@values),
75                            c(correct))
76                 omxCheckCloseEnough(got, .994, .01)
77         }
78 }
79
80 if (0) {
81   library(mirt)
82   rdata <- sapply(data, unclass)-1
83   # for flexMIRT, write CSV
84   #write.table(rdata, file="ifa-drm-mg.csv", quote=FALSE, row.names=FALSE, col.names=FALSE)
85   pars <- mirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars='values')
86   pars[pars$name=="a1",'value'] <- 1
87   pars[pars$name=="a1",'est'] <- FALSE
88   pars[pars$name=="COV_11",'est'] <- TRUE
89   fit <- mirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars=pars)
90   # LL -7064.519 * -2 = 14129.04
91   got <- coef(fit)
92   print(got$GroupPars)
93   # COV 4.551
94   got$GroupPars <- NULL
95   round(m2@matrices$itemParam@values - simplify2array(got), 2)
96   
97   # MH-RM takes forever, not run
98   pars <- confmirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars='values')
99   pars[pars$name=="a1",'value'] <- 1
100   pars[pars$name=="a1",'est'] <- FALSE
101   pars[pars$name=="COV_11",'est'] <- TRUE
102   fit <- confmirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars=pars)
103   got <- coef(fit)
104   got$GroupPars <- NULL
105   round(m2@matrices$itemParam@values - sapply(got, function(l) l[1,]), 2)
106 }