R Tutorial : Visualizing parallel slopes models
Skills:
Data Literacy50%
Key Takeaways
This video tutorial demonstrates how to visualize parallel slopes models using R and the ggplot library, covering the mathematical representation of the model and how to extract and plot the fitted values using the augment function from the Broom package.
Full Transcript
in this video you'll learn more about visualizing parallel slopes models in this scatterplot we use color to differentiate the cars from 2008 from those from 1999 do you notice anything about the green points relative to the red points in this manner we have depicted three variables two numeric and one categorical on the same scatterplot thus this plot will enable us to visualize our parallel slopes model in the data space we're going to make use of a little high school algebra to inform our understanding of the geometry of our model first since our categorical explanatory variable year only has two levels we're going to define a binary variable called newer that takes on the value 1 for cars from 2008 and is zero otherwise then we can express our model mathematically using this equation we compute the fitted coefficients using LM so what happens when the cars are newer plugging in and simplifying reveals the equation for a line note that since displacement is our numeric explanatory variable the slope of this line is negative three point six one one miles per gallon per liter and the intercept is 36 point six seven eight miles per gallon the sum of the other two coefficients what about the older cars from 1999 in their case the value of newer is zero and plugging that into our equation also results in the equation for our line this line also has a slope of negative three point six one one miles per gallon per liter but now the intercept is just thirty five point two seven six miles per gallon thus our model consists of two parallel lines one for newer cars from 2008 and one for older cars from 1999 the two lines are parallel because they have the same slope but they're not the same line because they have different intercepts this is why models with one numeric explanatory variable and one categorical explanatory variable are called parallel slopes models notice how the geometry was informed by the mathematics in order to visualize our model we need to extract the necessary information about our model that was created la conceptually the 330 coefficients will give us that information however it's easier in ggplot to simply plot the fitted values and connect them with a line this process is streamlined by the augment function from the Broom package applying augment to our model will return a data frame with the fitted values attached like the one you see here note that the name for the variable that contains the fitted values is dot fitted and the name of the categorical variable is factor dot year dot finally we can use the Geo mind function to put the two lines on the scatter plot that we created previously we need to tell genome line to plot the fitted values rather than the observed values and those values only exist in the Augmented model object that we created previously now it's your turn to build some visualizations
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/multiple-and-logistic-regression at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
In this scatterplot, we use color to differentiate the cars from 2008 from those in 1999. Do you notice anything about the green points relative to the red points?
In this manner, we have depicted three variables---two numeric and one categorical---on the same scatterplot. Thus, this plot will enable us to visualize our parallel slopes model in the data space.
We're going to make use of a little high school algebra to inform our understanding of the geometry of our model.
First, since our categorical explanatory variable year has only two levels, we're going to define a binary variable called newer that takes on the value 1 for cars from 2008, and is 0 otherwise.
Then, we can express our model mathematically using this equation.
We compute the fitted coefficients using lm().
So what happens when the cars are newer? Plugging in and simplifying reveals the equation for a line. Note that since displacement is our numeric explanatory variable, the slope of this line is -3 point 611 mpg per liter, and the intercept is 36 point 678---the sum of the other two coefficients.
What about the older cars from 1999? In their case, the value of newer is 0, and plugging that in to our equation also results in the equation for a line. This line also has a slope of -3 point 611 mpg per liter, but now the intercept is just 35 point 276 mpg.
Thus, our model consists of two parallel lines: one for newer cars from 2008, and one for older cars from 1999. The two lines are parallel because they have the same slope, but they are not the same line, because they have different intercepts.
This is why models with one numeric explanatory variable and one categorical explanatory variable are called parallel slopes models. Notice how the geometry was informed by th
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: Data Literacy
View skill →
🎓
Tutor Explanation
DeepCamp AI