Linear Regression in R, Step-by-Step
Key Takeaways
This video teaches linear regression in R through a step-by-step example
Full Transcript
I like Stat Quest. Do you like Stat Quest? I like Stat Quest. And I hope you like Stat Quest, too. Hello, and welcome to Stat Quest. Stat Quest is brought to you by the friendly folks in the genetics department at the University of North Carolina at Chapel Hill. Today, we're going to be talking about doing linear regression in R. This particular Stat Quest is intended to be a companion video for the Stat Quest on linear regression. For this tutorial, I'm going to assume that you already know how to get your data into R, and instead, I'm going to focus on how to get that data into a linear regression model and how to interpret the results. This is how I created the data for the Stat Quest on linear regression. I created a data frame with two columns, weight and size. If I just type mouse.data and then press return, R will print out the data frame in a nice column format. I then use the plot function to plot the data on an XY graph. This is where I set up the actual linear regression. I call the function LM, which stands for linear models, and I pass it a formula, and I pass it the mouse data. The way I specified the formula means that size are considered to be the Y values and weight are considered to be the X values. The linear models function then calculates the least squares estimates for the Y intercept and the slope. In R, the meat of doing a regression is in the summary function. This function generates all kinds of output, and I'm going to walk through it one step at a time. The first line just prints out the original call to the LM or linear models function. After that, you get a summary of the residuals. Those are the distance from the data to the fitted line. Ideally, they should be symmetrically distributed about the line. That means you want the min value and the max value to be approximately the same distance from zero. Likewise, you'd like the first quantile or 1Q and the third quantile or 3Q to be equidistant from zero. Also, it's nice to have the median close to zero as well. This next section tells us about the least squares estimates for the fitted line. This value is for the intercept and this value is for the slope. The standard error of the estimates and the T value are both provided to show you how the P values were calculated. These P values test whether the estimates for the intercept and the slope are equal to zero or not. If they're equal to zero, that means they don't have much use in the model. Lastly, these are the P values for the estimated parameters. Generally speaking, we are usually not interested in the intercept, so it doesn't matter what its P value is. However, we want a P value for weight to be less than .05. That is, we want it to be statistically significant. A significant P value for weight means that it will give us a reliable guess of mouse size. If you were unable to read the actual P value, but could, for some reason, see the star to its right, then these codes would give you a sense of what the P value was. The next line, the residual standard error, is the square root of the denominator in the equation for F. The next line tells us the multiple R squared and adjusted R squared values. Multiple R squared is just R squared as I describe it in the StatQuest on linear regression. It means that weight can explain 61% of the variation in size. This is good. Generally speaking, the adjusted R squared is the R squared scaled by the number of parameters in the model. The next line tells us if the R squared is significant or not. This is the value for F. These are the degrees of freedom. And here's our P value. Again, this says that weight gives us a reliable estimate for size. Lastly, we can add the regression line to the XY graph we started drawing earlier. Hooray! We've made it to the end of another exciting StatQuest. If you like this video and want to see more like it, please subscribe. And if you have ideas for future StatQuests, feel free to put them in the comments below.
Original Description
This video, which walks you through a simple regression in R, is a companion to the StatQuest on Linear Regression https://youtu.be/nk2CQITm_eo
If you want to just copy and paste the R code, you can get it from the StatQuest GitHub site: https://github.com/StatQuest/linear_regression_demo/blob/master/linear_regression_demo.R
For a complete index of all the StatQuest videos, check out:
https://statquest.org/video-index/
If you'd like to support StatQuest, please consider...
Patreon: https://www.patreon.com/statquest
...or...
YouTube Membership: https://www.youtube.com/channel/UCtYLUTtgS3k1Fg4y5tAhLbw/join
...buying one of my books, a study guide, a t-shirt or hoodie, or a song from the StatQuest store...
https://statquest.org/statquest-store/
...or just donating to StatQuest!
https://www.paypal.me/statquest
Lastly, if you want to keep up with me as I research and create new StatQuests, follow me on twitter:
https://twitter.com/joshuastarmer
#statquest #regression
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from StatQuest with Josh Starmer · StatQuest with Josh Starmer · 53 of 60
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
▶
54
55
56
57
58
59
60
Cutting Butter
StatQuest with Josh Starmer
onion-dice
StatQuest with Josh Starmer
R-squared, Clearly Explained!!!
StatQuest with Josh Starmer
Wrapping up dumplings for pot stickers.
StatQuest with Josh Starmer
The standard error, Clearly Explained!!!
StatQuest with Josh Starmer
That Dude (in the movies)
StatQuest with Josh Starmer
How to puree garlic
StatQuest with Josh Starmer
Confidence Intervals, Clearly Explained!!!
StatQuest with Josh Starmer
RPKM, FPKM and TPM, Clearly Explained!!!
StatQuest with Josh Starmer
Principal Component Analysis (PCA) clearly explained (2015)
StatQuest with Josh Starmer
StatQuest: RNA-seq - the problem with technical replicates
StatQuest with Josh Starmer
That's Alright
StatQuest with Josh Starmer
Christmas In Rio! (now on iTunes!)
StatQuest with Josh Starmer
Drawing and Interpreting Heatmaps
StatQuest with Josh Starmer
Rachel's Song (the ballad of Hazel Motes)
StatQuest with Josh Starmer
Deal With It
StatQuest with Josh Starmer
Say Your Goodbyes
StatQuest with Josh Starmer
Another Day
StatQuest with Josh Starmer
StatQuest: Linear Discriminant Analysis (LDA) clearly explained.
StatQuest with Josh Starmer
Maybe It'll Go Away
StatQuest with Josh Starmer
Nasty Weather
StatQuest with Josh Starmer
Roses
StatQuest with Josh Starmer
p-hacking and power calculations
StatQuest with Josh Starmer
I Love You
StatQuest with Josh Starmer
The Coldest Day of the Year
StatQuest with Josh Starmer
Psycho Killer
StatQuest with Josh Starmer
False Discovery Rates, FDR, clearly explained
StatQuest with Josh Starmer
A New Song
StatQuest with Josh Starmer
StatQuickie: Thresholds for Significance
StatQuest with Josh Starmer
Logs (logarithms), Clearly Explained!!!
StatQuest with Josh Starmer
Bar Charts Are Better than Pie Charts
StatQuest with Josh Starmer
Mr Hattie
StatQuest with Josh Starmer
StatQuickie: Which t test to use
StatQuest with Josh Starmer
Fisher's Exact Test and the Hypergeometric Distribution
StatQuest with Josh Starmer
Standard Deviation vs Standard Error, Clearly Explained!!!
StatQuest with Josh Starmer
StatQuest: DESeq2, part 1, Library Normalization
StatQuest with Josh Starmer
The Rainbow
StatQuest with Josh Starmer
StatQuest: edgeR, part 1, Library Normalization
StatQuest with Josh Starmer
The Main Ideas behind Probability Distributions
StatQuest with Josh Starmer
StatQuest: One or Two Tailed P-Values
StatQuest with Josh Starmer
Evil Genius
StatQuest with Josh Starmer
Sampling from a Distribution, Clearly Explained!!!
StatQuest with Josh Starmer
StatQuest: edgeR and DESeq2, part 2 - Independent Filtering
StatQuest with Josh Starmer
The Main Ideas of Fitting a Line to Data (The Main Ideas of Least Squares and Linear Regression.)
StatQuest with Josh Starmer
The Sum of Regrets
StatQuest with Josh Starmer
Lowess and Loess, Clearly Explained!!!
StatQuest with Josh Starmer
StatQuest: Hierarchical Clustering
StatQuest with Josh Starmer
StatQuest: K-nearest neighbors, Clearly Explained
StatQuest with Josh Starmer
Your Dark Side
StatQuest with Josh Starmer
Boxplots are Awesome!!!
StatQuest with Josh Starmer
What is a (mathematical) model?
StatQuest with Josh Starmer
Linear Regression, Clearly Explained!!!
StatQuest with Josh Starmer
Linear Regression in R, Step-by-Step
StatQuest with Josh Starmer
Maximum Likelihood, clearly explained!!!
StatQuest with Josh Starmer
Brothers
StatQuest with Josh Starmer
Using Linear Models for t-tests and ANOVA, Clearly Explained!!!
StatQuest with Josh Starmer
StatQuest: How to make a Mean Pizza Crust!!!
StatQuest with Josh Starmer
StatQuest: A gentle introduction to RNA-seq
StatQuest with Josh Starmer
I'm Alive
StatQuest with Josh Starmer
StatQuest: t-SNE, Clearly Explained
StatQuest with Josh Starmer
Related Reads
📰
📰
📰
📰
A lightweight workflow for keeping up with AI conference papers
Dev.to · Daniel
Why CitedEvidence Believes Great Researchers Read Less Than You Think
Medium · AI
How to Write a Literature Review That Actually Argues Something
Medium · Machine Learning
I Built a Personal Paper Engine to Stop Losing Research Papers
Dev.to · Ethan
🎓
Tutor Explanation
DeepCamp AI