File: RawCov.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 (34 lines) | stat: -rw-r--r-- 1,241 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
library(OpenMx)

data(demoOneFactor)
manifests <- names(demoOneFactor)
latents <- c("G")

template <- mxModel(
    "template", type="RAM",
    manifestVars = manifests,
    latentVars = latents,
    mxPath(from=latents, to=manifests, values=rnorm(length(manifests))),
    mxPath(from=manifests, arrows=2, values=rlnorm(length(manifests))),
    mxPath(from=latents, arrows=2, free=FALSE, values=1.0),
    mxPath(from = 'one', to = manifests, values=rnorm(length(manifests))))

factorRaw <- mxModel(template, name="OneFactorRaw",
		     mxData(demoOneFactor, type="raw"))

factorCov <- mxModel(template, name="OneFactorCov", 
		     mxData(observed=cov(demoOneFactor), means=colMeans(demoOneFactor),
			    type="cov", numObs=nrow(demoOneFactor)))

plan <- mxComputeSequence(list(
    mxComputeOnce('fitfunction', 'fit'),
    mxComputeNumericDeriv(checkGradient=FALSE, hessian=FALSE, iterations=2),
    mxComputeReportDeriv(),
    mxComputeReportExpectation()
))

factorRaw <- mxRun(mxModel(factorRaw, plan))
factorCov <- mxRun(mxModel(factorCov, plan))

omxCheckCloseEnough(factorRaw$output$fit, factorCov$output$fit + prod(dim(demoOneFactor))*log(2*pi), 1e-10)
omxCheckCloseEnough(factorRaw$output$gradient, factorCov$output$gradient, 1e-5)