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
|
#
# Copyright 2007-2020 by the individuals mentioned in the source code history
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
library(OpenMx)
library(testthat)
context("GREML Error Detection")
options(mxCondenseMatrixSlots=TRUE)
mxOption(NULL,"Analytic Gradients","Yes")
require(mvtnorm)
omxCheckError(mxExpectationGREML(V=1),
"argument 'V' is not of type 'character' (the name of the expected covariance matrix)")
set.seed(1234)
dat <- cbind(rnorm(100),rep(1,100))
colnames(dat) <- c("y","x")
testmod <- mxModel(
"GREMLtest",
#mxData(observed=dat, type="raw", sort=F),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 2, labels = "ve", lbound = 0.0001, name = "Ve"),
mxMatrix("Iden",nrow=100,name="I",condenseSlots=T),
mxAlgebra(I %x% Ve,name="V"),
mxExpectationGREML(V="V",Xvars=list("x"),yvars="y",addOnes=F),
mxFitFunctionGREML()
)
omxCheckError(mxRun(testmod),
"the GREML expectation function does not have a dataset associated with it in model 'GREMLtest'")
testmod <- mxModel(
testmod,
mxData(observed=dat, type="raw", sort=F),
mxMatrix("Full",1,1,F,labels="data.y",name="Z")
)
omxCheckError(mxRun(testmod),
"definition variables are incompatible (and unnecessary) with GREML expectation")
testmod <- mxModel(
"GREMLtest",
mxData(observed=dat, type="raw", sort=F),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 2, labels = "ve", lbound = 0.0001, name = "Ve"),
mxMatrix("Full",nrow=100,ncol=99,free=F,values=diag(100)[,-100],name="V",condenseSlots=T),
mxExpectationGREML(V="V",dataset.is.yX=T),
mxFitFunctionGREML()
)
omxCheckError(mxRun(testmod),
"'V' matrix is not square")
testmod$V <- mxMatrix("Full",nrow=99,ncol=99,free=F,values=diag(100)[-100,-100],name="V",condenseSlots=T)
omxCheckError(mxRun(testmod),
"y and V matrices do not have equal numbers of rows")
testmod <- mxModel(
"GREMLtest",
mxData(observed=dat, type="raw", sort=F),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 1, labels = "ve", lbound = 0.0001, name = "Ve"),
mxMatrix("Iden",nrow=100,name="I",condenseSlots=T),
mxAlgebra(I %x% Ve,name="V"),
mxExpectationGREML(V="V",dataset.is.yX=TRUE,casesToDropFromV=101L),
mxFitFunctionGREML()
)
omxCheckWarning(
mxRun(testmod, suppressWarnings=TRUE),
"casesToDrop vector in GREML expectation contains indices greater than the number of datapoints")
testmod <- mxModel(
"GREMLtest",
mxData(observed=dat, type="raw", sort=F),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 2, labels = "ve", lbound = 0.0001, name = "Ve"),
mxMatrix("Iden",nrow=100,name="I",condenseSlots=T),
mxMatrix("Iden",nrow=99,name="J",condenseSlots=T),
mxAlgebra(I %x% Ve,name="V"),
mxExpectationGREML(V="V", dataset.is.yX = T),
mxFitFunctionGREML(dV = c(ve="J"))
)
omxCheckError(mxRun(testmod),
"all derivatives of V must have the same dimensions as V")
testmod <- mxModel(
"GREMLtest",
mxData(observed=dat, type="raw", sort=F),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 2, labels = "ve", lbound = 0.0001, name = "Ve"),
mxMatrix("Iden",nrow=100,name="I",condenseSlots=T),
mxAlgebra(I %x% Ve,name="V"),
mxExpectationGREML(V="V", dataset.is.yX=T),
mxFitFunctionGREML(dV = c(ve="J"))
)
omxCheckError(mxRun(testmod),
"The reference 'J' does not exist. It is used by named reference 'GREMLtest.fitfunction' .")
testmod <- mxModel(
"GREMLtest",
mxData(observed = matrix(dat[,1],1,100,dimnames=list(NULL,paste("y",1:100,sep=""))), type="raw"),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 2, labels = "ve", lbound = 0.0001, name = "Ve"),
mxMatrix("Iden",nrow=100,name="I",condenseSlots=T),
mxMatrix("Zero",1,100,name="Zm"),
mxAlgebra(I %x% Ve,name="V"),
mxExpectationNormal(covariance="V",means="Zm", dimnames=paste("y",1:100,sep="")),
mxFitFunctionGREML()
)
omxCheckError(mxRun(testmod),
"GREML fitfunction is currently only compatible with GREML expectation")
testmod <- mxModel(
"GREMLtest",
mxData(observed=dat, type="raw", sort=F),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 2, labels = "ve", lbound = 0.0001, name = "Ve"),
mxMatrix("Iden",nrow=100,name="I",condenseSlots=T),
mxAlgebra(I %x% Ve,name="V"),
mxExpectationGREML(V="V",Xvars=list("x"),yvars="y",addOnes=F),
mxFitFunctionGREML()
)
omxCheckError(mxRefModels(testmod),
"Reference models for GREML expectation are not implemented")
omxCheckError(mxModel(
"GREMLtest",
mxData(observed=dat, type="raw", sort=F),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 2, labels = "ve", lbound = 0.0001, name = "Ve"),
mxMatrix("Iden",nrow=100,name="I",condenseSlots=T),
mxAlgebra(I %x% Ve,name="V"),
mxExpectationGREML(V="V",Xvars=list("x"),yvars="y",addOnes=F),
mxFitFunctionGREML(autoDerivType="muneric")
),"'muneric' should be one of 'semiAnalyt' and 'numeric'")
testmod <- mxModel(
"GREMLtest",
mxData(observed=dat, type="raw", sort=F),
mxMatrix(type = "Unit", nrow = 100, ncol=100, name = "V", condenseSlots = T),
mxExpectationGREML(V="V",Xvars=list("x"),yvars="y",addOnes=F),
mxFitFunctionGREML()
)
omxCheckError(mxRun(testmod),
"Expected covariance matrix is non-positive-definite at initial values")
testmod <- mxModel(
"GREMLtest",
mxData(observed=dat, type="raw", sort=F),
mxMatrix(type = "Unit", nrow = 100, ncol=100, name = "V", condenseSlots = T),
mxExpectationGREML(V="V",Xvars=list("x"),yvars="y",addOnes=F),
mxFitFunctionML()
)
omxCheckError(mxRun(testmod),
"Expected covariance matrix is non-positive-definite at initial values")
z <- matrix(-1,100,2)
colnames(z) <- c("z1","z2")
dat2 <- cbind(dat,z)
testmod <- mxModel(
"GREMLtest",
mxData(observed=dat2, type="raw", sort=F),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 2, labels = "ve", lbound = 0.0001, name = "Ve"),
mxMatrix("Iden",nrow=100,name="I",condenseSlots=T),
mxAlgebra(I %x% Ve,name="V"),
mxExpectationGREML(V="V",Xvars=list(c("x","z1","z2")),yvars="y",addOnes=F),
mxFitFunctionGREML()
)
omxCheckError(mxRun(testmod),
"Cholesky factorization failed at initial values; possibly, the matrix of covariates is rank-deficient")
testmod <- mxModel(
"GREMLtest",
mxData(observed=dat2, type="raw", sort=F),
mxMatrix(type = "Full", nrow = 1, ncol=1, free=T, values = 2, labels = "ve", lbound = 0.0001, name = "Ve"),
mxMatrix("Iden",nrow=100,name="I",condenseSlots=T),
mxAlgebra(I %x% Ve,name="V"),
mxExpectationGREML(V="V",Xvars=list(c("x","z1","z2")),yvars="y",addOnes=F),
mxFitFunctionML()
)
omxCheckError(mxRun(testmod),
"Cholesky factorization failed at initial values; possibly, the matrix of covariates is rank-deficient")
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))
x <- rnorm(100)
dat3 <- cbind(y,x)
rm(x,y)
colnames(dat3) <- c("y","x")
testmod <- 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.25, labels = "va2", name = "Va2"),
mxData(observed = dat3, 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"),
mxMatrix(type="Full",nrow=1,ncol=1,free=F,values=0.64,name="aug"),
mxMatrix(type="Zero",nrow=1,ncol=1,name="Zilch"),
mxFitFunctionGREML(dV=c(ve="I",va1="A1",va2="A2"),aug="aug",augHess="Zilch")
)
omxCheckError(
mxRun(testmod),
"if argument 'augHess' has nonzero length, then argument 'augGrad' must as well")
testmod$fitfunction <- mxFitFunctionGREML(dV=c(ve="I",va1="A1",va2="A2"),aug="aug")
omxCheckError(
mxRun(testmod),
"if arguments 'dV' and 'aug' have nonzero length, then 'augGrad' must as well")
testmod$fitfunction <- mxFitFunctionGREML(dV=c(ve="I",va1="A1",va2="A2",va3="I"))
omxCheckError(
mxRun(testmod),
"length of argument 'dV' is greater than the number of explicit free parameters")
testmod <- 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.25, labels = "va2", name = "Va2"),
mxData(observed = dat3, 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"),
mxComputeSequence(steps=list(
mxComputeNewtonRaphson(fitfunction="fitfunction"),
mxComputeOnce('fitfunction', c('fit','gradient','hessian','ihessian')),
mxComputeStandardError(),
mxComputeReportDeriv(),
mxComputeReportExpectation()
)),
mxFitFunctionGREML(dV=c(ve="I",va1="A1",va2="A2",va3="I"))
)
omxCheckError(
mxRun(testmod),
"length of argument 'dV' is greater than the number of explicit free parameters")
testmod <- 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.25, labels = "va2", name = "Va2"),
mxData(observed = dat3, 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"),
mxComputeSequence(steps=list(
mxComputeNewtonRaphson(fitfunction="fitfunction"),
mxComputeOnce('fitfunction', c('fit','gradient','hessian')),
mxComputeStandardError(),
mxComputeReportDeriv(),
mxComputeReportExpectation()
)),
mxMatrix(type="Full",nrow=1,ncol=1,free=F,values=2.1,name="aug"),
mxMatrix(type="Zero",nrow=2,ncol=1,free=F,name="ag"),
mxMatrix(type="Zero",nrow=3,ncol=3,free=F,name="ah"),
mxFitFunctionGREML(dV=c(ve="I",va1="A1",va2="A2"),aug="aug",augGrad="ag",augHess="ah")
)
omxCheckError(
mxRun(testmod),
"matrix referenced by 'augGrad' must have as many elements as there are explicit free parameters")
testmod$ag <- mxMatrix(type="Zero",nrow=3,ncol=1,free=F,name="ag")
testmod$ah <- mxMatrix(type="Zero",nrow=2,ncol=3,free=F,name="ah")
omxCheckError(
mxRun(testmod),
"matrix referenced by 'augHess' must be square (instead of 2x3)")
testmod$ah <- mxMatrix(type="Zero",nrow=2,ncol=2,free=F,name="ah")
omxCheckError(
mxRun(testmod),
"Augmentation derivatives non-conformable (gradient is size 3 and Hessian is 2x2)")
mxOption(reset=TRUE)
|