You are here

Running models multiple times with different data or parameters

Primary tabs

R allows you to create loops so that you can do things such as read in files one at a time and run a model on each of them.

For a given model such as myModel below, these tasks involve running the model repeatedly with different data or settings, and storing some of the output.

One approach is to define a model, and an object to store the results:

myModel <- mxModel( blah blah blah ) # define a FIML model but don't put any data in it.
myParameters <- matrix(NA, 100, 5) # suppose 100 files and you want to save 5 parameters from each run

Then simply write a for-loop in R to call the model repeatedly

for (i in 1:100) {
    tempFileName <- paste("myFile",i,".dat", sep=0) # Suppose files name myFile1.dat, myFile2.dat, etc.
    tempData <- read.table(tempFileName)  # Options to read.table would need to be set for your case.
    tempResults <- mxRun(mxModel(myModel, mxData(tempData, type="raw")) #run OpenMx on one file
    myParameters[i,] <- mxEval(A, tempResults)[1:5,6] #Suppose the parameters are in rows 1-5 of col 6 of A

After running this, the matrix, myParameters, contains just the parameters you wanted after 100 runs of OpenMx.

Multiple imputation and other resampling ideas

R has several helpful packages supporting this type of processing including 'pan', 'kmi', 'mitools', and 'MICE' . Search for "multiple imputation" on under the "packages" link