-
Notifications
You must be signed in to change notification settings - Fork 145
/
baltimore_bridges.Rmd
188 lines (161 loc) · 5.42 KB
/
baltimore_bridges.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
library(scales)
theme_set(theme_light())
maryland_bridges <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2018-11-27/baltimore_bridges.csv") %>%
select(-vehicles) %>%
mutate(inspection_yr = inspection_yr + 2000,
decade = 10 * (yr_built %/% 10),
responsibility = fct_lump(responsibility, 4),
county = str_to_title(county))
```
```{r}
maryland_bridges %>%
filter(yr_built >= 1900) %>%
count(decade = 10 * (yr_built %/% 10), sort = TRUE) %>%
ggplot(aes(decade, n)) +
geom_line() +
expand_limits(y = 0) +
labs(y = "# of bridges in Baltimore built this decade")
```
How does the condition of bridges depend on how old it is?
```{r}
maryland_bridges %>%
filter(yr_built >= 1900) %>%
group_by(decade) %>%
summarize(pct_good = mean(bridge_condition == "Good"),
total = n()) %>%
ggplot(aes(decade, pct_good)) +
geom_line() +
scale_y_continuous(labels = percent_format()) +
expand_limits(y = 0)
```
Most bridges built before 1970 we would not consider in "Good" condition (though it doesn't matter how long before 1970 they were built), while a vast majority of bridges built since 2000 are in Good condition.
```{r}
maryland_bridges %>%
replace_na(list(responsibility = "Other")) %>%
count(responsibility = fct_lump(responsibility, 4), sort = TRUE) %>%
mutate(responsibility = fct_reorder(responsibility, n)) %>%
ggplot(aes(responsibility, n)) +
geom_col() +
coord_flip()
```
```{r}
maryland_bridges %>%
filter(yr_built >= 1900) %>%
group_by(responsibility = fct_lump(responsibility, 4),
decade) %>%
summarize(pct_good = mean(bridge_condition == "Good"),
total = n()) %>%
filter(responsibility != "Other") %>%
ggplot(aes(decade, pct_good, color = responsibility)) +
geom_line() +
scale_y_continuous(labels = percent_format()) +
expand_limits(y = 0) +
labs(y = "% of bridges rated 'Good'")
```
The County Highway Agency consistently has the most bridges rated as "Good" from before 1970. State Toll Authority is underperforming in bridges built in the 1990s.
```{r}
maryland_bridges %>%
ggplot(aes(avg_daily_traffic)) +
geom_histogram() +
scale_x_log10(labels = comma_format())
```
```{r}
maryland_bridges %>%
filter(yr_built >= 1990) %>%
group_by(traffic_category = cut(avg_daily_traffic, c(0, 1000, 10000, Inf),
labels = c("<1000", "1000-10,000", "10,000+"))) %>%
summarize(pct_good = mean(bridge_condition == "Good"),
total = n())
```
What does the traffic look like geographically?
```{r}
maryland_bridges %>%
ggplot(aes(long, lat, color = avg_daily_traffic)) +
borders("state", regions = "Maryland") +
geom_point() +
scale_color_gradient2(low = "blue",
high = "red",
midpoint = log10(median(maryland_bridges$avg_daily_traffic)),
trans = "log10",
labels = comma_format()) +
coord_map() +
theme_void()
```
```{r}
maryland_bridges %>%
ggplot(aes(long, lat, color = bridge_condition)) +
borders("state", regions = "Maryland") +
geom_point(size = 1) +
coord_map() +
theme_void()
```
```{r}
maryland_bridges %>%
filter(yr_built >= 1900) %>%
ggplot(aes(long, lat, color = county)) +
borders("state", regions = "Maryland") +
geom_point(size = 1) +
coord_map() +
theme_void()
```
```{r}
maryland_bridges %>%
filter(yr_built >= 1900) %>%
group_by(county, decade) %>%
summarize(pct_good = mean(bridge_condition == "Good"),
total = n()) %>%
arrange(county, decade) %>%
ggplot(aes(decade, pct_good, color = county)) +
geom_line() +
scale_y_continuous(labels = percent_format()) +
expand_limits(y = 0) +
labs(y = "% of bridges rated 'Good'")
```
### Effect of county, responsibility, traffic and time on bridge condition
```{r}
# fit a logistic model
bridges <- maryland_bridges %>%
filter(yr_built >= 1900)
library(broom)
library(splines)
simple_model <- bridges %>%
mutate(good = bridge_condition == "Good") %>%
glm(good ~ ns(yr_built, 4), data = ., family = "binomial")
model <- bridges %>%
mutate(good = bridge_condition == "Good") %>%
glm(good ~ ns(yr_built, 4) + responsibility + county, data = ., family = "binomial")
augment(simple_model, bridges, type.predict = "response") %>%
ggplot(aes(yr_built, .fitted)) +
geom_line() +
expand_limits(y = 0) +
scale_y_continuous(labels = percent_format()) +
labs(y = "Predicted probability a bridge is rated 'Good'")
augment(model, bridges, type.predict = "response") %>%
ggplot(aes(yr_built, .fitted, color = responsibility)) +
geom_line() +
expand_limits(y = 0) +
facet_wrap(~ county) +
scale_y_continuous(labels = percent_format()) +
labs(y = "Predicted probability a bridge is rated 'Good'")
```
Controlled for the overall trend in change over time, what's the effect of being in a particular county or responsibility?
```{r}
model %>%
tidy(conf.int = TRUE) %>%
filter(str_detect(term, "responsibility|county")) %>%
mutate(term = reorder(term, estimate)) %>%
ggplot(aes(estimate, term)) +
geom_point() +
geom_errorbarh(aes(xmin = conf.low, xmax = conf.high)) +
geom_vline(xintercept = 0, color = "red", lty = 2)
```
We haven't found evidence of an effect of geography or ownership on bridge condition, once we control for time.