-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmt-age.qmd
More file actions
351 lines (257 loc) · 7.09 KB
/
vmt-age.qmd
File metadata and controls
351 lines (257 loc) · 7.09 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
---
title: "Vehicle Miles Traveled (VMT) by Age"
---
This page shows median cumulative vehicle miles traveled (VMT) by vehicle age. Each chart displays the 50th quantile (median) of total mileage for a given combination of powertrain and vehicle type as vehicles age over time. Lines in the charts below are smoothed using loess smoothihng. The "Data" tab shows the raw data, which also includes the 25th and 75th quantiles. See the [about](about.html#depreciation) page for more details. Note the [observation counts](about.html#listing-counts) by powertrain and vehicle type as some combinations have few observations.
```{r}
#| include: false
source("setup.R")
# Convert age from months to years
data <- vmt_age |>
mutate(age_years = age_bin / 12)
# Helper function to apply loess smoothing to a group
smooth_group <- function(df, x_col, y_col, span = 0.5) {
if (nrow(df) < 10) {
df$miles_smooth <- df[[y_col]]
return(df)
}
tryCatch(
{
fit <- suppressWarnings(loess(df[[y_col]] ~ df[[x_col]], span = span))
df$miles_smooth <- predict(fit)
df
},
error = function(e) {
df$miles_smooth <- df[[y_col]]
df
}
)
}
# Apply smoothing to all data
data <- data |>
group_by(powertrain, vehicle_type, quantile) |>
group_modify(~ smooth_group(.x, "age_years", "miles")) |>
ungroup()
# Filter to 50th quantile for charts
chart_data <- data |>
filter(quantile == 50)
# Helper function to create a chart for a given facet
make_chart <- function(data, facet_var, facet_value, line_var) {
plot_data <- data |>
filter(.data[[facet_var]] == facet_value)
plot_data |>
group_by(.data[[line_var]]) |>
e_charts(age_years) |>
e_line(miles_smooth, symbol = "none", smooth = FALSE) |>
e_tooltip(
trigger = "axis",
formatter = htmlwidgets::JS(
"
function(params) {
var result = params[0].axisValueLabel + '<br/>';
params.forEach(function(item) {
result += item.marker + ' ' + item.seriesName + ': ' +
Math.round(item.value[1]).toLocaleString() + '<br/>';
});
return result;
}
"
)
) |>
e_x_axis(name = "Age (years)") |>
e_y_axis(name = "Miles") |>
e_legend(
show = TRUE,
orient = "horizontal",
left = "center",
bottom = 0,
width = "70%",
selector = list(
list(type = "all", title = "All"),
list(type = "inverse", title = "Inverse")
),
selectorPosition = "start"
) |>
e_grid(bottom = 100) |>
e_color(background = "transparent") |>
e_toolbox_feature(feature = "dataZoom") |>
e_toolbox_feature(feature = "saveAsImage")
}
```
## By Vehicle Type
*Click legend items to show/hide lines.*
::: {.grid}
::: {.g-col-6}
### Car
```{r}
make_chart(chart_data, "vehicle_type", "Car", "powertrain")
```
:::
::: {.g-col-6}
### CUV
```{r}
make_chart(chart_data, "vehicle_type", "CUV", "powertrain")
```
:::
::: {.g-col-6}
### SUV
```{r}
make_chart(chart_data, "vehicle_type", "SUV", "powertrain")
```
:::
::: {.g-col-6}
### Pickup
```{r}
make_chart(chart_data, "vehicle_type", "Pickup", "powertrain")
```
:::
::: {.g-col-6}
### Minivan
```{r}
make_chart(chart_data, "vehicle_type", "Minivan", "powertrain")
```
:::
:::
## By Powertrain
*Click legend items to show/hide lines.*
::: {.grid}
::: {.g-col-6}
### Gasoline
```{r}
make_chart(chart_data, "powertrain", "Gasoline", "vehicle_type")
```
:::
::: {.g-col-6}
### Flex Fuel (E85)
```{r}
make_chart(chart_data, "powertrain", "Flex Fuel (E85)", "vehicle_type")
```
:::
::: {.g-col-6}
### Hybrid Electric (HEV)
```{r}
make_chart(chart_data, "powertrain", "Hybrid Electric (HEV)", "vehicle_type")
```
:::
::: {.g-col-6}
### Plug-In Hybrid Electric (PHEV)
```{r}
make_chart(
chart_data,
"powertrain",
"Plug-In Hybrid Electric (PHEV)",
"vehicle_type"
)
```
:::
::: {.g-col-6}
### Battery Electric (BEV)
```{r}
make_chart(chart_data, "powertrain", "Battery Electric (BEV)", "vehicle_type")
```
:::
::: {.g-col-6}
### BEV (Tesla)
```{r}
make_chart(chart_data, "powertrain", "BEV (Tesla)", "vehicle_type")
```
:::
::: {.g-col-6}
### BEV (Non-Tesla)
```{r}
make_chart(chart_data, "powertrain", "BEV (Non-Tesla)", "vehicle_type")
```
:::
::: {.g-col-6}
### Diesel
```{r}
make_chart(chart_data, "powertrain", "Diesel", "vehicle_type")
```
:::
::: {.g-col-6}
### Fuel Cell
```{r}
make_chart(chart_data, "powertrain", "Fuel Cell", "vehicle_type")
```
:::
:::
## Data: VMT by Age
<center>
<a href="`r url_vmt_age`" style="display: inline-block; padding: 8px 16px; background-color: #0066cc; color: white; text-decoration: none; border-radius: 4px; margin-bottom: 10px;">Download data</a>
<div style="width:900px">
```{r}
data |>
mutate(
age_years = round(age_years, 2),
miles = round(miles, 0),
miles_smooth = round(miles_smooth, 0)
) |>
select(powertrain, vehicle_type, age_years, quantile, miles, miles_smooth) |>
rename(
Powertrain = powertrain,
`Vehicle Type` = vehicle_type,
`Age (years)` = age_years,
Quantile = quantile,
Miles = miles,
`Miles (Smoothed)` = miles_smooth
) |>
reactable::reactable(
searchable = TRUE,
highlight = TRUE,
filterable = TRUE,
defaultPageSize = 10,
showPageSizeOptions = TRUE,
pageSizeOptions = c(5, 10, 25, 50)
)
```
</div>
</center>
## Data: Annual VMT by Type
This table shows the estimated annual vehicle miles traveled (VMT) by vehicle type and powertrain. Annual VMT is calculated as the slope of the linear relationship between vehicle age and cumulative mileage.
<center>
<a href="`r url_vmt_annual_type`" style="display: inline-block; padding: 8px 16px; background-color: #0066cc; color: white; text-decoration: none; border-radius: 4px; margin-bottom: 10px;">Download data</a>
<div style="width:700px">
```{r}
vmt_annual_type |>
mutate(vmt_annual = round(vmt_annual, 0)) |>
rename(
`Vehicle Type` = vehicle_type,
Powertrain = powertrain,
`Annual VMT` = vmt_annual
) |>
reactable::reactable(
searchable = TRUE,
highlight = TRUE,
filterable = TRUE,
defaultPageSize = 10,
showPageSizeOptions = TRUE,
pageSizeOptions = c(5, 10, 25, 50)
)
```
</div>
</center>
## Data: Annual VMT by Model
This table shows the estimated annual vehicle miles traveled (VMT) by make, model, vehicle type, and powertrain. Annual VMT is calculated as the slope of the linear relationship between vehicle age and cumulative mileage.
<center>
<a href="`r url_vmt_annual_model`" style="display: inline-block; padding: 8px 16px; background-color: #0066cc; color: white; text-decoration: none; border-radius: 4px; margin-bottom: 10px;">Download data</a>
<div style="width:900px">
```{r}
vmt_annual_model |>
mutate(vmt_annual = round(vmt_annual, 0)) |>
rename(
Make = make,
Model = model,
`Vehicle Type` = vehicle_type,
Powertrain = powertrain,
`Annual VMT` = vmt_annual
) |>
reactable::reactable(
searchable = TRUE,
highlight = TRUE,
filterable = TRUE,
defaultPageSize = 10,
showPageSizeOptions = TRUE,
pageSizeOptions = c(5, 10, 25, 50)
)
```
</div>
</center>