Course Outline
-
segmentGetting Started (Don't Skip This Part)
-
segmentStatistics and Data Science: A Modeling Approach
-
segmentPART I: EXPLORING VARIATION
-
segmentChapter 1 - Welcome to Statistics: A Modeling Approach
-
segmentChapter 2 - Understanding Data
-
segmentChapter 3 - Examining Distributions
-
segmentChapter 4 - Explaining Variation
-
segmentPART II: MODELING VARIATION
-
segmentChapter 5 - A Simple Model
-
segmentChapter 6 - Quantifying Error
-
segmentChapter 7 - Adding an Explanatory Variable to the Model
-
segmentChapter 8 - Digging Deeper into Group Models
-
segmentChapter 9 - Models with a Quantitative Explanatory Variable
-
segmentPART III: EVALUATING MODELS
-
segmentChapter 10 - The Logic of Inference
-
segmentChapter 11 - Model Comparison with F
-
segmentChapter 12 - Parameter Estimation and Confidence Intervals
-
segmentPART IV: MULTIVARIATE MODELS
-
segmentChapter 13 - Introduction to Multivariate Models
-
segmentChapter 14 - Multivariate Model Comparisons
-
segmentChapter 15 - Models with Interactions
-
15.1 Dogs in the Emergency Room
-
-
segmentChapter 16 - More Models with Interactions
-
segmentFinishing Up (Don't Skip This Part!)
-
segmentResources
list College / Advanced Statistics with R (ABCD)
Chapter 15 - Models with Interactions
15.1 Dogs in the Emergency Room
In previous chapters we introduced models with multiple predictor variables, and used these models to produce better predictions, with less error (usually), than we were able to achieve with single-predictor models. In this chapter we add one more twist to the story. All the multivariate models we have built so far are additive. In this chapter we will explain what that means, and then learn how to add interactions to our models.
Anxiety in the Emergency Room
Going to the emergency room can be an ordeal that provokes a lot of anxiety. Being sick or injured, then having to wait, can make people feel quite anxious, which probably isn’t very good for patients’ overall health. In this chapter we will analyze some data produced by a group of researchers trying to figure out how they might reduce anxiety among patients who come to the ER.
The data frame er
includes data from 84 patients who came to the emergency room and who were initially identified by ER personnel as appearing to exhibit some anxiety. Patients included in the study were asked to rate their own level of anxiety, on a scale of 1 to 10 twice: once on initial entry into the ER (base_anxiety
) and then again 30 minutes later (later_anxiety
).
Here’s a jitter plot that shows later_anxiety
as a function of base_anxiety
.
gf_jitter(later_anxiety ~ base_anxiety, data=er)
Could a Therapy Dog Help to Reduce Anxiety?
The data we’ve been analyzing came from a study in which patients initially identified as anxious were randomly assigned to one of two conditions on arrival at the ER. Patients in the Dog
group (n=43) were given a therapy dog to spend time with while they waited to see a doctor. Patients in the Control
group just waited as usual, with no access to a dog. The researchers hypothesized that therapy dogs might help to lessen patients’ anxiety as they waited.
source: https://www.barringtonbhw.com/the-benefits-of-therapy-dogs/
Add the color
argument into the jitter plot to color the dots according to which condition
each patient was assigned to (hint: there is a ~
involved).
require(coursekata)
# color the dots by condition
gf_jitter(later_anxiety ~ base_anxiety, data = er)
# color the dots by condition
gf_jitter(later_anxiety ~ base_anxiety, data = er, color=~condition)
ex() %>% check_function("gf_jitter") %>% {
check_arg(., "object") %>% check_equal()
check_arg(., "data") %>% check_equal()
check_arg(., "color") %>% check_equal()
}
Fitting the Additive Model
In the previous chapters we learned to fit these types of multivariate models; we will now start referring to them as additive or main effects models. Let’s look at the later_anxiety = condition + base_anxiety + other stuff model to see why it is called an additive model.
In the code window below, fit the multivariate model represented by this word equation (later anxiety = condition + base anxiety + other stuff) to the er
data set. Save the model as additive_model
, then print out the ANOVA table.
require(coursekata)
# Find the best-fitting additive model of this word equation
# later anxiety = condition + base anxiety + other stuff
additive_model <- lm()
# print out the ANOVA table
# Find the best-fitting additive model of this word equation
# later anxiety = condition + base anxiety + other stuff
additive_model <- lm(later_anxiety ~ condition + base_anxiety, data = er)
# print out the ANOVA table
supernova(additive_model)
ex() %>% {
check_object(., "additive_model") %>% check_equal()
check_output_expr(., "supernova(additive_model)")
}
Analysis of Variance Table (Type III SS)
Model: later_anxiety ~ condition + base_anxiety
SS df MS F PRE p
------------ --------------- | ------- -- ------- ------ ------ -----
Model (error reduced) | 469.512 2 234.756 51.147 0.5581 .0000
condition | 85.790 1 85.790 18.691 0.1875 .0000
base_anxiety | 409.948 1 409.948 89.317 0.5244 .0000
Error (from model) | 371.774 81 4.590
------------ --------------- | ------- -- ------- ------ ------ -----
Total (empty model) | 841.286 83 10.136
Visualizing and Interpreting the Additive Model
In the scatter plot below we have overlaid the additive model using this code:
gf_jitter(later_anxiety ~ base_anxiety, color = ~condition, data = er) %>%
gf_model(additive_model)
Use the code window below to print out the best-fitting parameter estimates of the additive model.
require(coursekata)
# find the best-fitting parameter estimates for this model
# later anxiety = condition + base anxiety + other stuff
# find the best-fitting parameter estimates for this model
# later anxiety = condition + base anxiety + other stuff
lm(later_anxiety ~ condition + base_anxiety, data = er)
ex() %>%
check_function("lm") %>%
check_result() %>%
check_equal()
Call:
lm(formula = later_anxiety ~ condition + base_anxiety, data = er)
Coefficients:
(Intercept) conditionDog base_anxiety
0.4317 -2.0279 0.8112