library(tidyverse)
library(sf)
library(tmap)
library(tigris)
library(spdep)
library(data.world)
library(readr)
library(dplyr)
library(texreg)
library(ggplot2)
library(tmap)
library(tmaptools)
library(tidyr)
library(Zelig)
library(visreg)
Amer.nation <- read_csv("/Users/Deepakie/Documents/Queens College/SOC712/Data/American Nations.csv")
head(Amer.nation)
religiondata$Geo_FIPS <- as.integer(religiondata$Geo_FIPS)
ct_map$GEOID <- as.integer(ct_map$GEOID)
Non-spatial data gives a statistical correlation that is seperate of all spatial examinations. We will use a Ecological regressions and ggplots in this section.The ecological regression produced for this analysis is the relationship between total population and % population religious. The ecological regression, shown below give the mean of all religious population and total population of each state.
religiondata_er<- religiondata %>%
group_by(StateFP) %>%
summarise(mean_p = mean(Religiousperc, na.rm = TRUE), mean_s = mean(Totalpop, na.rm = TRUE))
eco <- lm(mean_p ~ mean_s, data = religiondata_er)
summary(eco)
Call:
lm(formula = mean_p ~ mean_s, data = religiondata_er)
Residuals:
Min 1Q Median 3Q Max
-24.4110 -7.6785 0.6961 7.7478 27.2981
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.073e+01 2.199e+00 23.074 <2e-16 ***
mean_s -1.470e-05 9.640e-06 -1.525 0.133
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 11.65 on 50 degrees of freedom
Multiple R-squared: 0.04446, Adjusted R-squared: 0.02535
F-statistic: 2.327 on 1 and 50 DF, p-value: 0.1335
ggplot(data = religiondata, aes(x = StateFP, y = Religiousperc)) +
geom_point() +
coord_capped_cart(bottom='both', left='none') +
theme_light() + theme(panel.border=element_blank(), axis.line = element_line())
The plot shows the percent of total religious population per each states. The plot is hard to read and confusing to understand so we will now look at this analysis by a Spatial mapping.
religiondata <- religiondata %>%
mutate(fips = as.integer(Geo_FIPS))
ct_map <- ct_map %>%
mutate(fips = parse_integer(GEOID))
combined_data <- ct_map %>%
left_join(religiondata, by = "fips")
library(tmap)
tm_shape(combined_data) + tm_polygons("Religiousperc")
combined_data_sub <- combined_data %>%
filter(STATEFP != "02") %>%
filter(STATEFP != "15") %>%
filter(STATEFP != "60") %>%
filter(STATEFP != "66") %>%
filter(STATEFP != "69") %>%
filter(STATEFP != "72") %>%
filter(STATEFP != "78")
tm_shape(combined_data_sub, projection = 2163) + tm_polygons("Religiousperc")
combined_data_sub <- combined_data_sub %>%
mutate(redblue = Religiousperc - 51.41)
The mean of “religiousperc” in this dataset help us differentiate the states percentage of religious population to observe differences between states in a clearer way. The mean also helps avoide extrapolation, in this case the mean is 51.41.
tm_shape(combined_data_sub, projection = 2163) + tm_polygons("redblue", palette = "-RdBu")
us_states <- combined_data_sub %>%
aggregate_map(by = "STATEFP")
tm_shape(combined_data_sub, projection = 2163) + tm_polygons("redblue", palette = "-RdBu") +
tm_shape(us_states) + tm_borders(lwd = .36, col = "black", alpha = 1)
tm_shape(combined_data_sub, projection = 2163) + tm_polygons("redblue", palette = "-RdBu", border.col = "grey", border.alpha = .4) +
tm_shape(us_states) + tm_borders(lwd = .36, col = "black", alpha = 1)
This gives a spatial visualization by highlighting the state lines to understand the total percent regligious in the country among different states and counties. From the map above, the most religious areas are in scattered across the nation but we can identify large religious total percentages in the midnorth(North Dakota,South Dakota,Minnesota), midsouth(Texas, Louisiana,Oklahoma) and a little bit in midwest(mostly being in Utah). The map identifies by total religious percentage with ranges from -50 to 0, indiciating no relgiious populations, 0- 50 indicating religious population being between 0 -50%, 50 to 100% giving us higher total religious percentages and so on.
The American Nation: A History of the United States, written by Mark Carnes and John Garraty. The authors represent the political history of the United States tied with its social, economic and cultural development. Using the mapping file of the American Nations, this spatial analysis, shows county level percents of religious population among the 11 nations within America.
Amer.nation <- combined_data_sub %>%
aggregate_map(by = "STATEFP")
tm_shape(combined_data_sub, projection = 2163) + tm_polygons("redblue", palette = "-RdBu") +
tm_shape(Amer.nation) + tm_borders(lwd = .36, col = "black", alpha = 1)
tm_shape(combined_data_sub, projection = 2163) + tm_polygons("redblue", palette = "-RdBu", border.col = "grey", border.alpha = .4) +
tm_shape(Amer.nation) + tm_borders(lwd = .50, col = "black", alpha = 1)
The Tigris package makes creating spatial mapping easier and smoother to apply for the analysis as many functions have a parameter,CB. On slide 32 of Professor’s slides, we coded counties(CB = TRUE).
If CF is set to TRUE, it will direct tigris to load a cartographic boundry file. Cartographic boundry files default to a simplified resoultion of 1:500k. In some cases such as states, resolutions of 1.5 million and 1:20 million are available. For example, R specify the following modifications to the states() function and retrieves a simplified dataset. In other words when CB is set to FALSE, we get a detailed TIGER/line file. When it is set to TRUE, the file is generalized to (1:500K).
options(tigris_class = "sf")
t_county <- counties(cb = TRUE)
#names(t_county)
t_combined_data <- t_county %>%
mutate(fips_code = parse_integer(GEOID))
t_combined_data_sub <- t_combined_data %>%
filter(STATEFP != "02") %>%
filter(STATEFP != "15") %>%
filter(STATEFP != "60") %>%
filter(STATEFP != "66") %>%
filter(STATEFP != "69") %>%
filter(STATEFP != "72") %>%
filter(STATEFP != "78")
us_states <- t_combined_data_sub %>%
aggregate_map(by = "STATEFP")
Amer.nation <- t_combined_data_sub %>%
aggregate_map(by = "STATEFP")
t_combined_data_sub <- combined_data_sub %>%
mutate(redblue = Religiousperc - mean(religiondata$Religiousperc))
tm_shape(combined_data_sub, projection = 2163) + tm_polygons("redblue", palette = "-RdBu", border.col = "grey", border.alpha = .4) +
tm_shape(Amer.nation) + tm_borders(lwd = .50, col = "black", alpha = 1)
options(tigris_class = "sf")
t_county <- counties(cb = FALSE)
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|=== | 3%
|
|=== | 4%
|
|==== | 4%
|
|==== | 5%
|
|===== | 5%
|
|===== | 6%
|
|====== | 6%
|
|====== | 7%
|
|======= | 7%
|
|======= | 8%
|
|======== | 8%
|
|======== | 9%
|
|========= | 9%
|
|========= | 10%
|
|========= | 11%
|
|========== | 11%
|
|========== | 12%
|
|=========== | 12%
|
|=========== | 13%
|
|============ | 13%
|
|============ | 14%
|
|============= | 14%
|
|============= | 15%
|
|============== | 15%
|
|============== | 16%
|
|=============== | 16%
|
|=============== | 17%
|
|================ | 17%
|
|================ | 18%
|
|================= | 18%
|
|================= | 19%
|
|================== | 19%
|
|================== | 20%
|
|================== | 21%
|
|=================== | 21%
|
|=================== | 22%
|
|==================== | 22%
|
|==================== | 23%
|
|===================== | 23%
|
|===================== | 24%
|
|====================== | 24%
|
|====================== | 25%
|
|======================= | 25%
|
|======================= | 26%
|
|======================== | 26%
|
|======================== | 27%
|
|========================= | 27%
|
|========================= | 28%
|
|========================== | 28%
|
|========================== | 29%
|
|=========================== | 29%
|
|=========================== | 30%
|
|=========================== | 31%
|
|============================ | 31%
|
|============================ | 32%
|
|============================= | 32%
|
|============================= | 33%
|
|============================== | 33%
|
|============================== | 34%
|
|=============================== | 34%
|
|=============================== | 35%
|
|================================ | 35%
|
|================================ | 36%
|
|================================= | 36%
|
|================================= | 37%
|
|================================== | 37%
|
|================================== | 38%
|
|=================================== | 38%
|
|=================================== | 39%
|
|==================================== | 39%
|
|==================================== | 40%
|
|==================================== | 41%
|
|===================================== | 41%
|
|===================================== | 42%
|
|====================================== | 42%
|
|====================================== | 43%
|
|======================================= | 43%
|
|======================================= | 44%
|
|======================================== | 44%
|
|======================================== | 45%
|
|========================================= | 45%
|
|========================================= | 46%
|
|========================================== | 46%
|
|========================================== | 47%
|
|=========================================== | 47%
|
|=========================================== | 48%
|
|============================================ | 48%
|
|============================================ | 49%
|
|============================================= | 49%
|
|============================================= | 50%
|
|============================================= | 51%
|
|============================================== | 51%
|
|============================================== | 52%
|
|=============================================== | 52%
|
|=============================================== | 53%
|
|================================================ | 53%
|
|================================================ | 54%
|
|================================================= | 54%
|
|================================================= | 55%
|
|================================================== | 55%
|
|================================================== | 56%
|
|=================================================== | 56%
|
|=================================================== | 57%
|
|==================================================== | 57%
|
|==================================================== | 58%
|
|===================================================== | 58%
|
|===================================================== | 59%
|
|====================================================== | 59%
|
|====================================================== | 60%
|
|====================================================== | 61%
|
|======================================================= | 61%
|
|======================================================= | 62%
|
|======================================================== | 62%
|
|======================================================== | 63%
|
|========================================================= | 63%
|
|========================================================= | 64%
|
|========================================================== | 64%
|
|========================================================== | 65%
|
|=========================================================== | 65%
|
|=========================================================== | 66%
|
|============================================================ | 66%
|
|============================================================ | 67%
|
|============================================================= | 67%
|
|============================================================= | 68%
|
|============================================================== | 68%
|
|============================================================== | 69%
|
|=============================================================== | 69%
|
|=============================================================== | 70%
|
|=============================================================== | 71%
|
|================================================================ | 71%
|
|================================================================ | 72%
|
|================================================================= | 72%
|
|================================================================= | 73%
|
|================================================================== | 73%
|
|================================================================== | 74%
|
|=================================================================== | 74%
|
|=================================================================== | 75%
|
|==================================================================== | 75%
|
|==================================================================== | 76%
|
|===================================================================== | 76%
|
|===================================================================== | 77%
|
|====================================================================== | 77%
|
|====================================================================== | 78%
|
|======================================================================= | 78%
|
|======================================================================= | 79%
|
|======================================================================== | 79%
|
|======================================================================== | 80%
|
|======================================================================== | 81%
|
|========================================================================= | 81%
|
|========================================================================= | 82%
|
|========================================================================== | 82%
|
|========================================================================== | 83%
|
|=========================================================================== | 83%
|
|=========================================================================== | 84%
|
|============================================================================ | 84%
|
|============================================================================ | 85%
|
|============================================================================= | 85%
|
|============================================================================= | 86%
|
|============================================================================== | 86%
|
|============================================================================== | 87%
|
|=============================================================================== | 87%
|
|=============================================================================== | 88%
|
|================================================================================ | 88%
|
|================================================================================ | 89%
|
|================================================================================= | 89%
|
|================================================================================= | 90%
|
|================================================================================= | 91%
|
|================================================================================== | 91%
|
|================================================================================== | 92%
|
|=================================================================================== | 92%
|
|=================================================================================== | 93%
|
|==================================================================================== | 93%
|
|==================================================================================== | 94%
|
|===================================================================================== | 94%
|
|===================================================================================== | 95%
|
|====================================================================================== | 95%
|
|====================================================================================== | 96%
|
|======================================================================================= | 96%
|
|======================================================================================= | 97%
|
|======================================================================================== | 97%
|
|======================================================================================== | 98%
|
|========================================================================================= | 98%
|
|========================================================================================= | 99%
|
|==========================================================================================| 99%
|
|==========================================================================================| 100%
#names(t_county)
t_combined_data <- t_county %>%
mutate(fips_code = parse_integer(GEOID))
t_combined_data_sub <- t_combined_data %>%
filter(STATEFP != "02") %>%
filter(STATEFP != "15") %>%
filter(STATEFP != "60") %>%
filter(STATEFP != "66") %>%
filter(STATEFP != "69") %>%
filter(STATEFP != "72") %>%
filter(STATEFP != "78")
religiondata1 <- religiondata %>%
mutate(fips= Geo_FIPS)
us_states <- t_combined_data_sub %>%
aggregate_map(by = "STATEFP")
Amer.nation <- t_combined_data_sub %>%
aggregate_map(by = "STATEFP")
t_combined_data_sub <- combined_data_sub %>%
mutate(redblue = Religiousperc - mean(religiondata$Religiousperc))
tm_shape(combined_data_sub, projection = 2163) + tm_polygons("redblue", palette = "-RdBu", border.col = "grey", border.alpha = .4) +
tm_shape(Amer.nation) + tm_borders(lwd = .50, col = "black", alpha = 1)
Spatial data evulates the percent religious as a whole and its relationship between states but not in a precise and particular way as non-spatial. Non-spatial data is usually independent rather than multidemensional and auro correlated as spatial. Locational data as used for this analysis is assessed through spatial analysis rather than coverting location functions which have ecological fallacies due to other influenceing variables. Overall, by evaluating both spatial and non-spatial analysis for this assignment have advantages and disadvantages. Spatial data is simple but does not give a indepth analysis. When we combine both data analysis, we are able to evaluate total religious population percentage easily by location.