Rename start to adjustStart
[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 (0) {
78   library(mirt)
79   rdata <- sapply(data, unclass)-1
80   # for flexMIRT, write CSV
81   #write.table(rdata, file="ifa-drm-mg.csv", quote=FALSE, row.names=FALSE, col.names=FALSE)
82   pars <- mirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars='values')
83   pars[pars$name=="a1",'value'] <- 1
84   pars[pars$name=="a1",'est'] <- FALSE
85   pars[pars$name=="COV_11",'est'] <- TRUE
86   fit <- mirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars=pars)
87   # LL -7064.519 * -2 = 14129.04
88   got <- coef(fit)
89   print(got$GroupPars)
90   # COV 4.551
91   got$GroupPars <- NULL
92   round(m2@matrices$itemParam@values - simplify2array(got), 2)
93   
94   # MH-RM takes forever, not run
95   pars <- confmirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars='values')
96   pars[pars$name=="a1",'value'] <- 1
97   pars[pars$name=="a1",'est'] <- FALSE
98   pars[pars$name=="COV_11",'est'] <- TRUE
99   fit <- confmirt(rdata, 1, itemtype="2PL", D=1, quadpts=49, pars=pars)
100   got <- coef(fit)
101   got$GroupPars <- NULL
102   round(m2@matrices$itemParam@values - sapply(got, function(l) l[1,]), 2)
103 }