File: Power1.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 (75 lines) | stat: -rw-r--r-- 2,661 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
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
library(OpenMx)
library(testthat)

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

data(demoOneFactor)
manifests <- names(demoOneFactor)
latents <- c("G")
factorModel <- mxModel("One Factor",
                       type="RAM",
                       manifestVars = manifests,
                       latentVars = latents,
                       mxPath(from=latents, to=manifests, values=0.8,
                              labels=paste0("loading", 1:length(manifests))),
                       mxPath(from=manifests, arrows=2,values=1),
                       mxPath(from=latents, arrows=2,
                              free=FALSE, values=1.0),
                       mxPath(from="one", to=manifests),
                       mxData(demoOneFactor, type="raw"))
factorModelFit <- mxRun(factorModel)

indModel <- factorModelFit
indModel$A$values['x1','G'] <- 0.3
indModel$A$free['x1','G'] <- FALSE
indModel <- mxRun(indModel)

p1 <- mxPower(factorModelFit, indModel, method = 'ncp', sig.level = .01)
expect_equivalent(c(p1), 118)
p2 <- mxPower(factorModelFit, indModel, method = 'ncp', sig.level = .005)
expect_equivalent(c(p2), 132)
expect_error(mxPower(factorModelFit, indModel, method = 'ncp',
                     sig.level = .5),
             "Try method='empirical'")

got4 <- mxPowerSearch(factorModelFit, indModel, method = 'ncp')
omxCheckCloseEnough(got4[findInterval(.8, got4$power), 'N'], 73)

# only 1 p-value handled at a time
expect_error(mxPower(factorModelFit, indModel, method = 'ncp', sig.level=c(.05,.01)),
	     "one sig.level at a time")

# more than 1 power request is fine
p3 <- mxPower(factorModelFit, indModel, method = 'ncp', sig.level = .05,power=c(.5,.8))
expect_equivalent(p3, c(44,80))

set.seed(1)

got <- mxPowerSearch(factorModelFit, indModel)
m1 <- attr(got,'model')

got <- mxPowerSearch(factorModelFit, indModel, previousRun = got,
               grid=seq(15,150,length.out = 20))
m2 <- attr(got,'model')
expect_equal(coef(m1), coef(m2), scale=1, tolerance=1e-14)

got2 <- mxPowerSearch(factorModelFit, indModel, method='ncp',
                grid=seq(15,150,length.out = 20))

omxCheckCloseEnough(c(pmin(got2[,'power'] - got[,'lower'], 0),
                      pmin(got[,'upper'] - got2[,'power'], 0)),
                    rep(0,40), .05)

# --------------------

indModel <- factorModelFit
indModel$A$values['x1','G'] <- .1
indModel$A$free['x1','G'] <- FALSE
indModel <- mxRun(indModel)

got <- mxPowerSearch(factorModelFit, indModel, probes = 50, n=100)
got <- mxPowerSearch(factorModelFit, indModel, n=100, previousRun = got)
omxCheckCloseEnough(got[findInterval(.8, got$power), 'loading1'],
                    .16, .01)