Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

regression
r

How to perform robust regression in R

Fahad Akhtar

Overview

Robust regression is a method we use when we have influential observationsThese are the observations that dramatically alter the coefficient estimates of the regression model if removed. or outliers in our dataset. To do this, we use the rlm() method from the MASS library.

Syntax

rlm(formula, data)

Parameters

This method takes two inputs, formula and data.

formula is in the form of y +x1+x2+...y ~ + x_1 + x_2 +..., where x1x_1 and x2x_2 are variables from the data on which we are running the regression model.

Example

Let’s look at a working example.

suppressPackageStartupMessages(library(MASS))

# Lets start by creating a data set.

df <- data.frame(x1=c(1, 3, 3, 4, 4, 6, 6, 8, 9, 3,
                      11, 16, 16, 18, 19, 20, 23, 23, 24, 25),
                 x2=c(7, 7, 4, 29, 13, 34, 17, 19, 20, 12,
                      25, 26, 26, 26, 27, 29, 30, 31, 31, 32),
                  y=c(17, 170, 19, 194, 24, 2, 25, 29, 30, 32,
                      44, 60, 61, 63, 63, 64, 61, 67, 59, 70))
                      
#function call.

rlm(y~x1+x2, data=df)
Example code for the rlm method

Explanation

  • Line 1: In order to use the rlm() method, we access the MASS package by using library(MASS) command. The suppressPackageStartupMessages() method is used to prevent any additional messages from cluttering our console.

  • Lines 5 to 10: We generate our dataset.

  • Line 14: We call the rlm() method to perform regression on the dataset.

RELATED TAGS

regression
r

CONTRIBUTOR

Fahad Akhtar
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring