File: TestUseOptimizer.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 (71 lines) | stat: -rw-r--r-- 2,862 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
#
#   Copyright 2007-2019 by the individuals mentioned in the source code history
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
# 
#        http://www.apache.org/licenses/LICENSE-2.0
# 
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

library(OpenMx)
#options(error = utils::recover)

# Define a model
model <- mxModel('model')
model <- mxModel(model, mxMatrix("Full", values = c(0,0.2,0,0), name="A", nrow=2, ncol=2))
model <- mxModel(model, mxMatrix("Symm", values = c(0.8,0,0,0.8), name="S", nrow=2, ncol=2, free=TRUE))
model <- mxModel(model, mxMatrix("Iden", name="F", nrow=2, ncol=2, dimnames = list(c('a','b'), c('a','b'))))

model[["A"]]$free[2,1] <- TRUE
model[["S"]]$free[2,1] <- FALSE
model[["S"]]$free[1,2] <- FALSE
model[["A"]]$labels[2,1] <- "pear"
model[["S"]]$labels[1,1] <- "apple"
model[["S"]]$labels[2,2] <- "banana"

# Bounds must be added after all the free parameters are specified
model <- mxModel(model, mxBounds(c("apple", "banana"), 0.001, NA))

# Define the objective function
objective <- mxExpectationRAM("A", "S", "F")

# Define the observed covariance matrix
covMatrix <- matrix( c(0.77642931, 0.39590663, 0.39590663, 0.49115615), 
	nrow = 2, ncol = 2, byrow = TRUE, dimnames = list(c('a','b'), c('a','b')))

data <- mxData(covMatrix, 'cov', numObs = 100)

# Add the objective function and the data to the model
model <- mxModel(model, objective, data, mxFitFunctionML())

# Run the job
modelOut <- mxRun(model)

expectedParameters <- c(0.5099, 0.7686, 0.2863)

omxCheckCloseEnough(expectedParameters, 
	modelOut$output$estimate, 
	epsilon = 10 ^ -4)

# Run the job that only computes the log-likelihood of 
# the current free parameter values but does not move
# any of the free parameters.

fixedModel <- model
params <- omxGetParameters(fixedModel)
fixedModel <- omxSetParameters(fixedModel, names(params), free = FALSE, name = 'modelFixed')
omxCheckEquals(model$name, "model")
omxCheckEquals(fixedModel$name, "modelFixed")
fixedModelOut <- mxRun(fixedModel)

omxCheckError(mxRun(mxModel(fixedModel, mxComputeGradientDescent())),
	"The job for model 'modelFixed' exited abnormally with the error message: MxComputeGradientDescent: model has no free parameters; You may want to reset your model's compute plan with model$compute <- mxComputeDefault() and try again")

modelUnfitted <- mxRun(model, useOptimizer=FALSE)
omxCheckCloseEnough(mxEval(objective, fixedModelOut), mxEval(objective, modelUnfitted), 0.0001)