Performing a weighted analysis

Goals

This tutorial introduces the use of the optional weights for regression with the quantileFit procedure. After this tutorial you should be able to estimate a weighted quantile regression.

The weights input

The quantileFit procedure accepts weights as the optional fourth input:

quantileFit(dataset, formula, tau, weights)

or

quantileFit(y, x, tau, weights)

The weights should be observation specific and the length of the weight vector must be equal to the length of the independent and dependent variable vectors.

Example #1

Consider the previous example:

$$ln(wage) = \alpha + \beta_1 * age + \beta_2 * age^2 + \beta_3 * tenure .$$

Now, we will estimate the same model but will include weights. For the sake of simplicity, we will use randomly generated weights.

rndseed 68987;

// Create string with full path to dataset  
dataset = getGAUSSHome() $+ "examples/regsmpl.dta";

// Specify quantile levels
tau = 0.05|0.50|0.95;

/*
** Generate random weights
** Note that the dataset has 28534 obs
*/
weights = rndn(28534, 1);

// Estimate the model with matrix inputs
call quantileFit(dataset, "ln_wage ~ age + age:age + tenure", tau, weights);

Notice that we now include four inputs when calling quantileFit. Even though we use the default quantile levels we must still include tau as an input because weights must be the fourth input.

This produces the following output:

                     -- Weighted Analysis --

Total observations:                                   28101
Number of variables: 3
VAR. / tau (in %) 5% 50% 95%
--------------------------------------------------- CONSTANT 0.4912 0.4983 0.5082 age 0.0675 0.0665 0.0654 age:age -0.0011 -0.0010 -0.0010 tenure 0.0469 0.0467 0.0465

Note that GAUSS reports when a weighted analysis is performed before printing the estimates. In addition, we can see that the weights impact the estimated coefficients.

Example #2

Now suppose we incorrectly try to use a vector of weights that does not have the same number of observations as the original data:

rndseed 68987;

// Create string with full path to dataset  
dataset = getGAUSSHome() $+ "examples/regsmpl.dta";

// Specify quantile levels
tau = 0.05|0.50|0.95;

/*
** Generate random weights
** Note that the dataset has 28534 obs
** Use incorrect weight vector length
*/
weights = rndn(150, 1);

// Estimate the model with matrix inputs
call quantileFit(dataset, "ln_wage ~ age + age:age + tenure", tau, weights);

Now when we estimate our model we will see the following error message:

Because the default unit weights are used, our results are now the same as if we did not include any weights:

Total observations:                                   28101
Number of variables: 3
VAR. / tau (in %) 5% 50% 95%
--------------------------------------------------- CONSTANT -0.7630 0.5112 0.0006 age 0.1103 0.0656 0.1271 age:age -0.0017 -0.0010 -0.0016 tenure 0.0356 0.0466 0.0196

Conclusion

This tutorial showed you how to perform a weighted analysis using quantileFit. You should now know how to:

  • Specify weights for quantileFit

In the next tutorial we will learn how to store results from the quantileFit procedure.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.