01missing_variable {mi} | R Documentation |
Class "missing_variable" and Inherited Classes
Description
The missing_variable class is essentially the data comprising a variable plus all
the metadata needed to understand how its missing values will be imputed. However, no variable is
merely of missing_variable class; rather every variable is of a class that inherits from the
missing_variable class. Even if a variable has no missing values, it needs to be coerced to a class
that inherits from the missing_variable class before it can be used to impute values of other
missing_variables. Understanding the properties of different subclasses of the missing_variable class
is essential for modeling and imputing them. The missing_data.frame-class
is essentially
a list of objects that inherit from the missing_variable class, plus metadata need to understand how
these missing_variables relate to each other. Most users will never need to call missing_variable
directly since it is called by missing_data.frame
.
Usage
missing_variable(y, type, ...)
## Hidden arugments not included in the signature:
## favor_ordered = TRUE, favor_positive = FALSE,
## variable_name = deparse(substitute(y))
Arguments
y |
Can be any vector, some of whose values may be |
type |
Missing or a character string among the classes that inherit from the missing_variable
class. If missing, the constructor will guess (sometimes incorrectly) based on the characteristics
of the variable. The best way to improve the guessing of categorical variables is to
use the |
... |
Further hidden arguments that are not in the signature. The |
Value
The missing_variable function returns an object that inherits from the missing_variable class.
Objects from the Classes
The missing_variable class is virtual, so no objects
may be created from it. However, the missing_variable generic function can be used to
instantiate an object that inherits from the missing_variable class by specifying its
type
argument. A user would call the missing_data.frame
function on a data.frame
, which in turn calls the missing_variable function
on each column of the data.frame
using various heuristics to guess the
type
argument.
Slots
In the following table, indentation indicates inheritance from the class with less indentation, and
italics indicates that the class is virtual so no variables can be created with that class. Inherited
classes inherit the transformations, families, link functions, and fit_model-methods
from their parent class, although these are often superceeded by analogues that are tailored for the
inherited class. Also note, the default transformation for the continuous class is a standardization
using twice the standard deviation of the observed values.
The distinction between the transformation entailed by the family
and the transformation
entailed by the function in the tranformation slot may be confusing at this point. The former pertains
to how the linear predictor of a variable is mapped to the space of a variable when it is on the left-hand
side of a generalized linear model. The latter pertains — for continuous variables only — to how the
values in the raw_data slot are mapped into those in the data and thus affects how a continuous
variable enters into the model whether it is on the left or right-hand side. The classes are discussed in
much more detail below.
Class name [transformation] | Default family and link | Default fit_model |
missing_variable | none | throws error |
categorical | none | throws error |
unordered-categorical | binomial(link = 'logit') | multinom |
ordered-categorical | binomial(link = 'logit') | bayespolr |
binary | binomial(link = 'logit') | bayesglm |
interval | gaussian{link = 'identity'} | survreg |
continuous[standardize] | gaussian{link = 'identity'} | bayesglm |
semi-continuous[identity] | ||
nonnegative-continuous[logshift] | ||
SC_proportion[squeeze] | binomial(link = 'logit') | betareg |
positive-continuous[log ] | ||
proportion[identity] | binomial(link = 'logit') | betareg |
bounded-continuous[identity] | ||
count | quasipoisson{link = 'log'} | bayesglm |
irrelevant | throws error | |
fixed | throws error | |
The missing_variable class is virtual and has the following slots (this information is primarily directed at developeRs):
variable_name
:Object of class
character
of length one naming the variableraw_data
:Object of class
"ANY"
representing the observations on a variable, some of which may beNA
. No method should ever change this slot at all. Instead, methods should change the data slot.data
:Object of class
"ANY"
, which is initially a copy of the raw_data slot — transformed by the function in the transformation slot for continuous variables only — and whoseNA
values are replaced during the multiple imputation process. Seemi
n_total
:Object of class
"integer"
which is thelength
of the data slotall_obs
:Object of class
"logical"
of length one indicating whether all values of the data slot are observed and thus notNA
n_obs
:Object of class
"integer"
of length one indicating the number of values of the data slot that are observed and thus notNA
which_obs
:Object of class
"integer"
, which is a vector indicating the positions of the observed values in the data slotall_miss
:Object of class
"logical"
of length one indicating whether all values of the data slot areNA
n_miss
:Object of class
"integer"
of length one indicating the number of values of the data slot that areNA
which_miss
:Object of class
"integer"
, which is a vector indicating the positions of the missing values in the data slotn_extra
:Object of class
"integer"
of length one indicating how many (missing) observations have been added to the end of the data slot that are not included in the raw_data slot. Although the extra values will be imputed, they are not considered to be “missing” for the purposes of defining the previous three slotswhich_extra
:Object of class
"integer"
, which is a vector indicating the positions of the extra values at the end of the data slotn_unpossible
:Object of class
"integer"
of length one indicating the number of values that are logically or structurally unobservablewhich_unpossible
:Object of class
"integer"
indicating the positions of the unpossible values in the data slotn_drawn
:Object of class
"integer"
of length one which is the sum of the n_miss and n_extra slotswhich_drawn
:Object of class
"integer"
which is a vector concatinating the which_miss and which_extra slotsimputation_method
:Object of class
"character"
of length one indicating how theNA
values are to be imputed. Possibilities include “ppd” for imputation from the posterior predictive distribution, “pmm” for imputation via predictive mean matching, “mean” for mean-imputation, “median” for median-imputation, “expectation” for conditional mean-imputation. With enough programming effort, other kinds of imputation can be defined and specified here.family
:Object of class
"WeAreFamily"
that will typically be passed toglm
and similar functions during the multiple imputation processknown_families
:Object of class
character
indicating the families that are known to be supported for a class; seefamily
known_links
:Object of class
character
indicating what link functions are known to be supported by the elements of the known_families slot; seefamily
imputations
:Object of class
"MatrixTypeThing"
with rows equal to the number of iterations (initially zero) of the multiple imputation algorithm and columns equal to the n_drawn slot. The rows are appropriately extended and then filled by themi
functiondone
:Object of class
"logical"
of length one indicating whether theNA
values in the data slot have been replaced by imputed valuesparameters
:Object of class
"MatrixTypeThing"
with rows equal to the number of iterations (initially zero) of the multiple imputation algorithm and columns equal to the number of estimated parameters when modeling the data slot. The rows are appropriately extended and then filled by themi
functionmodel
:Object of class
"ANY"
which can be filled by an object that is output by one of thefit_model-methods
, which is done by default bymi
when all the iterations have completedfitted
:Object of class
"ANY"
although typically a vector or matrix that contains the fitted values of the model in the slot immediately above. Note that the fitted slot is filled by default bymi
, although the model slot is left empty by default to save RAM.estimator
:Object of class
"character"
of length one indicating which pre-existingfit_model
to use for an unordered-categorical variable. Options are"mnl"
, in whichmultinom
from the nnet package is used to fit the values of the unordered categorical variable; and"rnl"
, in which each category is separately modeled as the positive binary outcome against all other categories using abayesglm
fit_model
and the probabilities of each category are normalized to sum to 1 after each model is run. In general,"rnl"
is slightly less accurate than"mnl"
, but runs much more quickly especially when the unordered categorical variable has many unique categories.
The WeAreFamily class is a class union of character
and family
, while the
MatrixTypeThing class is a class union of matrix
only at the moment.
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
missing_data.frame
, categorical-class
, unordered-categorical-class
,
ordered-categorical-class
, binary-class
, interval-class
,
continuous-class
, semi-continuous-class
, nonnegative-continuous-class
,
SC_proportion-class
, censored-continuous-class
,
truncated-continuous-class
, bounded-continuous-class
,
positive-continuous-class
, proportion-class
, count-class
Examples
# STEP 0: GET DATA
data(nlsyV, package = "mi")
# STEP 0.5 CREATE A missing_variable (you never need to actually do this)
income <- missing_variable(nlsyV$income, type = "continuous")
show(income)
# STEP 1: CONVERT IT TO A missing_data.frame
mdf <- missing_data.frame(nlsyV) # this calls missing_variable() internally
show(mdf)