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
|
# This is the original test case that Timo & I wrote back in Spring 2013.
#options(error = utils::recover) # uncomment for more help with debugging
library(OpenMx)
library(mvtnorm)
#mxOption(key="Parallel diagnostics", value = "Yes")
set.seed(1)
more.noise <- 0
#more.noise <- 1
gen.data <- function(n) {
data.cov <- matrix(c(1, .2, .2, 1), byrow=TRUE, nrow=2)
latent <- rmvnorm(n, mean=c(0,0), sigma=data.cov)
colnames(latent) <- c("A","B")
latent <- as.data.frame(latent)
df <- data.frame(C=latent$A + latent$B,
D=latent$A - latent$B)
if (more.noise) {
df$C <- df$C + rnorm(1, sd=more.noise)
df$D <- df$D + rnorm(1, sd=more.noise)
}
df
}
fanout <- 5
school.data <- cbind(id=1:fanout, gen.data(fanout))
#school.data$C <- school.data$id * 1000
teacher.data <- cbind(schoolId=1:fanout, id=seq(1,fanout^2),
gen.data(fanout^2))
#teacher.data$C <- teacher.data$id * 100
student.data <- cbind(teacherId=seq(1,fanout^2),
id=seq(1,fanout^3), gen.data(fanout^3))
stack.data <- function(key, upper, lower) {
for (pk in upper$id) {
mask <- lower[[key]] == pk
for (col in c('C','D')) {
lower[mask, col] <-
lower[mask, col] + upper[upper$id == pk, 'C']
}
}
lower
}
teacher.data <- stack.data("schoolId", school.data, teacher.data)
student.data <- stack.data("teacherId", teacher.data, student.data)
manifests<-c("C","D")
latents<-c("A","B")
student <- mxModel(
"student", type="RAM",
manifestVars = manifests,
latentVars = latents,
mxPath(from="A",to=c("C","D"), free=c(FALSE,FALSE),
value=c(1,1), arrows=1,
label=c("A_TO_C","A_TO_D") ),
mxPath(from="B",to=c("C","D"), free=c(FALSE,FALSE), value=c(1,-1) ,
arrows=1, label=c("B_TO_C","B_TO_D") ),
mxPath(from="A",to=c("A","B"), free=c(TRUE,TRUE),
value=c(1,0), arrows=2,
label=c("VAR_A","COV_A_B") ),
mxPath(from="B",to=c("B"), free=c(TRUE), value=c(1) , arrows=2,
label=c("VAR_B") ),
mxPath(from="C",to=c("C"), free=as.logical(more.noise),
value=more.noise, arrows=2, label=c("VAR_C") ),
mxPath(from="D",to=c("D"), free=as.logical(more.noise),
value=more.noise, arrows=2, label=c("VAR_D") ),
mxPath(from="one", to=c(manifests, latents), value=0, free=FALSE)
);
relabel <- function(m, prefix) {
for (mat in c("A","S")) {
lab <- m[[mat]]$labels
lab[!is.na(lab)] <- paste0(prefix, lab[!is.na(lab)])
m[[mat]]$labels <- lab
}
m
}
teacher <- relabel(mxModel(student, name="teacher"), "tea_")
school <- relabel(mxModel(student, name="school"), "sch_")
student <- relabel(student, "st_")
school <- mxModel(
school,
mxData(school.data, type="raw", primaryKey="id"))
teacher <- mxModel(
teacher, school,
mxData(teacher.data, type="raw", primaryKey="id"),
mxPath('school.C', 'A', free=FALSE, value=1, joinKey="schoolId"))
student <- mxModel(
student, teacher,
mxData(student.data, type="raw", primaryKey="id"),
mxPath('teacher.C', 'A', free=FALSE, value=1, joinKey="teacherId"))
#student$expectation$verbose <- 1L
student$expectation$.rampartCycleLimit <- 0L
pt1 <- mxRun(mxModel(
student,
mxComputeSequence(list(
mxComputeOnce('fitfunction', 'fit'),
mxComputeNumericDeriv(checkGradient=FALSE,
iterations=2, hessian=FALSE),
mxComputeReportDeriv(),
mxComputeReportExpectation()))))
student$expectation$.rampartCycleLimit <- as.integer(NA)
pt2 <- mxRun(mxModel(
student,
mxComputeSequence(list(
mxComputeOnce('fitfunction', 'fit'),
mxComputeNumericDeriv(checkGradient=FALSE,
iterations=2, hessian=FALSE),
mxComputeReportDeriv(),
mxComputeReportExpectation()))))
omxCheckCloseEnough(pt2$expectation$debug$rampartUsage,
c((fanout-1)*fanout^2, (fanout-1)*fanout), 1)
omxCheckCloseEnough(pt2$expectation$debug$numGroups, 3)
if (0) {
layout <- pt2$expectation$debug$layout
head(layout[layout$group==3, ],n=20)
}
omxCheckCloseEnough(pt1$output$fit, pt2$output$fit, 1e-7)
omxCheckCloseEnough(pt1$output$gradient, pt2$output$gradient, 1e-2)
student <- mxRun(student)
if (!more.noise) {
omxCheckCloseEnough(student$output$fit, 1055.161, 1e-2)
} else {
omxCheckCloseEnough(student$output$fit, 1132.713, 1e-2) # but code RED
}
#print(student$expectation$debug$rampartUsage)
if (.Platform$OS.type != 'windows' && parallel::detectCores() > 1) {
omxCheckTrue(student$compute$steps[['GD']]$output$maxThreads > 1)
}
if (0) {
ex <- student$expectation
eo = ex$output
ed = ex$debug
ed$layout
}
got <- mxGenerateData(student)
omxCheckTrue(setequal(names(got), c("school", "teacher", "student")))
omxCheckTrue(setequal(colnames(got[['school']]),
colnames(student$school$data$observed)))
omxCheckTrue(all(got[['school']]$C != student$school$data$observed$C))
omxCheckError(mxGenerateData(student, 10, returnModel=TRUE),
paste("Specification of the number of rows",
"is not supported for relational models"))
got <- mxGenerateData(student, returnModel=TRUE)
omxCheckTrue(is(got, "MxModel"))
omxCheckTrue(all(got$school$data$observed$C != student$school$data$observed$C))
|