How to implement Quantile regression in R

In this recipe, we shall learn how to implement quantile regression in R with the help of an example. We shall make use of the

Recipe Objective: How to implement Quantile regression in R?

Quantile regression calculates the conditional quantile function as a linear combination of its predictors, just like linear regression, which calculates the conditional mean function as a linear combination of the given predictors. For implementing Quantile regression in R, we will make use of the "quantreg" package. The steps are as follows-

Step 1: Load the required packages

#loading required packages
library(quantreg)
library(caret)
library(ggplot2)

Step 2: Load the dataset necessary

mt cars is an inbuilt dataset in R that comprises fuel consumption and ten aspects of automobile design and performance for 32 automobiles. It is a data frame with 32 observations on 11 numeric variables.

[, 1] mpg Miles/(US) gallon
[, 2] cyl Number of cylinders
[, 3] disp Displacement (cu.in.)
[, 4] hp Gross horsepower
[, 5] drat Rear axle ratio
[, 6] wt Weight (1000 lbs)
[, 7] qsec 1/4 mile time
[, 8] vs Engine (0 = V-shaped, 1 = straight)
[, 9] am Transmission (0 = automatic, 1 = manual)
[,10] gear Number of forwarding gears
[,11] carb Number of carburetors

#loading the dataset
data(mtcars)

Step 3: Check the structure of the dataset

#structure
str(mtcars)

'data.frame':	32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

We can see that all the columns are numeric types.

Step 4: Fit the model

#fitting quantile regression model
quant <- rq(disp ~ wt, data = mtcars)
quant

	Call:
rq(formula = disp ~ wt, data = mtcars)

Coefficients:
(Intercept)          wt 
  -129.7880    108.7367 

Degrees of freedom: 32 total; 30 residual

Step 5: Check model summary

#model summary
summary(quant)

Call: rq(formula = disp ~ wt, data = mtcars)

tau: [1] 0.5

Coefficients:
            coefficients lower bd  upper bd 
(Intercept) -129.7880    -185.6818 -100.5439
wt           108.7367     103.1682  121.9677

wt turns out to be the most significant predictor variable in the model.

Step 6: Plots

#plots
plot(disp ~ wt, data = mtcars)
abline(rq(disp ~ wt, data = mtcars), col = "red")

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.