# Weighting - Create Weight

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 > Create Weight**). 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**Insert > More > Weighting > Save Variable**)**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 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 weighting variable** Saves a new variable to the data set that contains the weights.

## Code

```
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."}))
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))
sampling.weight = flipData::Calibrate(formCategorical,
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)
```

## References

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