Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Ridge Regression in R Programming
Next article icon

Ridge Regression in R Programming

Last Updated : 23 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Ridge regression is a classification algorithm that works in part as it doesn’t require unbiased estimators. Ridge regression minimizes the residual sum of squares of predictors in a given model. Ridge regression includes a shrinks the estimate of the coefficients towards zero.

Ridge Regression in R

Ridge regression is a regularized regression algorithm that performs L2 regularization that adds an L2 penalty, which equals the square of the magnitude of coefficients. All coefficients are shrunk by the same factor i.e none are eliminated. L2 regularization will not result in sparse models. Ridge regression adds bias to make the estimates reliable approximations to true population values. Ridge regression proceeds by adding a small value k to the diagonal elements of the correlation matrix i.e ridge regression got its name since the diagonal of ones in the correlation matrix are thought to be a ridge.

ridge regression

Here, k is a positive quantity less than 1(usually less than 0.3). The amount of bias in the estimator is given by:

ridge regression

The covariance matrix is given by: 

ridge regression

There exists a value of k for which the Mean Squared Error(MSE i.e variance plus the bias squared) of the ridge estimator is less than least squares estimator. The appropriate value of k depends on the true regression coefficients(that are being estimated)and the optimality of the ridge solution. 

  • When lambda = 0, ridge regression equals least squares regression.
  • When lambda = infinity, all coefficients shrunk to zero.

Also, the ideal penalty is in between 0 and infinity. Let's implement Ridge regression in R programming. 

Ridge Regression Implementation in R

The Dataset

Big Mart dataset consists of 1559 products across 10 stores in different cities. Certain attributes of each product and store have been defined. It consists of 12 features i.e Item_Identifier( is a unique product ID assigned to every distinct item), Item_Weight(includes the weight of the product), Item_Fat_Content(describes whether the product is low fat or not), Item_Visibility(mentions the percentage of the total display area of all products in a store allocated to the particular product), Item_Type(describes the food category to which the item belongs), Item_MRP(Maximum Retail Price (list price) of the product), Outlet_Identifier(unique store ID assigned. It consists of an alphanumeric string of length 6), Outlet_Establishment_Year(mentions the year in which store was established), Outlet_Size(tells the size of the store in terms of ground area covered), Outlet_Location_Type(tells about the size of the city in which the store is located), Outlet_Type(tells whether the outlet is just a grocery store or some sort of supermarket) and Item_Outlet_Sales( sales of the product in the particular store).

R
# Loading data train = fread("Train_UWu5bXk.csv") test = fread("Test_u94Q5KV.csv")  # Structure str(train) 

Output: 

output

Performing Ridge Regression on Dataset

Using Ridge regression algorithm on the dataset which includes 12 features with 1559 products across 10 stores in different cities.

R
# Installing Packages install.packages("data.table") install.packages("dplyr") install.packages("glmnet") install.packages("ggplot2") install.packages("caret") install.packages("xgboost") install.packages("e1071") install.packages("cowplot")  # load packages library(data.table) # used for reading and manipulation of data library(dplyr)     # used for data manipulation and joining library(glmnet)     # used for regression library(ggplot2) # used for plotting library(caret)     # used for modeling library(xgboost) # used for building XGBoost model library(e1071)     # used for skewness library(cowplot) # used for combining multiple plots  # Loading datasets train = fread("Train_UWu5bXk.csv") test = fread("Test_u94Q5KV.csv")  # Setting test dataset # Combining datasets # add Item_Outlet_Sales to test data test[, Item_Outlet_Sales := NA]  combi = rbind(train, test)  # Missing Value Treatment missing_index = which(is.na(combi$Item_Weight)) for(i in missing_index) { item = combi$Item_Identifier[i] combi$Item_Weight[i] =         mean(combi$Item_Weight[combi$Item_Identifier == item],         na.rm = T) }  # Replacing 0 in Item_Visibility with mean zero_index = which(combi$Item_Visibility == 0) for(i in zero_index) { item = combi$Item_Identifier[i] combi$Item_Visibility[i] =         mean(combi$Item_Visibility[combi$Item_Identifier == item],                                                     na.rm = T) }  # Label Encoding # To convert categorical in numerical combi[, Outlet_Size_num := ifelse(Outlet_Size == "Small", 0,                             ifelse(Outlet_Size == "Medium", 1, 2))]  combi[, Outlet_Location_Type_num :=                 ifelse(Outlet_Location_Type == "Tier 3", 0,                     ifelse(Outlet_Location_Type == "Tier 2", 1, 2))]  combi[, c("Outlet_Size", "Outlet_Location_Type") := NULL]  # One Hot Encoding # To convert categorical in numerical ohe_1 = dummyVars("~.", data = combi[, -c("Item_Identifier",                                         "Outlet_Establishment_Year",                                         "Item_Type")], fullRank = T) ohe_df = data.table(predict(ohe_1, combi[, -c("Item_Identifier",                                         "Outlet_Establishment_Year",                                         "Item_Type")]))  combi = cbind(combi[, "Item_Identifier"], ohe_df)  # Remove skewness skewness(combi$Item_Visibility) skewness(combi$price_per_unit_wt)  # log + 1 to avoid division by zero combi[, Item_Visibility := log(Item_Visibility + 1)]  # Scaling and Centering data num_vars = which(sapply(combi, is.numeric)) # index of numeric features num_vars_names = names(num_vars)  combi_numeric = combi[, setdiff(num_vars_names, "Item_Outlet_Sales"),                                                         with = F]  prep_num = preProcess(combi_numeric, method=c("center", "scale")) combi_numeric_norm = predict(prep_num, combi_numeric)  # removing numeric independent variables combi[, setdiff(num_vars_names, "Item_Outlet_Sales") := NULL] combi = cbind(combi, combi_numeric_norm)  # splitting data back to train and test train = combi[1:nrow(train)] test = combi[(nrow(train) + 1):nrow(combi)]  # Removing Item_Outlet_Sales test[, Item_Outlet_Sales := NULL]  # Model Building :Lasso Regression set.seed(123) control = trainControl(method ="cv", number = 5) Grid_la_reg = expand.grid(alpha = 1, lambda = seq(0.001,                                     0.1, by = 0.0002))  # Model Building : Ridge Regression set.seed(123) control = trainControl(method ="cv", number = 5) Grid_ri_reg = expand.grid(alpha = 0, lambda = seq(0.001, 0.1,                                                 by = 0.0002))  # Training Ridge Regression model Ridge_model = train(x = train[, -c("Item_Identifier",                                 "Item_Outlet_Sales")],                     y = train$Item_Outlet_Sales,                     method = "glmnet",                     trControl = control,                     tuneGrid = Grid_reg                     ) Ridge_model  # mean validation score mean(Ridge_model$resample$RMSE)  # Plot plot(Ridge_model, main="Ridge Regression") 

Output: 

  • Model Ridge_model:

output

The Ridge regression model uses the alpha value as 0 and lambda value as 0.1. RMSE was used to select the optimal model using the smallest value. 

  • Mean validation score:
output
Mean validation score

The mean validation score of the model is 1133.668. 

  • Plot:

output

The regularization parameter increases, RMSE remains constant.


Next Article
Ridge Regression in R Programming

D

dhruv5819
Improve
Article Tags :
  • Machine Learning
  • R Language
  • DSA
  • AI-ML-DS
  • R Machine-Learning
  • R Data-science
Practice Tags :
  • Machine Learning

Similar Reads

    Regularization in R Programming
    Regularization is a form of regression technique that shrinks or regularizes or constraints the coefficient estimates towards 0 (or zero). In this technique, a penalty is added to the various parameters of the model in order to reduce the freedom of the given model. The concept of Regularization can
    7 min read
    Data Reshaping in R Programming
    Generally, in R Programming Language, data processing is done by taking data as input from a data frame where the data is organized into rows and columns. Data frames are mostly used since extracting data is much simpler and hence easier. But sometimes we need to reshape the format of the data frame
    5 min read
    Weighted Ridge Regression in R
    Weighted Ridge Regression extends regular Ridge Regression by assigning different weights to data points based on their importance. This allows for more flexibility and improved model accuracy by giving more influence to reliable data points.What is Ridge Regression?Ridge Regression is a method used
    4 min read
    Ridge Regression
    Ridge regression, also known as L2 regularization, is a technique used in linear regression to address the problem of multicollinearity among predictor variables. Multicollinearity occurs when independent variables in a regression model are highly correlated, which can lead to unreliable and unstabl
    8 min read
    Data Wrangling in R Programming - Working with Tibbles
    R is a robust language used by Analysts, Data Scientists, and Business users to perform various tasks such as statistical analysis, visualizations, and developing statistical software in multiple fields.In R Programming Language Data Wrangling is a process of reimaging the raw data to a more structu
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences