Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

fit_model

Wrappers To Fit a Model


Description

The methods are called by the mi function to model a given missing_variable as a function of all the other missing_variables and also their missingness pattern. By overwriting these methods, users can change the way a missing_variable is modeled for the purposes of imputing its missing values. See also the table in missing_variable.

Usage

fit_model(y, data, ...)

Arguments

y

An object that inherits from missing_variable-class or missing

data

A missing_data.frame

...

Additional arguments, not currently utilized

Details

In mi, each missing_variable is modeled as a function of all the other missing_variables plus their missingness pattern. The fit_model methods are typically short wrappers around a statistical model fitting function and return the estimated model. The model is then passed to one of the mi-methods to impute the missing values of that missing_variable.

Users can easily overwrite these methods to estimate a different model, such as wrapping glm instead of bayesglm. See the source code for examples, but the basic outline is to first extract the X slot of the missing_data.frame, then drop some of its columns using the index slot of the missing_data.frame, next pass the result along with the data slot of y to a statistical fitting function, and finally returned the appropriately classed result (along with the subset of X used in the model).

Many of the optional arguments to a statistical fitting function can be specified using the slots of y (e.g. its family slot) or the slots of data (e.g. its weights slot).

The exception is the method where y is missing, which is used internally by mi, and should not be overwritten unless great care is taken to understand its role.

Value

If y is missing, then the modified missing_data.frame passed to data is returned. Otherwise, the estimated model is returned as a classed list object.

Author(s)

Ben Goodrich and Jonathan Kropko, for this version, based on earlier versions written by Yu-Sung Su, Masanao Yajima, Maria Grazia Pittau, Jennifer Hill, and Andrew Gelman.

See Also

Examples

getMethod("fit_model", signature(y = "binary", data = "missing_data.frame"))
setMethod("fit_model", signature(y = "binary", data = "missing_data.frame"), def =
function(y, data, ...) {
  to_drop <- data@index[[y@variable_name]]
  X <- data@X[, -to_drop]
  start <- NULL
  # using glm.fit() instead of bayesglm.fit()
  out <- glm.fit(X, y@data, weights = data@weights[[y@variable_name]], start = start, 
                 family = y@family, Warning = FALSE, ...)
  out$x <- X
  class(out) <- c("glm", "lm") # not "bayesglm" class anymore
  return(out)
})
## Not run: 
if(!exists("imputations", env = .GlobalEnv)) {
  imputations <- mi:::imputations # cached from example("mi-package")
}
imputations <- mi(imputations) # will use new fit_model() method for binary variables

## End(Not run)

mi

Missing Data Imputation and Model Checking

v1.0
GPL (>= 2)
Authors
Andrew Gelman [ctb], Jennifer Hill [ctb], Yu-Sung Su [aut], Masanao Yajima [ctb], Maria Pittau [ctb], Ben Goodrich [cre, aut], Yajuan Si [ctb], Jon Kropko [aut]
Initial release
2015-04-16

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.