Weighted Least Squares In R

This post will discuss the implementation of Weighted Least Squares (WLS) in R. The OLS method minimizes the sum of squared residuals, while the WLS weights the square residuals. The WLS technique is used when the OLS assumption related to constant variance in the errors is violated.

The WLS technique is also known as weighted linear regression it is a generalization of ordinary least squares (OLS) and linear regression in which knowledge of the variance of observations is incorporated into the regression.

Let us perform the WLS in R.

Here we will use mtcars data set. You need to load this data set first using attach() function. For example,

attach(mtcars)

Consider the following example regarding the weighted least squares in which the reciprocal of $wt$ variable is used as weights. Here two different weighted models are performed and then check the fit of the model using anova() function.

w_model1 <- lm(mpg ~ wt + hp, data = mtcars)

w_model2 <- lm(mpg ~ wt + hp, data = mtcars, weights = 1/wt)

To check the models fit, summary statistics of the fitted model, and different diagnostic plots of the fitted model, one can use the built-in functions as,

anova(w_model1, w_model2 )

summary(w_model1)
summary(w_model2)

plot(w_model1)
plot(w_model2)
Weighted Least Squares (Model 1)
Diagnostic Plots for WLS model: Model-1
Weighted Least Squares Model-2
Diagnostic Plots for WLS model: Model-2

Learn about Generalized Least Squares