I asked Jeff Laake this question because to "save time" my collaborators and I had split up model runs on several different computers... but didn't think ahead* as to how we would combine all the "mark00#.*" files produced from the model runs.
The problem arises because RMark/Mark will create five files ("*.out", "*.res", "*.vcv", "*.inp", and "*.rda") for every model you run and name them sequentially between models starting at "mark001.*". If you run 10 models on each of three isolated computers, you would end up with three working directories with "mark001.*" through "mark010.*". If you copied over these files to one working directory they would either replace each other or produce a set of "copy1" and "copy2", but in the final workspace you would still not be able to retrieve them in a call to "collect.models()" because you never ran those "imported" models in the final workspace (those model objects don't exist). If you had saved those model objects into your final workspace, you would still not be able to retrieve them properly because the "imported" models, although they may have different object names, would still be associated to outputs "mark001.*" through "mark010.*". A call to "collect.models()" would only retrieve the original 10 models you ran in that workspace.
The solution is to rename the output files in their original file directory and redefine which output files are associated with which models in each workspace (such that you can still retrieve the models with "collect.models()"), and then save them (model objects and output files) to a common workspace and working directory where you will eventually have ALL your models.
Jeff came up with this bit of code to do just that IF you had included "external=TRUE" in your model run call to "mark"
- Code: Select all
# Original model run
mymodel <- mark(data.process, data.ddl, model.parameters=list(), external=TRUE)
# Function to rename "mymodel" output files so they can be merged with
# other mark runs from a different workspace
rename.model=function(x,newname)
{
model=RMark:::load.model(x)
file.rename(paste(model$output,".out",sep=""),paste(newname,".out",sep=""))
file.rename(paste(model$output,".res",sep=""),paste(newname,".res",sep=""))
file.rename(paste(model$output,".vcv",sep=""),paste(newname,".vcv",sep=""))
file.rename(paste(model$output,".inp",sep=""),paste(newname,".inp",sep=""))
model$output=newname
savename=paste(newname,".rda",sep="")
class(savename)=class(x)
save(model,file=savename)
return(savename)
}
# Example use of the "rename.model" function. You can rename the output
# files anything between 001-999… make sure you don't overlap with the
# name of model outputs from the other workspaces you want to merge
mymodel <- rename.model(mymodel,"mark###")
After doing this in your separate, original workspaces/working directories, you need to save renamed model objects into a common workspace and output files into a common working directory. The latter is easy, just copy/paste the models from the original to the final working directory (or email the files as an attachment to your collaborators or whatever). Saving the objects from one workspace to another is not as simple (explained below)... I came up with this part and it is a little sloppy. Suggestions for improvement are welcome.
If you are only merging two workspaces (let's assume a larger, final workspace and a smaller offshoot in which you had renamed the models)…
1) Save a copy of the final workspace -imperative, or you will lose EVERYTHING!-. This can be done in many ways… here is one way.
- Code: Select all
save.image(".RData_copy")
2) Save the renamed models to the final workspace
- Code: Select all
# Save an object to a workspace, if none specified the current working
# directory (cwd) is the default
save(list=c("mymodel1","mymodel2","mymodel3"),file=".RData")
This will basically erase everything that was in the .RData workspace you chose except the models you just saved to it… which is why you need to have saved a copy of that .RData for the next step… reloading the "final workspace".
3) Re-load the final workspace
- Code: Select all
load(".RData_copy")
4) Save the final workspace
- Code: Select all
save.image(".RData")
If you are merging more than two workspaces, repeat steps 1-4 as many times as workspaces you have… please remember to save a copy of your growing "final workspace" as you progress through these steps, you don't want to have to rerun all the models (unless they are on the dipper data which runs in seconds

Hope this is useful!
HOW TO AVOID ALL OF THIS FROM THE BEGINING
*Had we thought about this beforehand, we would have added a prefix to the model output file names so they wouldn't overlap between workspaces.
- Code: Select all
# Add prefix to model output files
mymodel <- mark(data.process, data.ddl, model.parameters=list(), prefix="mymark")
Please remember to use a different prefix for each workspace.