THard=-n

6 replies [Last post]
HanOud's picture
Offline
Joined: 12/20/2009

I have had good experience with the Mx option THard=-n in Mx, also called Automatic Cold Restart. Is this option still available in OpenMx and, if not, why is it taken out?
Thanks for any answer in advance,
Han Oud

mspiegel's picture
Offline
Joined: 07/31/2009
So help the discussion,

So help the discussion, here's the description of Mx option THard=-n:

During optimization, an estimate of the covariance matrix of the estimated parameters is constructed. Sometimes, this covariance matrix is inaccurate and optimization fails to converge to the correct solution, a problem that is usually flagged by an IFAIL parameter of 6. Option TH=-n can be used to restart the optimization n times from the current solution, but with the parameter covariance matrix reset to zero.

Can this functionality be replicated in R? Somebody more knowledgeable than I will hopefully answer this question.

tbrick's picture
Offline
Joined: 07/31/2009
You can replicate THard by re-running the fitted model

You can replicate THard by re-running the fitted model, if I understand the THard argument right (Mike N., correct me if I'm wrong on that). We start cold every time you re-run the model, so it should automatically reset the estimated covariance of the parameters.

A helper function can replicate this effect by just calling mxRun repeatedly on the model. Admittedly, there's some overhead involved in doing so, since the model has to be re-flattened and reprocessed by the back-end. If we want a full implementation of this, it should be done in the back-end for the sake of performance. We'll put the feature on the list and you can use the helper in the meantime.

The helper function would be something like this (sorry it's messy--this is off the top of my head).

tryHard <- function(model, n, ...) {
fit <- mxRun(model)
while(fit@output$status[[1]] == 6 & n > 2) {
print(paste("Trying hard...", n-1, "more times."))
fit <- mxRun(fit)
n <- n - 1
}
fit <- mxRun(fit, ...)
return(fit)
}

The last re-run includes the original arguments, so it will do things like calculate confidence intervals, etc.
There are a few cases where it'll run an extra time to calculate CIs, but if it's already converged, the last run should be quick.

HanOud's picture
Offline
Joined: 12/20/2009
trying tryHard

Thanks, Timothy, for helping with tryHard. I had a try but did not get any relevant output. Did I do something wrong? See below input and output.

tryHard <- function(model,n){
+ fit<-mxRun(model)
+ while(fit@output$status[[1]]==6 & n>2){
+ print(paste("Trying hard...",n-1, "more times."))
+ fit<-mxRun(fit)
+ summary(fit)
+ n <- n-1
+ }
+ return(fit)
+ }
>
> model<-LatDepModelUn
> n<-10
> tryHard(model,n)
Running LatDepModelUn
MxModel 'LatDepModelUn'
type : default
@matrices : 'A', 'S', 'F', and 'M'
@algebras :
@constraints :
@intervals :
@latentVars : none
@manifestVars : none
@data : 49 x 10
@data means : NA
@data type: 'raw'
@submodels :
@objective : MxRAMObjective
@independent : FALSE
@options :
@output : TRUE
>

tbates's picture
Offline
Joined: 07/31/2009
it worked, just need to put the output into a model for summary

try this instead of your tryHard line

LatDepModelUn <- tryHard(LatDepModelUn,10)
summary(LatDepModelUn)

HanOud's picture
Offline
Joined: 12/20/2009
trying tryHard

Thanks Timothy and Timothy. It seems to work. Best, Han

Ryne's picture
Offline
Joined: 07/31/2009
Presumably you'd want to turn

Presumably you'd want to turn off the numerical Hessian for all iterations but the last, as there's no need for that overhead.