Learn R Programming

medflex (version 0.6-10)

neImpute.formula: Expand the dataset and impute nested counterfactual outcomes

Description

This function both expands the data along hypothetical exposure values and imputes nested counterfactual outcomes.

Usage

# S3 method for formula
neImpute(
  object,
  family,
  data,
  FUN = glm,
  nMed = 1,
  nRep = 5,
  xSampling = c("quantiles", "random"),
  xFit,
  percLim = c(0.05, 0.95),
  ...
)

Value

A data frame of class c("data.frame", "expData", "impData"). See expData for its structure.

Arguments

object

a formula object providing a symbolic description of the imputation model (see details).

family

a description of the error distribution and link function to be used in the model. Consult the help files of the model-fitting function specified in FUN for more details on appropriate argument specification.

data

data, as matrix or data frame, containing the exposure (and other relevant) variables. Redundant if already specified in call for fitted model specified in object (see details).

FUN

function used to fit model specified in formula.

nMed

number of mediators.

nRep

number of replications or hypothetical values of the exposure to sample for each observation unit.

xSampling

character string indicating how to sample from the conditional exposure distribution. Possible values are "quantiles" or "random" (see details).

xFit

an optional fitted object (preferably glm) for the conditional exposure distribution (see details).

percLim

a numerical vector of the form c(lower, upper) indicating the extreme percentiles to sample when using "quantiles" as sampling method to sample from the conditional exposure distribution (see details).

...

additional arguments (passed to FUN).

Details

Imputed counterfactual outcomes are predictions from the imputation model that is fitted internally by extracting information from the arguments object, family, data, FUN and ....

For imputation model specification via the object argument, use a formula of the form

Y ~ X + M1 + M2 + M3 + C1 + C2,

with the same outcome as in the final natural effect model and with predictor variables entered in the following prespecified order:

  1. exposure X: The first predictor is coded as exposure or treatment.

  2. mediator(s) M: The second predictor is coded as mediator. In case of multiple mediators (nMed > 1), then predictors 2:(nMed + 1) are coded as mediators.

  3. baseline covariates C: All remaining predictor variables are automatically coded as baseline covariates.

It is important to adhere to this prespecified order to enable neImpute to create valid pointers to these different types of predictor variables. This requirement extends to the use of operators different than the + operator, such as the : and * operators (when e.g. adding interaction terms). For instance, the formula specifications Y ~ X * M + C1 + C2, Y ~ X + M + X:M + C1 + C2 and Y ~ X + X:M + M + C1 + C2 will create identical pointers to the different types of variables, as the order of the unique predictor variables is identical in all three specifications.

Furthermore, categorical exposures that are not coded as factors in the original dataset, should be specified as factors in the formula, using the factor function, e.g. Y ~ factor(X) + M + C1 + C2. Quadratic or higher-order polynomial terms can be included as well, by making use of the I function or by using the poly function. For instance, Y ~ X + I(X^2) + M + C1 + C2 and Y ~ poly(X, 2, raw = TRUE) + M + C1 + C2 are equivalent and result in identical pointers to the different types of variables.

The command terms(object, "vartype") (with object replaced by the name of the resulting expanded dataset) can be used to check whether valid pointers have been created.

If multiple mediators are specified (nMed > 1), the natural indirect effect parameter in the natural effect model captures the joint mediated effect. That is, the effect of the exposure on the outcome via these mediators considered jointly. The remaining effect of the exposure on the outcome (not mediated through the specified mediators) is then captured by the natural indirect effect parameter.

The type of imputation model can be defined by specifying an appropriate model-fitting function via the FUN argument (its default is glm). This method can only be used with model-fitting functions that require a formula argument (so not when using e.g. SuperLearner).

In contrast to imputation models with categorical exposures, additional arguments need to be specified if the exposure is continuous. All of these additional arguments are related to the sampling procedure for the exposure.

Whereas the number of replications nRep for categorical variables equals the number of levels for the exposure coded as a factor (i.e. the number of hypothetical exposure values), the number of desired replications needs to be specified explicitly for continuous exposures. Its default is 5.

If xFit is left unspecified, the hypothetical exposure levels are automatically sampled from a linear model for the exposure, conditional on a linear combination of all covariates. If one wishes to use another model for the exposure, this default model specification can be overruled by referring to a fitted model object in the xFit argument. Misspecification of this sampling model does not induce bias in the estimated coefficients and standard errors of the natural effect model.

The xSampling argument allows to specify how the hypothetical exposure levels should be sampled from the conditional exposure distribution (which is either entered explicitly using the xFit argument or fitted automatically as described in the previous paragraph). The "random" option randomly samples nRep draws from the exposure distribution, whereas the "quantiles" option (default) samples nRep quantiles at equal-sized probability intervals. Only the latter hence yields fixed exposure levels given nRep and xFit.

In order to guarantee that the entire support of the distribution is being sampled (which might be a concern if nRep is chosen to be small), the default lower and upper sampled quantiles are the 5th and 95th percentiles. The intermittent quantiles correspond to equal-sized probability intervals. So, for instance, if nRep = 4, then the sampled quantiles will correspond to probabilities 0.05, 0.35, 0.65 and 0.95. These default 'outer' quantiles can be changed by specifying the percLim argument accordingly. By specifying percLim = NULL, the standard quantiles will be sampled (e.g., 0.2, 0.4, 0.6 and 0.8 if nRep = 4).

See Also

neImpute, neImpute.default, neModel, expData

Examples

Run this code
data(UPBdata)

## example using glm imputation model with binary exposure
impData <- neImpute(UPB ~ factor(attbin) + negaff + gender + educ + age, 
                    family = binomial, data = UPBdata)
head(impData)

## example using glm imputation model with continuous exposure
impData <- neImpute(UPB ~ att + negaff + gender + educ + age, 
                    family = binomial, data = UPBdata, nRep = 2)
head(impData)

## example using vglm (yielding identical results as with glm)
library(VGAM)
impData2 <- neImpute(UPB ~ att + negaff + gender + educ + age, 
                     family = binomialff, data = UPBdata, 
                     nRep = 2, FUN = vglm)
head(impData2)

Run the code above in your browser using DataLab