# Weight - Configure Weight from Variable(s)

Jump to navigation
Jump to search

This is the R-based approach to computing weights, that can be applied to tables and other analyses to adjust over- and under-representation of groups in the sample relative to the population. It is recommended to read How to Weight Survey Data for more information. The min/max/iterations fields in the OPTIONS section are using alongside the "repeated raking and trimming" method of trimming weights.

Applying weights using this method involves three steps:

**Configuring the weight**(an R Output, accessed via**Insert > More > Weighting > Configure Weight from Variable(s)**). Key Options for this are discussed below.**Generating the weighting variable**(R Variable, created by selecting the output at (1) above and then going to**Anything > Weighting > Multiple Variables > Save Weight Variable from Configuration**)**Applying the variable**made at (2) above, to whatever outputs (using the Object Inspector of the relevant target outputs: pages, tables, charts, visualizations)

## Example

## Options

**Filter(s)**If a filter is selected, the weight that is created will only apply to the filtered cases. Other cases will have missing values (`NA`).

**Weight**An (optional) existing weight. If supplied, the new weight will be as close as possible^{[1]}to this existing weight.

### CATEGORICAL ADJUSTMENT VARIABLES

**Adjustment variables**Categorical, Date/Time, or text variables that are to be used in creating weights. See How to Weight Survey Data for more information.

**Add variable targets**One such button will appear for each adjustment variable. When clicked, the targets are added into a spreadsheet (by typing or pasting). They are entered in the following format.

### NUMERIC ADJUSTMENT VARIABLES

**Adjustment variables**Numeric variables that are to be used in creating weights. See How to Weight Survey Data for more information.

**Add variable targets**One such button will appear for each adjustment variable. When clicked, the targets are added into a spreadsheet (by typing or pasting).

### OPTIONS

**Minimum Weight**The lowest value that a weight will, ideally, have for any case in the data. It is not guaranteed that a weight will be greater than this value, as after application of this minimum, the weight is than divided by its average, so that it takes an average value of 1.

**Maximum Weight**The maximum value that a weight will, ideally, have for any case in the data. It is not guaranteed that a weight will be less than this value, as after application of this maximum, the weight is than divided by its average, so that it takes an average value of 1.

**Iterations**The number of iterations of the algorithm applied after trimming the weight to reflect the minimum and maximum.

### SAVE VARIABLE(S)

**Save Weight Variable from Configuration** Saves a new variable to the data set that contains the weights.

## Code

```
var heading_text = "Configure Weight from Variables";
if (!!form.setObjectInspectorTitle)
form.setObjectInspectorTitle(heading_text, heading_text);
else
form.setHeading(heading_text);
form.group({label: "Categorical adjustment variables", expanded: true})
var cat_adj_vars = form.dropBox({name: "formCategorical",
label: "Adjustment variables:",
types: ["Variable"],
multi: true,
required: false}).getValues();
var target_controls = [];
for (var i = target_controls.length; i < cat_adj_vars.length; i++)
target_controls.push(form.dataEntry({name: "formCategoricalTarget" + (i + 1),
label: "Add variable targets " + (i + 1),
edit_label: "Edit variable targets " + (i + 1),
default_value: [["Target label 1",".5"],["Target label 2",".5"]],
required: true,
prompt: "Target percentages, proportions, or values. Needs to match the input Adjustment Variable.",
large_data_error: "The data entered is too large. You may only enter data with up to 1000 rows and up to 100 columns"}))
form.group({label:"Numeric adjustment variables", expanded:true})
var num_adj_vars = form.dropBox({name: "formNumeric",
label: "Adjustment variables:",
types: ["Variable: numeric"],
multi: true,
required: false}).getValues();
var num_target_controls = [];
for (var i = num_target_controls.length; i < num_adj_vars.length; i++)
target_controls.push(form.textBox({name: "formNumericTarget" + (i + 1),
label: "Target average variable " + (i + 1),
required: true,
prompt: "Target average"}))
form.group({label:"Options", expanded: true})
form.textBox({name: "formMinimumWeight",
label: "Minimum weight",
required: false,
default_value: 0,
prompt: "A weight will be created with a minimum of the specified value (if possible)"})
form.textBox({name: "formMaximumWeight",
label: "Maximum weight",
required: false,
default_value: 1000,
prompt: "A weight will be created with a minimum of the specified value (if possible)"})
form.numericUpDown({name: "formTrimIterations",
label: "Iterations",
increment: 1,
minimum: 0,
maximum: 1000,
default_value: 20,
prompt: "High values can, in some circumstance, improve the extent to which the lower and upper bounds are satisfied."})
```

```
# Reading targets
numeric.targets = rep(NA, length(formNumeric))
for(i in seq_along(numeric.targets))
numeric.targets[i] = as.numeric(get0(paste0("formNumericTarget", i)))
categorical.targets = vector("list", length(formCategorical))
for(i in seq_along(categorical.targets))
categorical.targets[[i]] = get0(paste0("formCategoricalTarget", i))
categorical.variables = formCategorical
if (!is.null(categorical.variables)) {
categorical.variables = lapply(categorical.variables, flipTransformations::ProcessQVariables)
}
sampling.weight = flipData::Calibrate(categorical.variables,
categorical.targets,
formNumeric,
numeric.targets,
lower = as.numeric(get0("formMinimumWeight")),
upper = as.numeric(get0("formMaximumWeight")),
input.weight = QPopulationWeight,
subset = QFilter,
always.calibrate = FALSE,
trim.iterations = formTrimIterations)
```

## See Also

- ↑ Deville, J.-C. and Särndal, C.-E. (1992). Calibration estimators in survey sampling. Journal of the American Statistical Association, 87, 376-382