How to perform robust regression in R
Overview
Robust regression is a method we use when we have 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 , where and 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)
Explanation
-
Line 1: In order to use the
rlm()method, we access theMASSpackage by usinglibrary(MASS)command. ThesuppressPackageStartupMessages()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.
Free Resources