Global Research Syndicate
No Result
View All Result
  • Login
  • Latest News
  • Consumer Research
  • Survey Research
  • Marketing Research
  • Industry Research
  • Data Collection
  • More
    • Data Analysis
    • Market Insights
  • Latest News
  • Consumer Research
  • Survey Research
  • Marketing Research
  • Industry Research
  • Data Collection
  • More
    • Data Analysis
    • Market Insights
No Result
View All Result
globalresearchsyndicate
No Result
View All Result
Home Data Analysis

Hands-On Guide For Non-Linear Regression Models In R

globalresearchsyndicate by globalresearchsyndicate
November 24, 2019
in Data Analysis
0
Hands-On Guide For Non-Linear Regression Models In R
0
SHARES
14
VIEWS
Share on FacebookShare on Twitter


It is a truth universally acknowledged that not all the data can be represented by a linear model. By definition, non-linear regression is the regression analysis in which observational data is modeled by a function which is a non-linear combination of the parameters and depends on one or more independent variables. Non-linear regression is capable of producing a more accurate prediction by learning the variations in the data and their dependencies. 

In this tutorial, we will look at three most popular non-linear regression models and how to solve them in R. This is a hands-on tutorial for beginners with the good conceptual idea of regression and the non-linear regression models.



Pre-requisites:

  • Understanding of Non-Linear Regression Models
  • Knowledge of programming

Polynomial Regression

Polynomial regression is very similar to linear regression but additionally, it considers polynomial degree values of the independent variables. It is a form of regression analysis in which the relationship between the independent variable X and the dependent variable Y is represented as an nth degree polynomial in x. The model can be extended to fit multiple independent factors.


W3Schools


Consider for example a simple dataset consisting of only 2 features, experience and salary. Salary is the dependent factor and Experience is the independent factor. Unlike Simple linear regression which generates the regression for Salary against the given Experiences, the Polynomial Regression considers up to a specified degree of the given Experience values. That is, Salary will be predicted against Experience, Experience^2,…Experience ^n.

Code

The Polynomial Regression is handled by the inbuilt function ‘lm’ in R. After loading the dataset follow the instructions below. 

Creating the Polynomial Regressor Model and fitting it with Training Set

dataset$X2 = dataset$X^2
dataset$X3 = dataset$X^3
dataset$X4 = dataset$X^4
poly_regressor = lm(formula = Y ~ .,data = dataset)


The first 3 lines calculate the nth degree polynomial of the independent variable X for each row of observations and add them as features into the original dataset. Here we have calculated till the 5th degree denoted as X4

  • formula: Used to differentiate the independent variable(s) from the dependent variable. In case of multiple independent variables, the variables are appended using ‘+’ symbol. Eg. Y ~ X1 +  X2 + X3 + …
  • X: independent Variable or factor. The column label is specified
  • Y: dependent Variable. The column label is specified.
  • data: The data the model trains on, training set.

Predicting the Y value for a new X

predict(poly_regressor,newdata = data.frame(X = value, X2 = value^2, X3 = value^3, X4 = value^4))

This line predicts the value of the dependent factor for a new given value of independent factor.

  • regressor: The regressor model that was previously created for training.
  • newdata: The new observation or set of observations that you want to predict Y for. Accepts arguments as dataframes.
  • value: replace this with a number you want to predict Y for.


Visualizing the predictions

install.packages('ggplot2') #install once
library(ggplot2)
X_grid = seq(min(dataset$X), max(dataset$X), 0.1)
ggplot() +
geom_point(aes(x = dataset$X, y = dataset$Y),colour = 'black') +
geom_line(aes(x = X_grid, y = predict(poly_reg, newdata = data.frame(X = X_grid,X2 = X_grid^2, X3 = X_grid^3, X4 = X_grid^4))),colour = 'red')+
ggtitle('Polynomial Regression')
xlab('X')
ylab('Y')

This block of code represents the dataset in a graph. ggplot2 library is used for plotting the data points. To obtain a smooth curve the axis is scaled to 1/10th of X (X_grid).

  • geom_point() : This function scatter plots all data points in a 2 Dimensional graph
  • geom_line() : Generates or draws the regression line in 2D graph
  • ggtitle(): Assigns the title of the graph
  • xlab: Labels the X- axis
  • ylab: Labels the Y-axis

Decision Tree Regression

Decision Tree Regression works by splitting a dimension into different sections containing a minimum number of data points and predicts the result for a new data item by calculating the mean value of all the data points in the section it belongs to. That is it breaks down a dataset into smaller and smaller subsets while at the same time an associated decision tree is developed incrementally. Decision tree builds regression or classification models in the form of a tree structure

Code

The Decision Tree Regression is handled by the rpart library.

Installing and Importing Libraries

install.packages('rpart') #install once
library(rpart) # importing the library

Creating the Decision Tree Regressor and providing the Training Set

decisionTree_regressor = rpart(formula = Y ~ .,data = dataset, control = rpart.control(minsplit = 1))

The expression ‘Y ~ .” takes all variables except Y in the training_set as independent variables.

  • formula: Used to differentiate the independent variable(s) from the dependent variable.In case of multiple independent variables, the variables are appended using ‘+’ symbol. Eg. Y ~ X1 +  X2 + X3 + …
  • control: parameters that control the formation of the decision tree.
  • minsplit: a controller used to specify the number of observations that must exist in a node in order for a split to be attempted.
  • X: Independent Variable or factor. The column label is specified.
  • Y: Dependent Variable. The column label is specified.
  • data : The data the model trains on, training set.

Predicting the values for the test set

y_pred = predict(decisionTree_regressor, newdata = data.frame(X = value))

This line predicts the Y value for a given X value. Replace ‘value ‘ with real value.

Visualizing the test set results

library(ggplot2)
x_grid = seq(min(dataset$X), max(dataset$X), 0.01)
ggplot() +
geom_point(aes(dataset$X, dataset$Y),color= 'red') +
geom_line(aes(x_grid, predict(decisionTree_regressor, data.frame(X = x_grid)), color = 'black'))+
ggtitle('Y vs X (Decision Tree Regression) ')
xlab('X')
ylab('Y') 

This code plots the data points and the regressor on a 2 Dimensional graph. For more precision, the axis is scaled to 1/10th of X (X_grid).

  • geom_point() : This function scatter plots all data-points in a 2 Dimensional graph
  • geom_line() : Generates or draws the regression line in 2D graph
  • ggtitle(): Assigns the title of the graph
  • xlab: Labels the X- axis
  • ylab: Labels the Y-axis

plot(decisionTree_regressor)

See Also


This line displays the tree structure generated.

Random Forest Regression

Random Forest Regression is one of the most popular and effective predictive algorithms used in Machine Learning. It is a form of ensemble learning where it makes use of an algorithm multiple times to predict and final prediction is the average of all predictions. Random Forest Regression is a combination of multiple Decision Tree Regressions. Hence the name Forest.

Code

The library randomForest is used for handling Random Forest Regression in R

Installing and Importing the Library

install.packages('randomForest') #install once
library(randomForest) # importing the library

Creating the Random Forest Regressor and fitting it with Training Set

random_forest_regressor = randomForest(x = training_set$X, y = training_set$Y, ntree = 300)

This line creates a Random Forest Regressor and provides the data to train.

  • X: independent variable
  • Y: dependent variable
  • ntree: the number of decision trees you want to generate to predict.

Predicting the value for a new X

y_pred = predict(regressor, data.frame(X = value))

Note:

Replace ‘value’ with a real number you want to predict Y for.


Enjoyed this story? Join our Telegram group. And be part of an engaging community.


Provide your comments below

comments

Amal Nair

Amal Nair

A Computer Science Engineer who is passionate about AI and all related technologies. He is someone who loves to stay updated with the Tech-revolutions that AI brings in.
Contact: [email protected]

Related Posts

How Machine Learning has impacted Consumer Behaviour and Analysis
Consumer Research

How Machine Learning has impacted Consumer Behaviour and Analysis

January 4, 2024
Market Research The Ultimate Weapon for Business Success
Consumer Research

Market Research: The Ultimate Weapon for Business Success

June 22, 2023
Unveiling the Hidden Power of Market Research A Game Changer
Consumer Research

Unveiling the Hidden Power of Market Research: A Game Changer

June 2, 2023
7 Secrets of Market Research Gurus That Will Blow Your Mind
Consumer Research

7 Secrets of Market Research Gurus That Will Blow Your Mind

May 8, 2023
The Shocking Truth About Market Research Revealed!
Consumer Research

The Shocking Truth About Market Research: Revealed!

April 25, 2023
market research, primary research, secondary research, market research trends, market research news,
Consumer Research

Quantitative vs. Qualitative Research. How to choose the Right Research Method for Your Business Needs

March 14, 2023
Next Post
College Football AP survey prediction after week 13

College Football AP survey prediction after week 13

Categories

  • Consumer Research
  • Data Analysis
  • Data Collection
  • Industry Research
  • Latest News
  • Market Insights
  • Marketing Research
  • Survey Research
  • Uncategorized

Recent Posts

  • Ipsos Revolutionizes the Global Market Research Landscape
  • How Machine Learning has impacted Consumer Behaviour and Analysis
  • Market Research: The Ultimate Weapon for Business Success
  • Privacy Policy
  • Terms of Use
  • Antispam
  • DMCA

Copyright © 2024 Globalresearchsyndicate.com

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT
No Result
View All Result
  • Latest News
  • Consumer Research
  • Survey Research
  • Marketing Research
  • Industry Research
  • Data Collection
  • More
    • Data Analysis
    • Market Insights

Copyright © 2024 Globalresearchsyndicate.com