R Tutorial: Simple Linear Regression
Key Takeaways
This video tutorial covers Simple Linear Regression using R, specifically the LM function from the stat package and data visualization with ggplot2, to predict future margin based on margin in year 1.
Full Transcript
now that we have inspected the correlations between the various variables we'll move on to predicting the future margin with help of the margin in year 1 we chose the margin in year 1 since the correlation between the two variables is the highest when I only use one independent variable for the prediction we call the model a simple linear regression in reality the ideal case of a perfect linear correlation that you can exactly predict Y with the given value of x is very unlikely most of the time the data points are scattered around in the form of a cloud for this we determine the direction of the relationship between x and y by fitting a straight line through the cloud this is what we use the least squares estimation procedure for this method helps us find the regression line and returned its coefficients the difference between our prediction a point on the line and the actual value a data point is called the prediction error or residual value that in a theory let's move on to some code we can specify the linear regression model using a formula object in the LM function from the stat package looking at the arguments notice that we are looking to predict future margin as a function of margin using sylvie data 1 we store the model as simple LM then we can use the summary function with simple LM as an argument to get an overview of the results take a look at the coefficient estimate for margin with the value of roughly 0.65 it is greater than zero which means that the higher the margin in year one the higher we expect the future margin to be also take a look at the multiple r-squared at the bottom of the output a value of roughly 0.32 means that about 30% of the variation and the future margin can be explained by the margin in year 1 but more on that later the ggplot function from the ggplot2 package gives us a nice visualization of the relationship here we produce a simple scatterplot of the observations using our sylvie data one data set the data is the first argument and we specify margin as the x-axis and future margin as the y-axis in the aes call this is the second argument to ggplot we also use geum smooth with method equals LM to fit a linear regression line through the data cloud before moving on to multiple linear regression let's take a look at the conditions that the data must satisfy for linear regression to be the best method the relationship between the independent variable and the dependent variable should be linear the independent variable should not contain any measurement errors the residuals should be uncorrelated one cause of correlation among the errors is violation of the linearity assumption the residuals should randomly vary around zero and their expectation should be equal to zero usually this assumption is not problematic as long as a constant is included in the model the variance of the prediction error has to be constant if not inferences made from the model can be misleading when doing statistical significance testing we also have to assume that the errors are normally distributed a well-established method to check the violation of these assumptions is a plot of the predicted values against the estimated residuals this is called a residual plot now let's try some examples
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/machine-learning-for-marketing-analytics-in-r at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Now that we have inspected the correlations between the various variables, we'll move on to predicting the future margin with help of the margin in year 1. We chose the margin in year one since the correlation between the two variables is the highest. When I only use one independent variable for the prediction, we call the model a simple linear regression.
In reality, the ideal case of a perfect linear correlation, where you can exactly predict y with a given value of x, is very unlikely. Most of the time the data points are scattered around, in the form of a cloud. For this, we determine the direction of the relationship between x and y by fitting a straight line through the cloud.
This is what we use the least-squares estimation procedure for. This method helps us find the regression line and returns its coefficients. The difference between our prediction (a point on the line) and the actual value (a data point) is called the prediction error or residual value.
That's enough theory, let's move on to some code.
We can specify the linear regression model using a formula object in the lm function from the stats package. Looking at the arguments, notice that we are looking to predict futureMargin as a function of margin, using clvData1. We store the model as simpleLM. Then we can use the summary function with simpleLM as an argument, to get an overview of the results.
Take a look at the coefficient estimate for margin. With a value of roughly 0.65, it is greater than 0 which means that the higher the margin in year 1 the higher we expect the future margin to be. Also, take a look at the multiple $R^2$ at the bottom of the output. A value of roughly 0.32 means that about 30 percent of the variation in the future margin can be ex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: ML Pipelines
View skill →
🎓
Tutor Explanation
DeepCamp AI