1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
require(OpenMx)
options(mxCondenseMatrixSlots=TRUE)
# mxOption(NULL,"Default optimizer","NPSOL")
# mxOption(NULL,"Number of threads",2)
# mxOption(NULL,"Print level",20)
# mxOption(NULL,"Print file",3)
# mxOption(NULL,"Verify level",3)
require(mvtnorm)
#Generate data:
set.seed(476)
A1 <- matrix(0,100,100)
A1[lower.tri(A1)] <- runif(4950, -0.025, 0.025)
A1 <- A1 + t(A1)
diag(A1) <- runif(100,0.95,1.05)
A2 <- matrix(0,100,100)
A2[lower.tri(A2)] <- runif(4950, -0.025, 0.025)
A2 <- A2 + t(A2)
diag(A2) <- runif(100,0.95,1.05)
y <- t(rmvnorm(1,sigma=A1*0.25)+rmvnorm(1,sigma=A2*0.25))
y <- y + rnorm(100,sd=sqrt(0.5))
y[100] <- NA
x <- rnorm(100)
dat <- cbind(y,x)
colnames(dat) <- c("y","x")
plan <- mxComputeSequence(
steps=list(
mxComputeGradientDescent(engine=mxOption(NULL,"Default optimizer")),
mxComputeOnce('fitfunction', c('gradient','hessian')),
mxComputeStandardError(),
mxComputeHessianQuality(),
mxComputeReportDeriv(),
mxComputeReportExpectation()
))
test0 <- mxModel(
"GREMLtest",
plan,
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(va1="A1",va2="A2",ve="I"),infoMatType="expected")
)
test0 <- mxRun(test0)
summary(test0)
omxCheckCloseEnough(test0$output$fit, 280.3646874, 1e-4)
omxCheckCloseEnough(coef(test0), c(0.5668669,-0.8059083,1.1844126), 1e-4)
omxCheckCloseEnough(test0$output$standardErrors[,1], c(1.2020943,0.8700919,0.8835703), 1e-4)
omxCheckCloseEnough(test0$output$gradient, c(-2.539113e-07, -3.300840e-07, -3.607828e-07), 1e-4)
omxCheckCloseEnough(test0$expectation$b[,1], c(-0.003919201,0.043639875), 1e-4)
omxCheckCloseEnough(vech(test0$expectation$bcov), c(0.0092116302,0.0003739306,0.0087043503), 1e-4)
test1sa <- mxModel(
"GREMLtest",
plan,
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(va1="A1",va2="A2"),infoMatType="expected")
)
test1sa <- mxRun(test1sa)
summary(test1sa)
omxCheckCloseEnough(test0$output$fit, test1sa$output$fit, 1e-12)
omxCheckCloseEnough(coef(test0),coef(test1sa),1e-7)
omxCheckCloseEnough(test0$output$standardErrors,test1sa$output$standardErrors,1e-7)
omxCheckCloseEnough(test0$output$gradient,test1sa$output$gradient,2e-6)
omxCheckCloseEnough(test0$expectation$b,test1sa$expectation$b,1e-8)
omxCheckCloseEnough(test0$expectation$bcov,test1sa$expectation$bcov,1e-10)
test1num <- mxModel(
"GREMLtest",
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(va1="A1",va2="A2"),autoDerivType="numeric",infoMatType="expected")
)
test1num <- mxRun(test1num)
summary(test1num)
omxCheckCloseEnough(test0$output$fit, test1num$output$fit, 5e-9)
omxCheckCloseEnough(coef(test0),coef(test1num),1e-5)
omxCheckCloseEnough(test0$output$standardErrors,test1num$output$standardErrors,0.05)
omxCheckCloseEnough(test0$output$gradient,test1num$output$gradient,5e-4)
omxCheckCloseEnough(test0$expectation$b,test1num$expectation$b,5e-6)
omxCheckCloseEnough(test0$expectation$bcov,test1num$expectation$bcov,1e-7)
test2sa <- mxModel(
"GREMLtest",
plan,
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(va2="A2",ve="I"),infoMatType="expected")
)
test2sa <- mxRun(test2sa)
summary(test2sa)
omxCheckCloseEnough(test0$output$fit, test2sa$output$fit, 1e-12)
omxCheckCloseEnough(coef(test0),coef(test2sa),1e-7)
omxCheckCloseEnough(test0$output$standardErrors,test2sa$output$standardErrors,1e-8)
omxCheckCloseEnough(test0$output$gradient,test2sa$output$gradient,1e-6)
omxCheckCloseEnough(test0$expectation$b,test2sa$expectation$b,1e-9)
omxCheckCloseEnough(test0$expectation$bcov,test2sa$expectation$bcov,1e-10)
test2num <- mxModel(
"GREMLtest",
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(va2="A2",ve="I"),autoDerivType="numeric",infoMatType="expected")
)
test2num <- mxRun(test2num)
summary(test2num)
omxCheckCloseEnough(test0$output$fit, test2num$output$fit, 5e-9)
omxCheckCloseEnough(coef(test0),coef(test2num),1e-5)
omxCheckCloseEnough(test0$output$standardErrors,test2num$output$standardErrors,0.05)
omxCheckCloseEnough(test0$output$gradient,test2num$output$gradient,5e-4)
omxCheckCloseEnough(test0$expectation$b,test2num$expectation$b,5e-6)
omxCheckCloseEnough(test0$expectation$bcov,test2num$expectation$bcov,1e-7)
test3sa <- mxModel(
"GREMLtest",
plan,
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(va1="A1",ve="I"),infoMatType="expected")
)
test3sa <- mxRun(test3sa)
summary(test3sa)
omxCheckCloseEnough(test0$output$fit, test3sa$output$fit, 1e-12)
omxCheckCloseEnough(coef(test0),coef(test3sa),1e-7)
omxCheckCloseEnough(test0$output$standardErrors,test3sa$output$standardErrors,1e-7)
omxCheckCloseEnough(test0$output$gradient,test3sa$output$gradient,2e-6)
omxCheckCloseEnough(test0$expectation$b,test3sa$expectation$b,1e-8)
omxCheckCloseEnough(test0$expectation$bcov,test3sa$expectation$bcov,1e-10)
test3num <- mxModel(
"GREMLtest",
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(va1="A1",ve="I"),autoDerivType="numeric",infoMatType="expected")
)
test3num <- mxRun(test3num)
summary(test3num)
omxCheckCloseEnough(test0$output$fit, test3num$output$fit, 5e-9)
omxCheckCloseEnough(coef(test0),coef(test3num),1e-5)
omxCheckCloseEnough(test0$output$standardErrors,test3num$output$standardErrors,0.05)
omxCheckCloseEnough(test0$output$gradient,test3num$output$gradient,5e-4)
omxCheckCloseEnough(test0$expectation$b,test3num$expectation$b,5e-6)
omxCheckCloseEnough(test0$expectation$bcov,test3num$expectation$bcov,1e-7)
test4sa <- mxModel(
"GREMLtest",
plan,
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(ve="I"),infoMatType="expected")
)
test4sa <- mxRun(test4sa)
summary(test4sa)
omxCheckCloseEnough(test0$output$fit, test4sa$output$fit, 1e-12)
omxCheckCloseEnough(coef(test0),coef(test4sa),1e-7)
omxCheckCloseEnough(test0$output$standardErrors,test4sa$output$standardErrors,1e-8)
omxCheckCloseEnough(test0$output$gradient,test4sa$output$gradient,1e-6)
omxCheckCloseEnough(test0$expectation$b,test4sa$expectation$b,1e-9)
omxCheckCloseEnough(test0$expectation$bcov,test4sa$expectation$bcov,1e-10)
test4num <- mxModel(
"GREMLtest",
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(ve="I"),autoDerivType="numeric",infoMatType="expected")
)
test4num <- mxRun(test4num)
summary(test4num)
omxCheckCloseEnough(test0$output$fit, test4num$output$fit, 5e-9)
omxCheckCloseEnough(coef(test0),coef(test4num),1e-5)
omxCheckCloseEnough(test0$output$standardErrors,test4num$output$standardErrors,0.05)
omxCheckCloseEnough(test0$output$gradient,test4num$output$gradient,5e-4)
omxCheckCloseEnough(test0$expectation$b,test4num$expectation$b,5e-6)
omxCheckCloseEnough(test0$expectation$bcov,test4num$expectation$bcov,1e-7)
test5sa <- mxModel(
"GREMLtest",
plan,
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(va1="A1"),infoMatType="expected")
)
test5sa <- mxRun(test5sa)
summary(test5sa)
omxCheckCloseEnough(test0$output$fit, test5sa$output$fit, 1e-12)
omxCheckCloseEnough(coef(test0),coef(test5sa),1e-7)
omxCheckCloseEnough(test0$output$standardErrors,test5sa$output$standardErrors,1e-8)
omxCheckCloseEnough(test0$output$gradient,test5sa$output$gradient,1e-6)
omxCheckCloseEnough(test0$expectation$b,test5sa$expectation$b,1e-9)
omxCheckCloseEnough(test0$expectation$bcov,test5sa$expectation$bcov,1e-10)
test5num <- mxModel(
"GREMLtest",
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(va1="A1"),autoDerivType="numeric",infoMatType="expected")
)
test5num <- mxRun(test5num)
summary(test5num)
omxCheckCloseEnough(test0$output$fit, test5num$output$fit, 5e-9)
omxCheckCloseEnough(coef(test0),coef(test5num),1e-5)
omxCheckCloseEnough(test0$output$standardErrors,test5num$output$standardErrors,0.05)
omxCheckCloseEnough(test0$output$gradient,test5num$output$gradient,5e-4)
omxCheckCloseEnough(test0$expectation$b,test5num$expectation$b,5e-6)
omxCheckCloseEnough(test0$expectation$bcov,test5num$expectation$bcov,1e-7)
test6sa <- mxModel(
"GREMLtest",
plan,
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(va2="A2"),infoMatType="expected")
)
test6sa <- mxRun(test6sa)
summary(test6sa)
omxCheckCloseEnough(test0$output$fit, test6sa$output$fit, 1e-12)
omxCheckCloseEnough(coef(test0),coef(test6sa),1e-7)
omxCheckCloseEnough(test0$output$standardErrors,test6sa$output$standardErrors,1e-8)
omxCheckCloseEnough(test0$output$gradient,test6sa$output$gradient,1e-6)
omxCheckCloseEnough(test0$expectation$b,test6sa$expectation$b,1e-9)
omxCheckCloseEnough(test0$expectation$bcov,test6sa$expectation$bcov,1e-10)
test6num <- mxModel(
"GREMLtest",
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values =0.5, labels = "ve", lbound = 0.0001,
name = "Ve"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.25, labels = "va1", name = "Va1"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 0.20, labels = "va2", name = "Va2"),
mxData(observed = dat, type="raw", sort=FALSE),
mxExpectationGREML(V="V",yvars="y", Xvars="x", addOnes=T),
mxMatrix("Iden",nrow=100,name="I"),
mxMatrix("Symm",nrow=100,free=F,values=A1,name="A1"),
mxMatrix("Symm",nrow=100,free=F,values=A2,name="A2"),
mxAlgebra((A1%x%Va1) + (A2%x%Va2) + (I%x%Ve), name="V"),
mxFitFunctionGREML(dV=c(va2="A2"),autoDerivType="numeric",infoMatType="expected")
)
test6num <- mxRun(test6num)
summary(test6num)
omxCheckCloseEnough(test0$output$fit, test6num$output$fit, 5e-9)
omxCheckCloseEnough(coef(test0),coef(test6num),1e-5)
omxCheckCloseEnough(test0$output$standardErrors,test6num$output$standardErrors,0.05)
omxCheckCloseEnough(test0$output$gradient,test6num$output$gradient,5e-4)
omxCheckCloseEnough(test0$expectation$b,test6num$expectation$b,5e-6)
omxCheckCloseEnough(test0$expectation$bcov,test6num$expectation$bcov,1e-7)
|