How to implement linear regression in R

This recipe helps you implement linear regression in R

Recipe Objective - How to implement linear regression in R?

Step 1 - Downloading packages

install.packages("neuralnet")
install.packages("MASS")

Step 2 - Loading packages

library(neuralnet)
library(MASS)

Step 3 - Loading the Dataset

data <- Boston

Step 4 - Checking for null values

apply(data,2,function(x) sum(is.na(x)))

Step 5 - Splitting dataset into train and test

index <- sample(1:nrow(data),round(0.75*nrow(data)))
maxd <- apply(data, 2, max)
mind <- apply(data, 2, min)

Step 6 - Function to normalize data

scaled <- as.data.frame(scale(data, center = mind, scale = maxd - mind))

Step 7 - Training data

train <- scaled[index,]

Step 8 - Testing data

test <- scaled[-index,]

Step 9 - Deriving formula for linear regression

t <- names(train)
formula <- as.formula(paste("medv ~", paste(t[!t %in% "medv"], collapse = " + ")))

Step 10 - Model Building - Linear regression

nn_model <- neuralnet(formula,data=train,hidden=c(5,3),linear.output=T)

Step 11 - Plotting neural network

plot(nn_model)

Step 12 - Predicting on test data

prediction <- compute(nn_model,test[,1:13])
prediction <- prediction$net.result*(max(data$medv)-min(data$medv))+min(data$medv)
test.a <- (test$medv)*(max(data$medv)-min(data$medv))+min(data$medv)

Step 13 - Calculating Mean Square Error

MSE <- sum((test.a - prediction)^2)/nrow(test)
print(MSE)

Step 14 - Plot of Real vs Predicted values

plot(test$medv,prediction,col='Green',main='Real vs predicted NN',pch=18,cex=0.7)
abline(0,1,lwd=2)
legend('bottomright',legend='Neural Network',pch=18,col='Green', bty='n')

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

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

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification