Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion R/build_run_modify.R
Original file line number Diff line number Diff line change
Expand Up @@ -3785,7 +3785,6 @@ umxAlgebra <- function(name = NA, expression, dimnames = NA, ..., joinKey=as.cha
#'
# type = c("Auto", "FIML", "cov", "cor", "WLS", "DWLS", "ULS"),
umxRun <- function(model, tryHard = c( "yes", "no", "ordinal", "search"), calc_sat = TRUE, setValues = FALSE, setLabels = FALSE, summary = !umx_set_silent(silent = TRUE), intervals = FALSE, optimizer = NULL, comparison = NULL){
# TODO: umxRun: Return change in -2LL for models being re-run
# TODO: umxRun: Stash saturated model for re-use
# TODO: umxRun: Optimise for speed
tryHard = match.arg(tryHard)
Expand All @@ -3803,8 +3802,25 @@ umxRun <- function(model, tryHard = c( "yes", "no", "ordinal", "search"), calc_s
if(setValues){
model = xmuValues(model)
}

# Check if the model has been run
start2LL = if(!is.null(model$output)) model$output$Minus2LogLikelihood else NULL

model = xmu_safe_run_summary(model, autoRun = TRUE, summary = summary, tryHard = tryHard)

if(!is.null(start2LL)){
# 1. Was run
# 2. Has a previous -2LL
end2LL = model$output$Minus2LogLikelihood
if(!is.null(end2LL)){
# 3. Has a new -2LL
model@output$changeLL = end2LL - start2LL
if(summary){
message("Change in -2LL = ", round(model@output$changeLL, 3))
}
}
}

if(calc_sat){
if(umx_is_RAM(model)){
if(model$data$type == "raw"){
Expand Down
39 changes: 39 additions & 0 deletions repro_umxRun_change.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
library(umx)
data(demoOneFactor)
latents = c("G")
manifests = names(demoOneFactor)
m1 = mxModel("fact", type="RAM", manifestVars=manifests, latentVars=latents,
mxPath(latents , to = manifests),
mxPath(manifests, arrows = 2),
mxPath(latents , arrows = 2, free = FALSE, values = 1),
mxData(cov(demoOneFactor), type = "cov", numObs=500)
)
m1 = umxRun(m1)
# Initial run, start2LL should be NULL, so no message about change.

# Modify model to make it worse/better
# umxModify calls xmu_safe_run_summary internally? Yes.
# But let's use umxRun on a run model.
m1_rerun = umxRun(m1)
# Should see "Change in -2LL = 0" (or close to 0)

# Modify manually to force a change
m3 = m1
m3$S$values[1,1] = 0.5 # Change start value
m3 = umxRun(m3)
# m3 has output from m1 (it is a copy). So start2LL is m1's fit.
# New run will find optimum (which should be same as m1 if only start value changed and convex).
# So change 0.

# What if we fix a parameter?
m4 = omxSetParameters(m1, labels="G_to_x1", free=FALSE, values=.1)
# m4 still has m1's output? Yes, omxSetParameters preserves output slot?
# Let's assume yes.
m4 = umxRun(m4)
# Should show large change.
if(!is.null(m4$output$changeLL)){
message("Test passed: changeLL found in output")
print(m4$output$changeLL)
} else {
message("Test failed: changeLL not found in output")
}
Loading