# Regression - Save Variable(s) - Propensity Weight

Create a weight variable via the Propensity scores in a Regression or Machine Learning classification model.

Creates a new variable containing the Propensity Weights for each case in the data from a model that is a binary classifier (e.g., a Machine Learning or Regression model that has an outcome variable with only two categories such as a Binary Logit regression).

## Technical details

Assume the outcome variable $\displaystyle{ Y }$ is an ordinal or nominal variable that has two categories (classes) labelled 0 and 1, with 1 being the label for the positive class. The propensity score is calculated based off the estimated probabilities of being in the positive class $\displaystyle{ \widehat{p}(x) = P(Y = 1 |X = x) }$ where $\displaystyle{ X }$ denotes all the predictor variables used in the Regression or Machine Learning model and $\displaystyle{ x }$ being the observed predictors for each case. Then the propensity weight is calculated based off the observed category each case belongs to in the outcome variable. Cases with $\displaystyle{ Y = 1 }$ are allocated a weight of $\displaystyle{ 1/\widehat{p}(x) }$ while cases with $\displaystyle{ Y = 0 }$ are allocated a weight of $\displaystyle{ 1/(1 - \widehat{p}(x)) }$.

## How to apply this QScript

• Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
• Click on the QScript when it appears in the QScripts and Rules section of the search results.

OR

• Select Automate > Browse Online Library.
• Select this QScript from the list.

## Customizing the QScript

This QScript is written in JavaScript and can be customized by copying and modifying the JavaScript.

• Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
• Hover your mouse over the QScript when it appears in the QScripts and Rules section of the search results.
• Press Edit a Copy (bottom-left corner of the preview).
• Modify the JavaScript (see QScripts for more detail on this).
• Either:
• Run the QScript, by pressing the blue triangle button.
• Save the QScript and run it at a later time, using Automate > Run QScript (Macro) from File.

### Customizing QScripts in older versions

• Create a new text file, giving it a file extension of .QScript. See here for more information about how to do this.
• Modify the JavaScript (see QScripts for more detail on this).
• Run the file using Automate > Run QScript (Macro) from File.

## JavaScript

includeWeb('QScript R Output Functions');
let required_input_type = 'Binary Classification model (Logistic regression or Machine Learning ' +
'classification model with a binary outcome variable)';
let question_type = 'Number';
let is_weight = true;
let variable_name = 'propensity.weight';
let allowed_r_classes = ['BinaryLogitRegression', 'MultinomialLogitRegression', 'DeepLearning',