Previous: Making the Model Compatible Up: Making the Model Compatible Next: To Work with setx()

To Work with zelig()

Zelig has implemented a unique method for incorporating new models which lets contributors test their models within the Zelig framework without any modification of the zelig() function itself.

Using a wrapper function zelig2contrib() (where contrib is the name of your new model), zelig2contrib() redefines the inputs to zelig() to work with the inputs you need for your function contrib(). For example, if you type

zelig(..., model = "normal.regression")
zelig() looks for a zelig2normal.regression() wrapper in any environment (either attached libraries or your workspace). If the wrapper exists, then zelig() runs the model.

If you have a pre-existing model, writing a zelig2contrib() function is quite easy. Let's say that your model is contrib(), and takes the following arguments: formula, data, weights, and start. The zelig() function, in contrast, only takes the formula, data, model, and by arguments. You may use the ... to pass additional arguments from zelig() to zelig2contrib(), and <- NULL to omit the elements you do not need. Continuing the Normal regression example from Section [*], let formula, model, and data be the inputs to zelig(), M is the number of subsets, and ... are the additional arguments not defined in the zelig() call, but passed to normal.regression().

zelig2normal.regression <- function(formula, model, data, M, ...) {
  mf <- match.call(expand.dots = TRUE)                     # [1]
  mf$model <- mf$M <- NULL                                 # [2]
  mf[[1]] <- as.name("normal.regression")                  # [3]
  as.call(mf)                                              # [4] 
}
The bracketed numbers above correspond to the comments below:
  1. Create a call (an expression to be evaluated) by creating a list of the arguments in zelig2normal.regression(), including the extra arguments taken by normal.regression(), but not by zelig(). All wrappers must take the same standardized arguments ( formula, model, data, and M), which may be used in the wrapper function to manipulate the zelig() call into the normal.regression() call. Additional arguments to normal.regression(), such as start.val are passed implicitly from zelig() using the ... operator.

  2. Erase extraneous information from the call object mf. In this wrapper, model and M are not used. In other models, these are used to further manipulate the call, and so are included in the standard inputs to all wrappers.

  3. Reassign the first element of the call (currently zelig2normal.regression) with the name of the function to be evaluated, normal.regression().

  4. Return the call to zelig(), which will evaluate the call for each multiply-imputed data set, each subset defined in by, or simply data.

If you use an S4 class to represent your model, say mymodel, within zelig.default(), Zelig's internal function, create.ZeligS4(), automatically creates a new S4 class called ZeligS4mymodel in the global environment with two additional slots. These include zelig, which stores the name of the model, and zelig.data, which stores the data frame if save.data=TRUE and is empty otherwise. These names are taken from the original call. This new output inherits the original class mymodel so all the generic functions associated with mymodel should still work. If you would like to see an example, see the models implemented using the VGAM package, such as multinomial probit.



Gary King 2011-11-29