-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_workbook.Rmd
145 lines (105 loc) · 2.82 KB
/
05_workbook.Rmd
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
---
title: "Data Viz and Regression in R"
author: "Aaron R. Williams"
output:
html_document:
code_folding: show
toc: TRUE
toc_float: TRUE
editor_options:
chunk_output_type: console
---
```{r}
library(ggplot2)
library(tidyverse)
library(broom)
library(modelr)
theme_set(theme_minimal())
```
## Exercise 1
```{r}
# sample 300 observations and set ordinal factors to nominal
set.seed(20200622)
diamonds <- diamonds %>%
slice_sample(n = 300) %>%
mutate(across(where(is.factor), .fns = as.character))
# estimate a multiple linear regression model
diamonds_model1 <- lm(formula = price ~ carat + cut, data = diamonds)
## YOUR WORK GOES HERE
# estimate a multiple linear regression model
class(diamonds_model1)
augment(diamonds_model1) %>%
ggplot(aes(x = .fitted, y = .resid)) +
geom_point(alpha = 0.2) +
geom_smooth()
```
## Exercise 2
```{r}
# fit and tidy model 1
diamonds_model1 <- lm(formula = price ~ carat + cut, data = diamonds)
diamonds_model1_coefs <- tidy(
diamonds_model1,
conf.int = TRUE,
conf.level = 0.95
)
# fit and tidy model 2
diamonds_model2 <- lm(formula = price ~ carat + cut + x, data = diamonds)
diamonds_model2_coefs <- tidy(
diamonds_model2,
conf.int = TRUE,
conf.level = 0.95
)
## YOUR WORK GOES HERE
# combine the model results
models_coefs <- bind_rows(
`model1` = diamonds_model1_coefs,
`model2` = diamonds_model2_coefs,
.id = "model"
)
# visualize
## YOUR WORK GOES HERE
models_coefs %>%
ggplot(aes(x = estimate,
y = term,
xmin = conf.high,
xmax = conf.high,
color = model)) +
geom_vline(xintercept = 0) +
geom_pointrange(position = position_dodge(width = 0.5)) +
scale_x_continuous(limits = c(-15000, 15000),
labels = scales::dollar)
```
## Exercise 3
```{r}
# estimate a linear model for each country
many_models <- diamonds %>%
group_by() %>% ## YOUR WORK GOES HERE
nest(data = c()) %>% ## YOUR WORK GOES HERE
mutate(
model = map(
.x = data,
.f = ~glance(lm(formula = , data = .)) ## YOUR WORK GOES HERE
)
) %>%
ungroup()
# extract r^2 from each model
## YOUR WORK GOES HERE
# plot
## YOUR WORK GOES HERE
```
## Exercise 4
```{r}
# create a binary outcome variable
cars <- cars %>%
mutate(crash = as.numeric(dist > 25))
# fit a linear probability model
cars_lm <- ## YOUR WORK GOES HERE
# fit a logistic regression model
cars_glm <- ## YOUR WORK GOES HERE
# add conditional probabilities for both models
models <- data_grid(cars, speed = seq_range(speed, 1000)) %>%
add_predictions(cars_lm, var = "lm") %>%
add_predictions(cars_glm, type = "response", var = "glm")
# visualize
## YOUR WORK GOES HERE
```