Using U.S. ACS census data, specifically the Places designation,
we are exploring patterns in geographic data about commuting to and from work in the U.S. We explore: - how commute time varies in different locations throughout the U.S. -how commute time and commute type correlate with income, specifically income inequality -to what degree commuting variables can predict income inequality.
We seek to: find areas where the financial and temporal burden of commuting may be strongest, especially in lower income areas. identify areas where pedestrian infrastructure and public transportation investment would have maximum impact on financial wellbeing.
Year 2020 is missing from the BigQuery version of the data. The following SQL query was used to gather relevant variables:
SELECT geo_id, total_pop, commuters_by_public_transportation, median_income, income_per_capita, median_rent, percent_income_spent_on_rent, aggregate_travel_time_to_work, commuters_by_car_truck_van, commuters_by_carpool, commuters_by_subway_or_elevated, commuters_drove_alone, walked_to_work, worked_at_home, gini_index, year
FROM
(SELECT geo_id, total_pop, commuters_by_public_transportation, median_income, income_per_capita, median_rent, percent_income_spent_on_rent, aggregate_travel_time_to_work, commuters_by_car_truck_van, commuters_by_carpool, commuters_by_subway_or_elevated, commuters_drove_alone, walked_to_work, worked_at_home, gini_index, '2010' as year
FROM `bigquery-public-data.census_bureau_acs.place_2010_1yr`)
UNION ALL (SELECT geo_id, total_pop, commuters_by_public_transportation, median_income, income_per_capita, median_rent, percent_income_spent_on_rent, aggregate_travel_time_to_work, commuters_by_car_truck_van, commuters_by_carpool, commuters_by_subway_or_elevated, commuters_drove_alone, walked_to_work, worked_at_home, gini_index, '2011' as year
FROM `bigquery-public-data.census_bureau_acs.place_2011_1yr`)
UNION ALL (SELECT geo_id, total_pop, commuters_by_public_transportation, median_income, income_per_capita, median_rent, percent_income_spent_on_rent, aggregate_travel_time_to_work, commuters_by_car_truck_van, commuters_by_carpool, commuters_by_subway_or_elevated, commuters_drove_alone, walked_to_work, worked_at_home, gini_index, '2012' as year
FROM `bigquery-public-data.census_bureau_acs.place_2012_1yr`)
UNION ALL (SELECT geo_id, total_pop, commuters_by_public_transportation, median_income, income_per_capita, median_rent, percent_income_spent_on_rent, aggregate_travel_time_to_work, commuters_by_car_truck_van, commuters_by_carpool, commuters_by_subway_or_elevated, commuters_drove_alone, walked_to_work, worked_at_home, gini_index, '2013' as year
FROM `bigquery-public-data.census_bureau_acs.place_2013_1yr`)
UNION ALL (SELECT geo_id, total_pop, commuters_by_public_transportation, median_income, income_per_capita, median_rent, percent_income_spent_on_rent, aggregate_travel_time_to_work, commuters_by_car_truck_van, commuters_by_carpool, commuters_by_subway_or_elevated, commuters_drove_alone, walked_to_work, worked_at_home, gini_index, '2014' as year
FROM `bigquery-public-data.census_bureau_acs.place_2014_1yr`)
UNION ALL (SELECT geo_id, total_pop, commuters_by_public_transportation, median_income, income_per_capita, median_rent, percent_income_spent_on_rent, aggregate_travel_time_to_work, commuters_by_car_truck_van, commuters_by_carpool, commuters_by_subway_or_elevated, commuters_drove_alone, walked_to_work, worked_at_home, gini_index, '2015' as year
FROM `bigquery-public-data.census_bureau_acs.place_2015_1yr` )
UNION ALL (SELECT geo_id, total_pop, commuters_by_public_transportation, median_income, income_per_capita, median_rent, percent_income_spent_on_rent, aggregate_travel_time_to_work, commuters_by_car_truck_van, commuters_by_carpool, commuters_by_subway_or_elevated, commuters_drove_alone, walked_to_work, worked_at_home, gini_index, '2016' as year
FROM `bigquery-public-data.census_bureau_acs.place_2016_1yr`)
UNION ALL (SELECT geo_id, total_pop, commuters_by_public_transportation, median_income, income_per_capita, median_rent, percent_income_spent_on_rent, aggregate_travel_time_to_work, commuters_by_car_truck_van, commuters_by_carpool, commuters_by_subway_or_elevated, commuters_drove_alone, walked_to_work, worked_at_home, gini_index, '2017' as year
FROM `bigquery-public-data.census_bureau_acs.place_2017_1yr`)
UNION ALL (SELECT geo_id, total_pop, commuters_by_public_transportation, median_income, income_per_capita, median_rent, percent_income_spent_on_rent, aggregate_travel_time_to_work, commuters_by_car_truck_van, commuters_by_carpool, commuters_by_subway_or_elevated, commuters_drove_alone, walked_to_work, worked_at_home, gini_index, '2018' as year
FROM `bigquery-public-data.census_bureau_acs.place_2018_1yr`)
UNION ALL (SELECT geo_id, total_pop, commuters_by_public_transportation, median_income, income_per_capita, median_rent, percent_income_spent_on_rent, aggregate_travel_time_to_work, commuters_by_car_truck_van, commuters_by_carpool, commuters_by_subway_or_elevated, commuters_drove_alone, walked_to_work, worked_at_home, gini_index, '2019' as year
FROM `bigquery-public-data.census_bureau_acs.place_2019_1yr`)
UNION ALL (SELECT geo_id, total_pop, commuters_by_public_transportation, median_income, income_per_capita, median_rent, percent_income_spent_on_rent, aggregate_travel_time_to_work, commuters_by_car_truck_van, commuters_by_carpool, commuters_by_subway_or_elevated, commuters_drove_alone, walked_to_work, worked_at_home, gini_index, '2021' as year
FROM `bigquery-public-data.census_bureau_acs.place_2021_1yr` )
There are a lot of NA values, especially in the commuter-type variables. Let’s fill these with 0s for the summaries, assuming that ‘NA’ means that transportation type is unavailable in those locations. We do this for summary statistics only, leaving NAs in the data for correlation and modeling.
“Places” according to this particular ACS dataset, are geographical areas with a population above 20000. These mostly include towns and cities, but come at the expense of excluding rural communities. In a future project, I may look at similar data per county or congressional district for a more nuanced and inclusive perspective.
Let’s look at an aggregated summary of the variables we are exploring:
| year | commuters_by_public_transportation | commuters_by_car_truck_van | walked_to_work | worked_at_home |
|---|---|---|---|---|
| 2010 | 7823 | 48918 | 2801 | 2716 |
| 2011 | 7939 | 50092 | 2924 | 2718 |
| 2012 | 8250 | 54719 | 3083 | 3000 |
| 2013 | 8410 | 53114 | 3029 | 2959 |
| 2014 | 8582 | 54719 | 3027 | 3091 |
| 2015 | 8664 | 55002 | 3120 | 3243 |
| 2016 | 8486 | 54806 | 3078 | 3548 |
| 2017 | 8348 | 54046 | 3057 | 3646 |
| 2018 | 8087 | 53243 | 3007 | 3625 |
| 2019 | 8158 | 53232 | 3033 | 3970 |
| 2021 | 4054 | 38234 | 2088 | 12857 |
| year | commuters_by_public_transportation | commuters_by_car_truck_van | walked_to_work | worked_at_home |
|---|---|---|---|---|
| 2010 | 87543 | 110353 | 16588 | 9042 |
| 2011 | 88804 | 110445 | 17186 | 8700 |
| 2012 | 90080 | 111999 | 17091 | 9121 |
| 2013 | 91896 | 113426 | 17170 | 9217 |
| 2014 | 94063 | 115769 | 17219 | 9309 |
| 2015 | 95490 | 118323 | 17918 | 9764 |
| 2016 | 94569 | 120179 | 17459 | 10524 |
| 2017 | 94069 | 121843 | 17982 | 10908 |
| 2018 | 91446 | 122070 | 16794 | 11050 |
| 2019 | 91625 | 122308 | 18090 | 11918 |
| 2021 | 54583 | 101151 | 14122 | 49703 |
The drastic increase in people working from home likely explains the dip in commuters of all types in 2021. This is obviously due to the COVID-19 Pandemic: general statements about commuter patterns and income likely won’t hold under normal circumstances.
Let’s examine some general, nationwide patterns and trends before identifying specific areas.
# drastic decrease in commute time during remote work
# note that NAs have been dropped
commuter_data %>% group_by(year) %>%
summarise_at(c('commuters_by_public_transportation', 'commuters_by_car_truck_van', 'walked_to_work', 'worked_at_home'), ~round(mean(., na.rm=TRUE), 0)) %>% pivot_longer(cols = !year) %>%
ggplot() + geom_line(aes(x = year, y=value, color=name))
Let’s add calculated fields to express the number of commuter types in terms of the population of each area. This is much more useful for analysis. For now, we will focus on 2021 data.
# calculated fields
commuters_by_pop <- transform(commuter_data,
commuters_by_public_transportation = commuters_by_public_transportation/total_pop,
commuters_by_car_truck_van = commuters_by_car_truck_van/total_pop,
commuters_by_subway_or_elevated = commuters_by_subway_or_elevated/total_pop,
walked_to_work = walked_to_work/total_pop,
worked_at_home = worked_at_home/total_pop,
commuters_by_carpool = commuters_by_carpool/total_pop,
commuters_drove_alone = commuters_drove_alone/total_pop,
aggregate_travel_time_to_work = aggregate_travel_time_to_work/total_pop)
ggplot(commuters_by_pop) +
geom_point(mapping = aes(x = worked_at_home,
y = median_income,
color = gini_index)) +
scale_x_continuous(trans='log10') +
scale_y_continuous(trans='log10') +
scale_colour_continuous(type = 'viridis', direction = -1) +
facet_wrap(~year, nrow = 3) +
theme()
## Warning: Removed 3447 rows containing missing values (`geom_point()`).
As a sample, notice how drastically the number of workers from home increased in 2021 compared to previous years.
The Gini Coefficient is a measure of how much income deviates from ‘perfect equality’: the higher the coefficient, the bigger the expected income gap is between individuals in the given area, indicating greater income inequality. With that said, it makes sense that areas where people spend less on rent and have a higher median salary would correlate to a lower Gini Index. Next I examine the Spearman Correlation with the Gini Index, focusing on transportation variables only:
commuters_by_pop %>% filter(year == 2021) %>% select(c(commuters_by_public_transportation,commuters_by_car_truck_van, commuters_by_subway_or_elevated, walked_to_work, aggregate_travel_time_to_work, commuters_by_carpool, commuters_drove_alone, worked_at_home, gini_index)) %>%
cor(use = "pairwise.complete.obs", method = 'spearman')
## commuters_by_public_transportation
## commuters_by_public_transportation 1.0000000
## commuters_by_car_truck_van -0.6610452
## commuters_by_subway_or_elevated 0.5060045
## walked_to_work 0.6558665
## aggregate_travel_time_to_work 0.1141948
## commuters_by_carpool -0.1702120
## commuters_drove_alone -0.7088294
## worked_at_home 0.2631814
## gini_index 0.3626431
## commuters_by_car_truck_van
## commuters_by_public_transportation -0.6610452
## commuters_by_car_truck_van 1.0000000
## commuters_by_subway_or_elevated -0.3909371
## walked_to_work -0.4003528
## aggregate_travel_time_to_work 0.1711840
## commuters_by_carpool 0.4328659
## commuters_drove_alone 0.9532458
## worked_at_home -0.4126037
## gini_index -0.3970475
## commuters_by_subway_or_elevated
## commuters_by_public_transportation 0.50600452
## commuters_by_car_truck_van -0.39093705
## commuters_by_subway_or_elevated 1.00000000
## walked_to_work 0.25669616
## aggregate_travel_time_to_work 0.35660905
## commuters_by_carpool -0.03851464
## commuters_drove_alone -0.43934061
## worked_at_home 0.22883085
## gini_index 0.09727719
## walked_to_work aggregate_travel_time_to_work
## commuters_by_public_transportation 0.6558665 0.11419483
## commuters_by_car_truck_van -0.4003528 0.17118403
## commuters_by_subway_or_elevated 0.2566962 0.35660905
## walked_to_work 1.0000000 -0.23490578
## aggregate_travel_time_to_work -0.2349058 1.00000000
## commuters_by_carpool -0.2748211 0.34026162
## commuters_drove_alone -0.3873235 0.08260582
## worked_at_home 0.3715723 -0.14557415
## gini_index 0.4008405 -0.40631042
## commuters_by_carpool commuters_drove_alone
## commuters_by_public_transportation -0.17021199 -0.70882940
## commuters_by_car_truck_van 0.43286590 0.95324581
## commuters_by_subway_or_elevated -0.03851464 -0.43934061
## walked_to_work -0.27482114 -0.38732346
## aggregate_travel_time_to_work 0.34026162 0.08260582
## commuters_by_carpool 1.00000000 0.19646742
## commuters_drove_alone 0.19646742 1.00000000
## worked_at_home -0.52924511 -0.29406622
## gini_index -0.43822750 -0.31828754
## worked_at_home gini_index
## commuters_by_public_transportation 0.2631814 0.36264314
## commuters_by_car_truck_van -0.4126037 -0.39704753
## commuters_by_subway_or_elevated 0.2288309 0.09727719
## walked_to_work 0.3715723 0.40084054
## aggregate_travel_time_to_work -0.1455742 -0.40631042
## commuters_by_carpool -0.5292451 -0.43822750
## commuters_drove_alone -0.2940662 -0.31828754
## worked_at_home 1.0000000 0.24501418
## gini_index 0.2450142 1.00000000
I will now explore to what extent transportation-related variables can predict income inequality. For example, aggregate travel time to work is negatively correlated to Gini, indicating that high income inequality correlates with lower commute times. This, along with the ‘worked at home’ and ‘commuters by public transportation’ variables, indicates that areas with more workers from home, at least in 2021, experienced higher income inequality.
# Aggregate Travel Time - try poly 2
commuters_by_pop %>% filter(year == 2021 & aggregate_travel_time_to_work != 0) %>%
ggplot() +
geom_point(mapping = aes(x = aggregate_travel_time_to_work,
y = gini_index,
color = median_income)) +
#eom_smooth(aes(x = gini_index, y = median_income)) +
scale_colour_continuous(type = 'viridis', direction = -1) +
geom_smooth(mapping = aes(x = aggregate_travel_time_to_work,
y =gini_index),
method = 'loess') +
labs(title = "Gini Index and Aggregate Travel Time",
subtitle = "with median income") +
ylab("Gini Index") + xlab("Aggregate Travel Time \n/ Total Population")
## `geom_smooth()` using formula = 'y ~ x'
# Commuters by Public Transport - try poly 3
commuters_by_pop %>% filter(year == 2021 & commuters_by_public_transportation != 0) %>%
ggplot() +
geom_point(mapping = aes(x = log(commuters_by_public_transportation),
y = gini_index,
color = median_income)) +
scale_colour_continuous(type = 'viridis', direction = -1) +
geom_smooth(mapping = aes(x = log(commuters_by_public_transportation),
y = gini_index),
method = 'loess') +
labs(title = "Gini Index and Public Transport",
subtitle = "with median income") +
ylab("Gini Index") + xlab("log(Public Transport Commuters \n/ Total Population)")
## `geom_smooth()` using formula = 'y ~ x'
# Worked at Home - sqrt relationship
commuters_by_pop %>% filter(year == 2021 & worked_at_home != 0) %>%
ggplot() +
geom_point(mapping = aes(x = worked_at_home,
y = gini_index,
color = median_income)) +
scale_colour_continuous(type = 'viridis', direction = -1) +
geom_smooth(mapping = aes(x = worked_at_home,
y = gini_index,),
method = 'loess') +
labs(title = "Gini Index and Workers from Home",
subtitle = "with median income") +
ylab("Gini Index") + xlab("Workers from Home \n/ Total Population")
## `geom_smooth()` using formula = 'y ~ x'
Note that a few areas where the aggregate travel time was missing were dropped from the plot above.
Sparser data at the upper and lower tails of independent variables will likely affect the model’s residuals and confidence intervals.
Working from home was seemingly a white-collar privilege during the pandemic, with high median income areas having more workers from home overall.
This indicates that transportation variables have some relationship with the Gini Index: I attempt to model this next using Linear Models.
subset_2021 <- commuters_by_pop %>% filter(year==2021)
model_1 <- glm(gini_index ~ commuters_by_public_transportation +
commuters_by_car_truck_van +
commuters_by_subway_or_elevated +
walked_to_work +
commuters_by_carpool +
worked_at_home +
aggregate_travel_time_to_work,
data = subset_2021,
na.action = 'na.exclude')
summ(model_1)
## MODEL INFO:
## Observations: 146 (488 missing obs. deleted)
## Dependent Variable: gini_index
## Type: Linear regression
##
## MODEL FIT:
## χ²(7) = 0.10, p = 0.00
## Pseudo-R² (Cragg-Uhler) = -0.01
## Pseudo-R² (McFadden) = -0.13
## AIC = -550.66, BIC = -523.81
##
## Standard errors: MLE
## -----------------------------------------------------------------------
## Est. S.E. t val. p
## ---------------------------------------- ------- ------ -------- ------
## (Intercept) 0.65 0.04 17.41 0.00
## commuters_by_public_transportation 0.16 0.24 0.69 0.49
## commuters_by_car_truck_van -0.19 0.09 -2.10 0.04
## commuters_by_subway_or_elevated 0.95 1.27 0.75 0.45
## walked_to_work 0.31 0.34 0.89 0.37
## commuters_by_carpool -1.03 0.27 -3.76 0.00
## worked_at_home -0.18 0.07 -2.67 0.01
## aggregate_travel_time_to_work -0.01 0.00 -2.67 0.01
## -----------------------------------------------------------------------
##
## Estimated dispersion parameter = 0
predictions_1 <- predict(model_1, interval = 'confidence')
# Trim high p-value variables for M2
model_2 <- glm(gini_index ~
commuters_by_car_truck_van +
commuters_by_carpool +
worked_at_home +
aggregate_travel_time_to_work,
data = subset_2021,
na.action = 'na.exclude')
summ(model_2)
## MODEL INFO:
## Observations: 197 (437 missing obs. deleted)
## Dependent Variable: gini_index
## Type: Linear regression
##
## MODEL FIT:
## χ²(4) = 0.10, p = 0.00
## Pseudo-R² (Cragg-Uhler) = -0.01
## Pseudo-R² (McFadden) = -0.09
## AIC = -731.46, BIC = -711.76
##
## Standard errors: MLE
## ------------------------------------------------------------------
## Est. S.E. t val. p
## ----------------------------------- ------- ------ -------- ------
## (Intercept) 0.66 0.03 23.72 0.00
## commuters_by_car_truck_van -0.26 0.05 -5.00 0.00
## commuters_by_carpool -0.85 0.24 -3.47 0.00
## worked_at_home -0.14 0.06 -2.26 0.02
## aggregate_travel_time_to_work -0.01 0.00 -3.20 0.00
## ------------------------------------------------------------------
##
## Estimated dispersion parameter = 0
predictions_2 <- predict(model_2, interval = 'confidence')
ggplot() +
geom_point(aes(x = subset_2021$gini_index, y = predictions_2, color = "Model 2"), alpha = 0.7) +
geom_point(aes(x = subset_2021$gini_index, y = predictions_1, color = "Model 1"), alpha = 0.7) +
geom_abline(aes(slope = 1, intercept = 0), color = "darkgrey") +
labs(title = 'Predictions vs. Actual Values') +
xlab('Gini Index') +
ylab('Predictions')
## Warning: Removed 437 rows containing missing values (`geom_point()`).
## Warning: Removed 488 rows containing missing values (`geom_point()`).
Comparing predictions on the test set to actual values, these line up along the y = x line fairly well, considering the simplicity of the models used.
Now I will check whether the residuals are evenly distributed around the dependent variable, Gini Index.
# Add residuals of each model to the main dataframe.
subset_2021$resid_1 <- residuals(model_1)
subset_2021$resid_2 <- residuals(model_2)
# residuals of M1 and M2
ggplot(subset_2021) +
geom_point(aes(x = gini_index, y = resid_2, color = 'Model 2'), na.rm=TRUE) +
geom_point(aes(x = gini_index, y = resid_1, color = 'Model 1'), na.rm=TRUE) +
labs(title = "Residual Plot") +
xlab("Gini Index, Observed") +
ylab("Residuals") +
geom_abline(slope = 0, intercept = 0, color = 'lightblue')
plot(model_1, which = 1)
plot(model_2, which = 1)
\(y-\hat y\) : Negative residuals indicate the model overshot in prediction.
As intuited, the model didn’t capture any strong relationship at extreme values of Gini.
We can check each independent variable against the dependent variable, Gini:
data_long <- subset_2021 %>%
pivot_longer(cols = c('commuters_by_car_truck_van',
'commuters_by_carpool',
'worked_at_home',
'aggregate_travel_time_to_work'))
ggplot(data_long) +
geom_point(aes(x = value, y= gini_index, color = median_income)) +
xlab("Value / Total Pop") +
ylab("Gini Index") +
scale_colour_continuous(type = 'viridis') +
labs(title="Dependent Variables") +
theme(axis.text.x = element_text(angle = 25, hjust = 1)) +
facet_wrap(~name, scales = "free_x") +
geom_smooth(mapping = aes(x=value, y = gini_index))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 1325 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 1325 rows containing missing values (`geom_point()`).
Wider confidence intervals at the tails indicate that predictions here tend to be uncertain due to sparsity. This is reflected in the residuals plots as well.
Next, I examine variable interactions and data transformations to zero in on a more informative model.
First, I examine all 2-way interactions, expanding the variables of interest to include all transportation variables.
model_3 <- lm(gini_index ~ (commuters_by_public_transportation +
commuters_by_car_truck_van +
commuters_by_subway_or_elevated +
walked_to_work +
commuters_by_carpool +
worked_at_home +
aggregate_travel_time_to_work)^2,
data = subset_2021,
na.action = 'na.exclude')
summ(model_3)
## MODEL INFO:
## Observations: 146 (488 missing obs. deleted)
## Dependent Variable: gini_index
## Type: OLS linear regression
##
## MODEL FIT:
## F(28,117) = 3.34, p = 0.00
## R² = 0.44
## Adj. R² = 0.31
##
## Standard errors: OLS
## ---------------------------------------------------------------------------------
## Est.
## ------------------------------------------------------------------------ --------
## (Intercept) 0.47
## commuters_by_public_transportation 0.37
## commuters_by_car_truck_van -0.05
## commuters_by_subway_or_elevated -47.67
## walked_to_work 4.77
## commuters_by_carpool -0.37
## worked_at_home -0.40
## aggregate_travel_time_to_work 0.02
## commuters_by_public_transportation:commuters_by_car_truck_van 3.18
## commuters_by_public_transportation:commuters_by_subway_or_elevated 19.24
## commuters_by_public_transportation:walked_to_work -41.26
## commuters_by_public_transportation:commuters_by_carpool 2.71
## commuters_by_public_transportation:worked_at_home 4.22
## commuters_by_public_transportation:aggregate_travel_time_to_work -0.09
## commuters_by_car_truck_van:commuters_by_subway_or_elevated -8.81
## commuters_by_car_truck_van:walked_to_work -19.72
## commuters_by_car_truck_van:commuters_by_carpool 3.58
## commuters_by_car_truck_van:worked_at_home 2.57
## commuters_by_car_truck_van:aggregate_travel_time_to_work -0.04
## commuters_by_subway_or_elevated:walked_to_work 104.14
## commuters_by_subway_or_elevated:commuters_by_carpool 369.02
## commuters_by_subway_or_elevated:worked_at_home 43.45
## commuters_by_subway_or_elevated:aggregate_travel_time_to_work 2.30
## walked_to_work:commuters_by_carpool 5.69
## walked_to_work:worked_at_home -4.99
## walked_to_work:aggregate_travel_time_to_work 0.34
## commuters_by_carpool:worked_at_home 6.00
## commuters_by_carpool:aggregate_travel_time_to_work -0.25
## worked_at_home:aggregate_travel_time_to_work -0.09
## ---------------------------------------------------------------------------------
##
## ---------------------------------------------------------------------------------
## S.E.
## ------------------------------------------------------------------------ --------
## (Intercept) 0.29
## commuters_by_public_transportation 2.86
## commuters_by_car_truck_van 0.83
## commuters_by_subway_or_elevated 23.05
## walked_to_work 4.22
## commuters_by_carpool 3.84
## worked_at_home 0.68
## aggregate_travel_time_to_work 0.03
## commuters_by_public_transportation:commuters_by_car_truck_van 3.94
## commuters_by_public_transportation:commuters_by_subway_or_elevated 149.97
## commuters_by_public_transportation:walked_to_work 24.89
## commuters_by_public_transportation:commuters_by_carpool 30.52
## commuters_by_public_transportation:worked_at_home 7.32
## commuters_by_public_transportation:aggregate_travel_time_to_work 0.16
## commuters_by_car_truck_van:commuters_by_subway_or_elevated 65.67
## commuters_by_car_truck_van:walked_to_work 12.22
## commuters_by_car_truck_van:commuters_by_carpool 11.63
## commuters_by_car_truck_van:worked_at_home 2.46
## commuters_by_car_truck_van:aggregate_travel_time_to_work 0.07
## commuters_by_subway_or_elevated:walked_to_work 154.85
## commuters_by_subway_or_elevated:commuters_by_carpool 201.34
## commuters_by_subway_or_elevated:worked_at_home 57.02
## commuters_by_subway_or_elevated:aggregate_travel_time_to_work 2.12
## walked_to_work:commuters_by_carpool 42.54
## walked_to_work:worked_at_home 8.79
## walked_to_work:aggregate_travel_time_to_work 0.34
## commuters_by_carpool:worked_at_home 8.28
## commuters_by_carpool:aggregate_travel_time_to_work 0.28
## worked_at_home:aggregate_travel_time_to_work 0.07
## ---------------------------------------------------------------------------------
##
## ---------------------------------------------------------------------------------
## t val.
## ------------------------------------------------------------------------ --------
## (Intercept) 1.64
## commuters_by_public_transportation 0.13
## commuters_by_car_truck_van -0.06
## commuters_by_subway_or_elevated -2.07
## walked_to_work 1.13
## commuters_by_carpool -0.10
## worked_at_home -0.59
## aggregate_travel_time_to_work 0.81
## commuters_by_public_transportation:commuters_by_car_truck_van 0.81
## commuters_by_public_transportation:commuters_by_subway_or_elevated 0.13
## commuters_by_public_transportation:walked_to_work -1.66
## commuters_by_public_transportation:commuters_by_carpool 0.09
## commuters_by_public_transportation:worked_at_home 0.58
## commuters_by_public_transportation:aggregate_travel_time_to_work -0.53
## commuters_by_car_truck_van:commuters_by_subway_or_elevated -0.13
## commuters_by_car_truck_van:walked_to_work -1.61
## commuters_by_car_truck_van:commuters_by_carpool 0.31
## commuters_by_car_truck_van:worked_at_home 1.05
## commuters_by_car_truck_van:aggregate_travel_time_to_work -0.64
## commuters_by_subway_or_elevated:walked_to_work 0.67
## commuters_by_subway_or_elevated:commuters_by_carpool 1.83
## commuters_by_subway_or_elevated:worked_at_home 0.76
## commuters_by_subway_or_elevated:aggregate_travel_time_to_work 1.09
## walked_to_work:commuters_by_carpool 0.13
## walked_to_work:worked_at_home -0.57
## walked_to_work:aggregate_travel_time_to_work 1.01
## commuters_by_carpool:worked_at_home 0.72
## commuters_by_carpool:aggregate_travel_time_to_work -0.90
## worked_at_home:aggregate_travel_time_to_work -1.23
## ---------------------------------------------------------------------------------
##
## -------------------------------------------------------------------------------
## p
## ------------------------------------------------------------------------ ------
## (Intercept) 0.10
## commuters_by_public_transportation 0.90
## commuters_by_car_truck_van 0.95
## commuters_by_subway_or_elevated 0.04
## walked_to_work 0.26
## commuters_by_carpool 0.92
## worked_at_home 0.56
## aggregate_travel_time_to_work 0.42
## commuters_by_public_transportation:commuters_by_car_truck_van 0.42
## commuters_by_public_transportation:commuters_by_subway_or_elevated 0.90
## commuters_by_public_transportation:walked_to_work 0.10
## commuters_by_public_transportation:commuters_by_carpool 0.93
## commuters_by_public_transportation:worked_at_home 0.57
## commuters_by_public_transportation:aggregate_travel_time_to_work 0.60
## commuters_by_car_truck_van:commuters_by_subway_or_elevated 0.89
## commuters_by_car_truck_van:walked_to_work 0.11
## commuters_by_car_truck_van:commuters_by_carpool 0.76
## commuters_by_car_truck_van:worked_at_home 0.30
## commuters_by_car_truck_van:aggregate_travel_time_to_work 0.52
## commuters_by_subway_or_elevated:walked_to_work 0.50
## commuters_by_subway_or_elevated:commuters_by_carpool 0.07
## commuters_by_subway_or_elevated:worked_at_home 0.45
## commuters_by_subway_or_elevated:aggregate_travel_time_to_work 0.28
## walked_to_work:commuters_by_carpool 0.89
## walked_to_work:worked_at_home 0.57
## walked_to_work:aggregate_travel_time_to_work 0.31
## commuters_by_carpool:worked_at_home 0.47
## commuters_by_carpool:aggregate_travel_time_to_work 0.37
## worked_at_home:aggregate_travel_time_to_work 0.22
## -------------------------------------------------------------------------------
subset_2021$resid_3 <- residuals(model_3)
ggplot(subset_2021) +
geom_point(aes(x = gini_index, y = resid_3, color = median_income)) +
scale_color_continuous(type='viridis') +
labs(title = "Residual Plot") +
ylab("Residuals") +
xlab("Gini Index, Observed") +
geom_abline(slope = 0, intercept = 0, color = 'blue')
## Warning: Removed 488 rows containing missing values (`geom_point()`).
model_4 <- glm(gini_index ~ commuters_by_car_truck_van +
commuters_by_carpool +
aggregate_travel_time_to_work +
commuters_by_car_truck_van*walked_to_work +
commuters_by_public_transportation +
commuters_by_public_transportation*commuters_by_carpool +
commuters_by_public_transportation*walked_to_work,
data = subset_2021,
na.action = 'na.exclude')
summ(model_4)
## MODEL INFO:
## Observations: 146 (488 missing obs. deleted)
## Dependent Variable: gini_index
## Type: Linear regression
##
## MODEL FIT:
## χ²(8) = 0.09, p = 0.00
## Pseudo-R² (Cragg-Uhler) = -0.01
## Pseudo-R² (McFadden) = -0.12
## AIC = -543.75, BIC = -513.92
##
## Standard errors: MLE
## ------------------------------------------------------------------------------
## Est. S.E.
## ------------------------------------------------------------- -------- -------
## (Intercept) 0.56 0.05
## commuters_by_car_truck_van -0.04 0.14
## commuters_by_carpool -0.82 0.33
## aggregate_travel_time_to_work -0.01 0.00
## walked_to_work 1.31 1.61
## commuters_by_public_transportation 0.73 0.42
## commuters_by_car_truck_van:walked_to_work -2.33 4.74
## commuters_by_carpool:commuters_by_public_transportation 2.33 6.28
## walked_to_work:commuters_by_public_transportation -16.13 10.81
## ------------------------------------------------------------------------------
##
## -----------------------------------------------------------------------------
## t val. p
## ------------------------------------------------------------- -------- ------
## (Intercept) 12.11 0.00
## commuters_by_car_truck_van -0.32 0.75
## commuters_by_carpool -2.48 0.01
## aggregate_travel_time_to_work -2.55 0.01
## walked_to_work 0.81 0.42
## commuters_by_public_transportation 1.73 0.09
## commuters_by_car_truck_van:walked_to_work -0.49 0.62
## commuters_by_carpool:commuters_by_public_transportation 0.37 0.71
## walked_to_work:commuters_by_public_transportation -1.49 0.14
## -----------------------------------------------------------------------------
##
## Estimated dispersion parameter = 0
subset_2021$resid_4 = residuals(model_4)
predictions_4 <- predict(model_4)
ggplot(subset_2021) +
geom_point(aes(x = gini_index, y = resid_4, color = median_income)) +
scale_color_continuous(type='viridis') +
labs(title = "Residual Plot") +
ylab("Residuals") +
xlab("Gini Index, Observed") +
geom_abline(slope = 0, intercept = 0, color = 'blue')
## Warning: Removed 488 rows containing missing values (`geom_point()`).
Next I explore the log transformations of the interaction terms and whether these make linear relationships clearer.
ggplot(subset_2021) +
geom_point(aes(x=commuters_by_carpool, y = commuters_by_public_transportation, color = gini_index)) +
scale_colour_continuous(type = 'viridis', direction = 1)
## Warning: Removed 488 rows containing missing values (`geom_point()`).
subset_2021 %>% transform(carpool_public = log(commuters_by_carpool*commuters_by_public_transportation)) %>%
ggplot() +
geom_point(aes(x=carpool_public, y = gini_index, color = median_income)) +
scale_colour_continuous(type = 'viridis') +
geom_smooth(aes(x=carpool_public, y = gini_index)) +
labs(title = 'Interaction of Carpoolers and Public Transport Users') +
xlab('log(carpoolers*public_transport_users)') +
ylab('Gini Index')
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 488 rows containing non-finite values (`stat_smooth()`).
## Removed 488 rows containing missing values (`geom_point()`).
subset_2021 %>% transform(cartruck_walked = log(commuters_by_car_truck_van*walked_to_work)) %>%
ggplot() +
geom_point(aes(x=cartruck_walked, y = gini_index, color = median_income)) +
scale_colour_continuous(type = 'viridis') +
geom_smooth(aes(x=cartruck_walked, y = gini_index)) +
labs(title = 'Interaction of Car/Truck Commuters and Walkers') +
xlab('log(car_truck*walkers)') +
ylab('Gini Index')
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 437 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 437 rows containing missing values (`geom_point()`).
subset_2021 %>% transform(walked_public = log(walked_to_work*commuters_by_public_transportation)) %>%
ggplot() +
geom_point(aes(x=walked_public, y = gini_index, color = median_income)) +
scale_colour_continuous(type = 'viridis') +
geom_smooth(aes(x=walked_public, y = gini_index)) +
labs(title = 'Interaction of Walkers and Public Transport Users') +
xlab('log(walkers*public_transport_users)') +
ylab('Gini Index')
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 488 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 488 rows containing missing values (`geom_point()`).
Note that the log transformation makes the relationships above clearer. Without the transformation, the interactions are often not significant predictors.
model_5 <- glm(gini_index ~ commuters_by_car_truck_van +
commuters_by_carpool +
aggregate_travel_time_to_work +
commuters_by_public_transportation +
log(commuters_by_car_truck_van*walked_to_work) +
log(commuters_by_public_transportation*walked_to_work),
data = subset_2021,
na.action = 'na.exclude')
summ(model_5)
## MODEL INFO:
## Observations: 146 (488 missing obs. deleted)
## Dependent Variable: gini_index
## Type: Linear regression
##
## MODEL FIT:
## χ²(6) = 0.09, p = 0.00
## Pseudo-R² (Cragg-Uhler) = -0.01
## Pseudo-R² (McFadden) = -0.12
## AIC = -547.80, BIC = -523.93
##
## Standard errors: MLE
## -------------------------------------------------------------------------
## Est. S.E. t val. p
## ------------------------------------------ ------- ------ -------- ------
## (Intercept) 0.63 0.04 14.77 0.00
## commuters_by_car_truck_van -0.08 0.10 -0.80 0.42
## commuters_by_carpool -0.79 0.26 -3.01 0.00
## aggregate_travel_time_to_work -0.01 0.00 -2.37 0.02
## commuters_by_public_transportation 0.10 0.26 0.39 0.70
## log(commuters_by_car_truck_van -0.00 0.01 -0.19 0.85
## walked_to_work)
## log(commuters_by_public_transportation 0.01 0.01 1.17 0.24
## walked_to_work)
## -------------------------------------------------------------------------
##
## Estimated dispersion parameter = 0
predictions_5 <- predict(model_5)
# Residuals look much better here.
subset_2021$resid_5 <- residuals(model_5)
ggplot() +
geom_point(aes(x = predictions_5,
y = subset_2021$resid_5,
color = "Model 5")) +
geom_point(aes(x = predictions_4,
y = subset_2021$resid_4,
color = "Model 4")) +
labs(title = "Residual Plot") +
xlab("predictions") +
ylab("residuals") +
geom_abline(aes(slope = 0, intercept = 0, color = 'Residual = 0'), show.legend = FALSE) +
geom_vline(aes(xintercept = mean(predictions_5, na.rm = TRUE), color = 'Prediction Mean'), show.legend = FALSE) +
geom_vline(aes(xintercept = mean(subset_2021$gini_index), color = 'Gini Mean, Obs.'), show.legend = FALSE)
## Warning: Removed 488 rows containing missing values (`geom_point()`).
## Removed 488 rows containing missing values (`geom_point()`).
Going back to the original trend plots, I try the following relationships: * worked at home - sqrt * public - poly 2 * aggregate - poly 2
model_6 <- subset_2021 %>% drop_na(c('worked_at_home', 'gini_index', 'commuters_by_public_transportation', 'aggregate_travel_time_to_work')) %>%
glm(gini_index ~ 1 +
poly(I(sqrt(worked_at_home)), 2) + # this is basically saying square root transformation
poly(commuters_by_public_transportation, 2) +
aggregate_travel_time_to_work,
data = .,
na.action = 'na.exclude')
summ(model_6)
## MODEL INFO:
## Observations: 146
## Dependent Variable: gini_index
## Type: Linear regression
##
## MODEL FIT:
## χ²(5) = 0.09, p = 0.00
## Pseudo-R² (Cragg-Uhler) = -0.01
## Pseudo-R² (McFadden) = -0.11
## AIC = -545.85, BIC = -524.96
##
## Standard errors: MLE
## ---------------------------------------------------------------------------
## Est. S.E. t val. p
## -------------------------------------------- ------- ------ -------- ------
## (Intercept) 0.58 0.02 31.25 0.00
## poly(I(sqrt(worked_at_home)), -0.01 0.04 -0.21 0.84
## 2)1
## poly(I(sqrt(worked_at_home)), -0.13 0.04 -3.28 0.00
## 2)2
## poly(commuters_by_public_transportation, 0.24 0.04 5.89 0.00
## 2)1
## poly(commuters_by_public_transportation, -0.09 0.04 -2.44 0.02
## 2)2
## aggregate_travel_time_to_work -0.01 0.00 -5.75 0.00
## ---------------------------------------------------------------------------
##
## Estimated dispersion parameter = 0
resid_plot <- ggplot() +
geom_point(aes(x = predict(model_6), y = residuals(model_6))) +
geom_hline(aes(yintercept = 0), show.legend = FALSE) +
labs(title='Predictions against Residuals', subtitle = 'Model 7') +
xlab('Predictions') +
ylab("Residuals")
ggMarginal(resid_plot, type='histogram')
model_7 <-subset_2021 %>% drop_na(c('worked_at_home', 'gini_index', 'commuters_by_public_transportation', 'aggregate_travel_time_to_work')) %>% glm(gini_index ~ sqrt(worked_at_home) + commuters_by_public_transportation + I(commuters_by_public_transportation^2) + aggregate_travel_time_to_work, data = ., na.action = 'na.exclude' )
summ(model_7)
## MODEL INFO:
## Observations: 146
## Dependent Variable: gini_index
## Type: Linear regression
##
## MODEL FIT:
## χ²(4) = 0.07, p = 0.00
## Pseudo-R² (Cragg-Uhler) = -0.01
## Pseudo-R² (McFadden) = -0.09
## AIC = -537.01, BIC = -519.11
##
## Standard errors: MLE
## ----------------------------------------------------------------------------
## Est. S.E. t val. p
## --------------------------------------------- ------- ------ -------- ------
## (Intercept) 0.54 0.02 22.62 0.00
## sqrt(worked_at_home) 0.01 0.04 0.25 0.80
## commuters_by_public_transportation 1.19 0.32 3.68 0.00
## I(commuters_by_public_transportation^2) -4.24 2.50 -1.70 0.09
## aggregate_travel_time_to_work -0.01 0.00 -4.94 0.00
## ----------------------------------------------------------------------------
##
## Estimated dispersion parameter = 0
# Because we cannot deal with missing values the same way when using poly()
resid_plot <- ggplot() +
geom_point(aes(x = predict(model_7), y = residuals(model_7))) +
geom_hline(aes(yintercept = 0), show.legend = FALSE) +
labs(title='Predictions against Residuals', subtitle = 'Model 7') +
xlab('Predictions') +
ylab("Residuals")
ggMarginal(resid_plot, type='histogram')
Experiments with log transformations didn’t yield any better results. Because this is more for informative purposes than predictive, the most parsimonious and informative model among models 4, 5, and 6 will be chosen.
Using ANOVA is problematic because of the way missing values were omitted while building the models: they are not actually built from the same dataset. If all rows containing NAs had been dropped before building the models, a sample of only 146 places would have remained.
All three models are very close in the AIC criterion, so it really comes down to how informative the features are. Model 6 has low standard errors, informative features, and nicely dispersed residuals.
The following models experiment with link functions, which have the benefit of transforming data without affecting variance.
# Use a link function
model_8 <- glm(gini_index ~ commuters_by_car_truck_van +
commuters_by_carpool +
aggregate_travel_time_to_work +
commuters_by_car_truck_van:walked_to_work +
commuters_by_public_transportation:walked_to_work,
data = subset_2021, family = gaussian(link = "inverse"))
summ(model_8) # About same AIC as M5
## MODEL INFO:
## Observations: 146 (488 missing obs. deleted)
## Dependent Variable: gini_index
## Type: Generalized linear model
## Family: gaussian
## Link function: inverse
##
## MODEL FIT:
## χ²(5) = 0.08, p = 0.00
## Pseudo-R² (Cragg-Uhler) = -0.01
## Pseudo-R² (McFadden) = -0.11
## AIC = -543.70, BIC = -522.82
##
## Standard errors: MLE
## --------------------------------------------------------------------------------
## Est. S.E. t val.
## ------------------------------------------------------- ------- ------- --------
## (Intercept) 1.58 0.11 14.50
## commuters_by_car_truck_van 0.79 0.33 2.42
## commuters_by_carpool 3.26 1.20 2.72
## aggregate_travel_time_to_work 0.02 0.01 1.73
## commuters_by_car_truck_van:walked_to_work -6.18 5.08 -1.22
## walked_to_work:commuters_by_public_transportation -3.06 19.95 -0.15
## --------------------------------------------------------------------------------
##
## --------------------------------------------------------------
## p
## ------------------------------------------------------- ------
## (Intercept) 0.00
## commuters_by_car_truck_van 0.02
## commuters_by_carpool 0.01
## aggregate_travel_time_to_work 0.09
## commuters_by_car_truck_van:walked_to_work 0.23
## walked_to_work:commuters_by_public_transportation 0.88
## --------------------------------------------------------------
##
## Estimated dispersion parameter = 0
model_9 <- glm(gini_index ~ commuters_by_car_truck_van +
commuters_by_carpool +
aggregate_travel_time_to_work +
commuters_by_car_truck_van:walked_to_work +
commuters_by_public_transportation:walked_to_work,
data = subset_2021, family = gaussian(link = "log"))
summ(model_9) # Same AIC
## MODEL INFO:
## Observations: 146 (488 missing obs. deleted)
## Dependent Variable: gini_index
## Type: Generalized linear model
## Family: gaussian
## Link function: log
##
## MODEL FIT:
## χ²(5) = 0.08, p = 0.00
## Pseudo-R² (Cragg-Uhler) = -0.01
## Pseudo-R² (McFadden) = -0.11
## AIC = -544.70, BIC = -523.81
##
## Standard errors: MLE
## -------------------------------------------------------------------------------
## Est. S.E. t val.
## ------------------------------------------------------- ------- ------ --------
## (Intercept) -0.48 0.05 -8.88
## commuters_by_car_truck_van -0.41 0.16 -2.58
## commuters_by_carpool -1.55 0.56 -2.76
## aggregate_travel_time_to_work -0.01 0.00 -1.83
## commuters_by_car_truck_van:walked_to_work 2.95 2.47 1.19
## walked_to_work:commuters_by_public_transportation 1.08 9.81 0.11
## -------------------------------------------------------------------------------
##
## --------------------------------------------------------------
## p
## ------------------------------------------------------- ------
## (Intercept) 0.00
## commuters_by_car_truck_van 0.01
## commuters_by_carpool 0.01
## aggregate_travel_time_to_work 0.07
## commuters_by_car_truck_van:walked_to_work 0.24
## walked_to_work:commuters_by_public_transportation 0.91
## --------------------------------------------------------------
##
## Estimated dispersion parameter = 0
# Use log transform instead of link function
model_10 <- glm(log(gini_index) ~ log(commuters_by_car_truck_van) +
log(commuters_by_carpool) +
log(aggregate_travel_time_to_work) +
commuters_by_car_truck_van*walked_to_work +
commuters_by_public_transportation*walked_to_work,
data = subset_2021)
summary(model_10) # Worse AIC
##
## Call:
## glm(formula = log(gini_index) ~ log(commuters_by_car_truck_van) +
## log(commuters_by_carpool) + log(aggregate_travel_time_to_work) +
## commuters_by_car_truck_van * walked_to_work + commuters_by_public_transportation *
## walked_to_work, data = subset_2021)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -0.211742 -0.045914 -0.006988 0.028557 0.234794
##
## Coefficients:
## Estimate Std. Error t value
## (Intercept) 0.27801 0.47695 0.583
## log(commuters_by_car_truck_van) 0.50823 0.23181 2.192
## log(commuters_by_carpool) -0.07996 0.02507 -3.189
## log(aggregate_travel_time_to_work) -0.13846 0.04523 -3.061
## commuters_by_car_truck_van -1.35399 0.65146 -2.078
## walked_to_work 5.83753 3.67751 1.587
## commuters_by_public_transportation 2.40836 0.88320 2.727
## commuters_by_car_truck_van:walked_to_work -15.43564 11.15052 -1.384
## walked_to_work:commuters_by_public_transportation -34.86443 21.18392 -1.646
## Pr(>|t|)
## (Intercept) 0.56093
## log(commuters_by_car_truck_van) 0.03004 *
## log(commuters_by_carpool) 0.00177 **
## log(aggregate_travel_time_to_work) 0.00265 **
## commuters_by_car_truck_van 0.03954 *
## walked_to_work 0.11474
## commuters_by_public_transportation 0.00723 **
## commuters_by_car_truck_van:walked_to_work 0.16852
## walked_to_work:commuters_by_public_transportation 0.10210
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for gaussian family taken to be 0.005538832)
##
## Null deviance: 1.16865 on 145 degrees of freedom
## Residual deviance: 0.75882 on 137 degrees of freedom
## (488 observations deleted due to missingness)
## AIC: -333.57
##
## Number of Fisher Scoring iterations: 2
Looking at QQ plots and running \(\chi^2\) tests on each model against the null model (intercept only), the fears from above are still confirmed: models do not predict well at the tails, and the models accounted for little of the variance in the data.
plot(model_1, which = 1)
plot(model_1, which =2)
plot(model_2, which = 1)
plot(model_2, which =2) # no
plot(model_3, which = 1) # no
plot(model_3, which =2)
plot(model_4, which = 1) # no
plot(model_4, which =2)
plot(model_5, which =1)
plot(model_5, which =2) # no
# this is the best one
plot(model_6, which =1)
plot(model_6, which =2) # good
plot(model_7, which =1)
plot(model_7, which =2)
plot(model_8, which =1)
plot(model_8, which =2) #no
plot(model_9, which =1)
plot(model_9, which =2) #no
plot(model_10, which =1)
plot(model_10, which =2) #no
It looks like model 6 has the most normal residuals, but all models are unstable in the upper quantile.
Due in part to different missing values when comparing models, the significance of predictors isn’t reliable from model to model, making comparison difficult.
Now I want to see areas in the U.S. for which the model works, and areas where it doesn’t work. Taking the absolute value of the actual Gini Index minus the predicted value shows how far the prediction is off: higher values here correspond to lower accuracy.
Indeed, this is cherrypicking. But if I can find what type of areas the model works in and doesn’t, it might inform my model further or suggest augmenting my data with non-transportation variables.
m6 <- model_6$data
m6$resid_6 <- model_6$residuals
m6 <-transform(m6, abs_resid = abs(resid_6))
US <- shift_geometry(states(cb = TRUE, progress_bar=FALSE))
## Retrieving data for the year 2021
US_centroids <-st_centroid(shift_geometry(places(cb=TRUE, progress_bar = FALSE)))
## Retrieving data for the year 2021
## Retrieving Census-designated places for the entire United States
## Warning: st_centroid assumes attributes are constant over geometries
US_joined <- US_centroids %>% right_join(m6, by = join_by(GEOID == geo_id), multiple = 'all')
US_plot <- ggplot(axes =FALSE) +
geom_sf(data = US, color = 'lightgrey') +
geom_sf(aes(color = abs_resid,
size = gini_index), data = US_joined) +
scale_color_continuous(type = 'viridis') +
#theme_void() +
labs(title = 'Where the Model Works',
subtitle = 'Places with Population > 20k') +
coord_sf(crs = 'ESRI:102003') +
theme_void()
ggplotly(US_plot)