File: test-gendata-multilevel.R

package info (click to toggle)
r-cran-openmx 2.21.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,412 kB
  • sloc: cpp: 36,577; ansic: 13,811; fortran: 2,001; sh: 1,440; python: 350; perl: 21; makefile: 5
file content (40 lines) | stat: -rw-r--r-- 1,119 bytes parent folder | download | duplicates (3)
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
library(OpenMx)
library(testthat)
context("gendata-multilevel")

suppressWarnings(RNGversion("3.5"))
set.seed(1)

df <- NULL
for (batch in 1:50) {
  df <- rbind(df, expand.grid(case=1:(5+sample.int(4, 1)), Batch=batch, Yield=0))
}

batch <- mxModel(
    'batch', type="RAM",
    latentVars = c('batch'),
    mxData(data.frame(batch=unique(df$Batch)), 'raw', primaryKey='batch'),
    mxPath('batch', arrows=2, values=.75, lbound=.001))

trueYield <- mxModel(
    'yield', type='RAM', batch,
    manifestVars = c('Yield'),
    mxData(df, 'raw'),
    mxPath('one', 'Yield', values=1e-6),
    mxPath('Yield', arrows=2, values=1),
    mxPath('batch.batch', 'Yield', free=FALSE, values=1, joinKey="Batch"))

result <- expand.grid(rep=1:5)
for (px in names(coef(trueYield))) result[[px]] <- NA
result$rep <- NULL

for (rep in 1:nrow(result)) {
  yield <- mxGenerateData(trueYield, returnModel = TRUE)
  
  yield <- mxRun(yield, silent = TRUE)
  
  result[rep, names(coef(yield))] <- coef(yield)
}

omxCheckCloseEnough(colMeans(result) - coef(trueYield), rep(0,3), .1)
omxCheckCloseEnough(apply(result, 2, var), rep(0,3), .03)