Skip to contents

Checks whether proposed target population means values for targets are suitable in number and order for submission to optweight and optweight.svy. Users should include one value per variable in formula. For factor variables, one value per level of the variable is required. The output of check.targets can also be used as an input to targets in optweight and optweight.svy.

Usage

check.targets(formula,
           data = NULL,
           targets,
           stop = FALSE)

# S3 method for optweight.targets
print(x, digits = 5, ...)

Arguments

formula

A formula with the covariates to be balanced with optweight on the right hand side. See glm for more details. Interactions and functions of covariates are allowed.

data

An optional data set in the form of a data frame that contains the variables in formula.

targets

A vector of target population means values for each covariate. These should be in the order corresponding to the order of the corresponding variable in formula, except for interactions, which will appear after all lower-order terms. For factor variables, a target value must be specified for each level of the factor, and these values must add up to 1. If empty, the current sample means will be produced. If NULL, an NA vector named with the covariate names will be produced.

stop

logical; if TRUE, an error will be thrown if the number of values in targets is not equal to the correct number of (expanded) covariates in formula, and no messages will be displayed if the targets input is satisfactory. If FALSE, a message will be displayed if the number of values in targets is not equal to the correct number of covariates in formula, and other messages will be displayed.

x

An optweight.targets object; the output of a call to check.targets.

digits

How many digits to print.

...

Ignored.

Value

An optweight.targets object, which is a named vector of target population mean values, one for each (expanded) covariate specified in formula. This should be used as user inputs to optweight and optweight.svy.

Details

The purpose of check.targets is to allow users to ensure that their proposed input to targets in optweight and optweight.svy is correct both in the number of entries and their order. This is especially important when factor variables and interactions are included in the formula because factor variables are split into several dummies and interactions are moved to the end of the variable list, both of which can cause some confusion and potential error when entering targets values.

Factor variables are internally split into a dummy variable for each level, so the user must specify a target population mean value for each level of the factor. These must add up to 1, and an error will be displayed if they do not. These values represent the proposrtion of units in the target population with each factor level.

Interactions (e.g., a:b or a*b in the formula input) are always sent to the end of the variable list even if they are specified elsewhere in the formula. It is important to run check.targets to ensure the order of the proposed targets corresponds to the represented order of covariates used in the formula. You can run check.targets with targets = NULL to see the order of covariates that is required without specifying any targets.

Author

Noah Greifer

See also

Examples

library("cobalt")
#>  cobalt (Version 4.4.0, Build Date: 2022-08-13)
data("lalonde", package = "cobalt")

#Checking if the correct number of entries are included:
check.targets(treat ~ age + race + married +
                nodegree + re74,
                data = lalonde,
                targets = c(25, .4, .1, .5, .3,
                            .5, 4000))
#> - targets:
#>         age  race_black race_hispan  race_white     married    nodegree 
#>        25.0         0.4         0.1         0.5         0.3         0.5 
#>        re74 
#>      4000.0 
#Notice race is split into three values (.4, .1, and .5)