mxTryHard {OpenMx} | R Documentation |

## Make multiple attempts to run a model

### Description

Makes multiple attempts to fit an MxModel object with `mxRun()`

until the optimizer yields an acceptable solution or the maximum number of attempts (set by `extraTries`

) is reached. Between attempts, start values are perturbed by random numbers (see details). Optimization-control parameters may also be altered. From among its attempts, the function returns the fitted model with the best fit (smallest fit-function value). If `bestInitsOutput`

is TRUE, then in addition the start values used for the best-fitting model will be printed to the console.

*note*: If the model contains `mxConstraint`

s, or if `mxFitFunctionWLS`

is being used, the Hessian cannot be checked, and so `checkHess`

will be coerced to FALSE.

### Usage

```
mxTryHard(model, extraTries = 10, greenOK = FALSE, loc = 1, scale = 0.25,
initialGradientStepSize = imxAutoOptionValue("Gradient step size"),
initialGradientIterations = imxAutoOptionValue('Gradient iterations'),
initialTolerance=as.numeric(mxOption(NULL,'Optimality tolerance')),
checkHess = TRUE, fit2beat = Inf, paste = TRUE, iterationSummary=FALSE,
bestInitsOutput=TRUE, showInits=FALSE, verbose=0, intervals = FALSE,
finetuneGradient=TRUE, jitterDistrib=c("runif","rnorm","rcauchy"), exhaustive=FALSE,
maxMajorIter=3000, OKstatuscodes, wtgcsv=c("prev","best","initial"), silent=interactive())
mxTryHardOrig(model, finetuneGradient=FALSE, maxMajorIter=NA,
wtgcsv=c("prev","best"), silent=FALSE, ...)
mxTryHardctsem(model, initialGradientStepSize = .00001,
initialGradientIterations = 1,
initialTolerance=1e-12, jitterDistrib="rnorm", ...)
mxTryHardWideSearch(model, finetuneGradient=FALSE, jitterDistrib="rcauchy",
exhaustive=TRUE, wtgcsv="prev", ...)
mxTryHardOrdinal(model, greenOK = TRUE, checkHess = FALSE,
finetuneGradient=FALSE, exhaustive=TRUE,
OKstatuscodes=c(0,1,5,6), wtgcsv=c("prev","best"), ...)
```

### Arguments

`model` |
The MxModel to be run. |

`extraTries` |
The number of attempts to run the model |

`greenOK` |
Logical; is a solution with Mx status GREEN (optimizer status code 1) acceptable? Defaults to |

`loc` , `scale` |
Numeric. The location and scale parameters of the distribution from which random values are drawn to perturb start values between attempts, defaulting respectively to 1 and 0.25. See below, under "Details," for additional information. |

`initialGradientStepSize` , `initialGradientIterations` , `initialTolerance` |
Numeric. Initial values of optimization-control parameters passed to |

`checkHess` |
Logical; is a positive-definite Hessian a requirement for an acceptable solution? Defaults to |

`fit2beat` |
Numeric upper limit to the fitfunction value that an acceptable solution may have. Useful if a nested submodel of |

`paste` |
Logical. If |

`iterationSummary` |
Logical. If |

`bestInitsOutput` |
Logical. If |

`showInits` |
Logical. If |

`verbose` |
If |

`intervals` |
Logical. If TRUE, OpenMx will estimate any specified confidence intervals. |

`finetuneGradient` |
Logical. If |

`jitterDistrib` |
Character string naming which random-number distribution–either uniform (rectangular), normal (Gaussian), or Cauchy–to be used to perturb start values. Defaults to the uniform distribution (for |

`exhaustive` |
Logical. If |

`maxMajorIter` |
Integer; passed to |

`OKstatuscodes` |
Optional integer vector containing optimizer status codes that an acceptable solution is permitted to have. |

`wtgcsv` |
Character vector. "Where to get current start values." See below, under "Details," for additional information. |

`silent` |
Logical; for |

`...` |
Additional arguments to be passed to |

### Details

`mxTryHardOrig()`

, `mxTryHardctsem()`

, `mxTryHardWideSearch()`

, and `mxTryHardOrdinal()`

are wrapper functions to the main workhorse function `mxTryHard()`

. Each wrapper function has default values for certain arguments that are tailored toward a specific purpose. `mxTryHardOrig()`

imitates the functionality of the earliest implementations of `mxTryHard()`

in OpenMx's history; its chief purpose is to find good start values that lead to an acceptable solution. `mxTryHardctsem()`

uses `mxTryHard()`

to "zero in" on an acceptable solution with models that can be difficult to optimize, such as continuous-time state-space models. `mxTryHardWideSearch()`

uses `mxTryHard()`

to search a wide region of the parameter space, in hope of avoiding local fitfunction minima. `mxTryHardOrdinal()`

attempts to use `mxTryHard()`

as well as it can be used with models involving ordinal data.

Argument `wtgcsv`

dictates where `mxTryHard()`

is permitted to find free-parameter values, at the start of each fit attempt after the first, before randomly perturbing them to create the current fit attempt's start values. If `"prev"`

is included, then `mxTryHard()`

is permitted to use the parameter estimates of the most recent non-error fit attempt. If `"best"`

is included, then `mxTryHard()`

is permitted to use the parameter estimates at the best solution so far. If `"initial"`

is included, then `mxTryHard()`

is permitted to use the initial start values in `model`

, as provided by the user. The default is to permit all three, in which case `mxTryHard()`

is written to use the best solution's values if available, and otherwise to use the most recent solution's values, but to periodically revert to the initial values if recent fit attempts have not improved on the best solution.

Once the start values are located for the current fit attempt, they are randomly perturbed before being assigned to the MxModel. The distributional family from which the perturbations are randomly generated is dictated by argument `jitterDistrib`

. The distribution is parameterized by arguments `loc`

and `scale`

, respectively the location and scale parameters. The location parameter is the distribution's median. For the uniform distribution, `scale`

is the absolute difference between its median and extrema (i.e., half the width of the rectangle); for the normal distribution, `scale`

is its standard deviation; and for the Cauchy, `scale`

is one-half its interquartile range. Start values are first multiplied by random draws from a distribution with the provided `loc`

and `scale`

, then added to random draws from a distribution with the same `scale`

but with a median of zero.

### Value

Usually, `mxTryHard()`

returns a post-`mxRun()`

`MxModel`

object. Specifically, this will be the fitted model having the smallest fit-function value found by `mxTryHard()`

during its attempts. The start values used to obtain this fitted model are printed to console if `bestInitsOutput=TRUE`

.

If every attempt at running `model`

fails, `mxTryHard()`

returns an object of class 'try-error'.

`mxTryHard()`

throws a warning if the returned `MxModel`

object has a nonzero status code (unless nonzero status codes are considered acceptable per argument `greenOK`

or `OKstatuscodes`

).

### See Also

### Examples

```
library(OpenMx)
data(demoOneFactor) # load the demoOneFactor dataframe
manifests <- names(demoOneFactor) # set the manifest to the 5 demo variables
latents <- c("G") # define 1 latent variable
model <- mxModel(model="One Factor", type="RAM",
manifestVars = manifests,
latentVars = latents,
mxPath(from=latents, to=manifests, labels=paste("b", 1:5, sep="")),
mxPath(from=manifests, arrows=2, labels=paste("u", 1:5, sep="")),
mxPath(from=latents , arrows=2, free=FALSE, values=1.0),
mxData(cov(demoOneFactor), type="cov", numObs=500)
)
model <- mxTryHard(model) # Run the model, returning the result into model
summary(model) # Show summary of the fitted model
```

*OpenMx*version 2.21.11 Index]