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