How to implement logistic regression in R

This recipe helps you implement logistic regression in R

Recipe Objective - How to implement logistic 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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

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

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.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

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.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.