R Tutorial: Linear regression - the fundamental method
Key Takeaways
This video tutorial covers linear regression using R, specifically the LM function, and explores model diagnostics using summary and glance functions from the broom package.
Full Transcript
let's start with the most basic regression algorithm linear regression linear regression assumes that the expected outcome is the weighted sum of all the inputs it also assumes that the change in Y is linearly proportional to the change in any X this is the simplest of the regression methods in our cricket example linear regression assumes that temperature is directly proportional to cricket chirp rate it are you fit a linear regression model using the LM function this function takes as arguments a formula that describes the model you want to fit and the data here the data is in the data frame cricket with an outcome column temperature and an input column trips per second a formula in R is designated by a twiddle or tilt the left hand side of the formula is the outcome you want to predict such as temperature or blood pressure the right hand side of the formula holds the input variables you can concatenate multiple input variables with a plus sign to convert a string into a formula use the s formula function print the model to look at its structure you will see a report on the coefficients or betas of the model the intercept is beta 0 the value of the model when all the inputs are 0 the other coefficients are the weights for the weighted sum of the variables in this example the coefficient for trips per second is just over 3 that means 2 things first the sign of the coefficient is positive so temperature should increase as trip rate increases second for every unit increase in chirp rate the temperature should increase by little over 3 degrees if everything else is held constant you can get the model Diagnostics by calling summary on the model summary includes not only the values of the coefficients but the standard error in their estimated value along with other Diagnostics we will cover some of these Diagnostics in a later chapter but for now just know that they are available to get these Diagnostics conveniently packaged in a data frame use the glance function from the package broom for the R squared diagnostic you can also use the function R app F test from the package sigur now let's do some exercises to review what you've learned
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/supervised-learning-in-r-regression at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Let's start with the most basic regression algorithm: linear regression.
Linear regression assumes that the expected outcome is the weighted sum of all the inputs. It also assumes that the change in y is linearly proportional to the change in any x.
This is the simplest of the regression methods. In our cricket example, linear regression assumes that temperature is directly proportional to cricket chirp rate. In R, you fit a linear regression model using the lm function. This function takes as arguments a formula that describes the model you want to fit, and the data. Here, the data is in the dataframe cricket, with an outcome column temperature and an input column chirps_per_sec.
A formula in R is designated by a twiddle, or tilde. The left hand side of the formula is the outcome you want to predict, such as temperature or blood_pressure. The right hand side of the formula holds the input variables. You can concatenate multiple input variables with a plus sign.
To convert a string into a formula, use the as-dot-formula function. Print the model to look at its structure. You will see a report on the coefficients (or betas) of the model. The intercept is beta-zero: the value of the model when all the inputs are zero. The other coefficients are the weights for the weighted sum of the variables. In this example, the coefficient for chirps_per_sec is just over 3. That means two things:
First the sign of the coefficient is positive, so temperature should increase as chirp rate increases. Second for every unit increase in chirp rate, the temperature should increase by a little over 3 degrees, if everything else is held constant.
You can get the model diagnostics by calling summary on the model. Summary includes not only the values of the coe
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: Supervised Learning
View skill →Related Reads
📰
📰
📰
📰
AI Interview Copilot vs AI Mock Interview Tool: Which One Do Developers Need?
Dev.to AI
I Automated My Developer Portfolio (and You Can Too)
Dev.to AI
How to Check If ChatGPT and Perplexity Actually Recommend Your Website (I Wrote a Script to Find…
Medium · Python
Building a voice-first database: why we chose speech over spreadsheets
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI