estweight: Estimate propensity weights for biased samples

View source: R/estweight.R

estweightR Documentation

Estimate propensity weights for biased samples

Description

Estimate propensity weights for biased samples using information from a representative sample.

Usage

estweight(
  data,
  weight_model = "logistic",
  incl_wts_for_rep = FALSE,
  return_model_fit = FALSE
)

Arguments

data

Combined dataset including the convenience sample and representative sample with a subject identifier, ID, in the first column, any covariates needed for matching, and an indicator, biased, that is 1 for subjects in the biased sample in the last column. Note that all factors in the dataset should either be binary or of class factor

weight_model

Which propensity weight estimation model should be used. Either "logistic", "randomForest", "CBPS", or "entbal"

incl_wts_for_rep

A logical value indicating whether propensity weights for observations from the representative sample should be included in the output.

return_model_fit

An optional flag which changes the output so that it returns a list of estimated propensity weights and the model fit. This is useful for downstream uncertainty estimates. Note that a model fit is only returned if a logistic propensity weight estimation model is selected (weight_model == "logistic")

Value

estweight returns a dataframe containing IDs and the corresponding estimated propensity weights. If the flag return_model_fit == TRUE and the weight_model == "logistic", it instead returns a list where the first element is the dataframe of IDS and weights and the second element is the model fit.

Examples

#' data("mtcars")
repsample = mtcars
n = nrow(repsample)
expit = function(x) {exp(x) / (1 + exp(x))}

# Calculate probability of being oversampled
repsample$sampprob = expit(.01*(repsample$am*4 + repsample$carb*3
+ repsample$drat*.9 -repsample$mpg*repsample$disp*.05 + .002*repsample$hp^2 + 80))

# draw biased and representative samples
b.samp = repsample[sample(1:n, 500, prob = repsample$sampprob, replace = TRUE), ]
r.samp = repsample[sample(1:n, 500, replace = TRUE), ]

# Create indicator of biased sample membership
b.samp$biased = 1; r.samp$biased = 0

# Format data to pass to function
Xcomb = data.frame(ID = 1:(1000), rbind(b.samp, r.samp))
Xfit = Xcomb[,c("ID", colnames(Xcomb)[c(2:8,10:12)], "biased")]

# Estimate propensity weights
estweight(data = Xfit, weight_model = "logistic")

oliviabern/estweight documentation built on Oct. 2, 2022, 7:50 p.m.