-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrapdemo.R
69 lines (54 loc) · 2.16 KB
/
bootstrapdemo.R
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
#
# OpenMx Script to demonstrate use of R's boot package for bootstrapping
#
# Author: M.C. Neale 1 September 2009
#
# Load required libraries
require(OpenMx)
require(boot)
bootdemo <- function(swift=FALSE,R=100) {
# Define a function called mles which will return maximum likelihood estimates
# It uses the demoOneFactor dataset and one factor model on the OpenMx homepage
# http://openmx.psyc.virginia.edu
mles<-function(dataset,wt){
require(OpenMx)
manifests <- names(dataset)
latents <- c("G")
covwt <- cov.wt(dataset,wt)
mlevals <- mxRun(mxModel("One Factor", type="RAM",
manifestVars = manifests,
latentVars = latents,
mxPath(from=latents, to=manifests),
mxPath(from=manifests, arrows=2),
mxPath(from=latents, arrows=2,
free=F, values=1.0),
mxData(covwt$cov, type="cov",
numObs=500)))
return(as.vector(mlevals@output$estimate))}
# Run R bootstraps
boottime = system.time(boot(demoOneFactor,mles,R=R,swift=swift))
cat("done booting - system.time is:\n")
print(boottime)
# For comparison, take a look at the SE output from running the homepage job once
data(demoOneFactor)
manifests <- names(demoOneFactor)
latents <- c("G")
factorModel <- mxModel("One Factor", type="RAM",
manifestVars = manifests,
latentVars = latents,
mxPath(from=latents, to=manifests),
mxPath(from=manifests, arrows=2),
mxPath(from=latents, arrows=2,
free=F, values=1.0),
mxData(cov(demoOneFactor), type="cov",
numObs=500))
facrun<-mxRun(factorModel)
summary(facrun)
# the estimates and standard errors should match up pretty well, though the number of replicates R above might be increased
# therefore, only the factorModel estimates are compared:
loadings<-facrun@matrices$A@values[1:5,6]
errors<-diag(facrun@matrices$S@values[1:5,1:5])
estimates<-as.vector(c(loadings,errors))
omxCheckCloseEnough(as.vector(c(0.3971525,0.5036615,0.5772418,0.7027743,0.7962506,0.04081422,0.03802001,0.04082720,0.03938708,0.03628711)),estimates,.001)
# The above should indicate that the results are close enough.
} # function bootdemo