library(tidycensus)
## Warning: package 'tidycensus' was built under R version 4.2.3
#Q1
census_api_key("edd28860119f5a254a2915190d528839b25a475e",install="TRUE", overwrite = TRUE)
## Your original .Renviron will be backed up and stored in your R HOME directory if needed.
## Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY").
## To use now, restart R or run `readRenviron("~/.Renviron")`
## [1] "edd28860119f5a254a2915190d528839b25a475e"
var <- c('B19013_001E','B03002_001E','B03002_004E','B01001_002E','B01001_026E','B01003_001E','B01002_001E')
Travis_County <- get_acs(geography = "tract",
variables = var,
county = "Travis",
state = "TX",
output="wide",
geometry = TRUE)
## Getting data from the 2017-2021 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
##
|
| | 0%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|== | 4%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 9%
|
|======= | 10%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============= | 18%
|
|============= | 19%
|
|============== | 19%
|
|============== | 20%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 22%
|
|================ | 23%
|
|================= | 24%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 40%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 50%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 69%
|
|================================================= | 70%
|
|================================================= | 71%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 80%
|
|======================================================== | 81%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 83%
|
|========================================================== | 84%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 86%
|
|============================================================= | 86%
|
|============================================================= | 87%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 89%
|
|=============================================================== | 90%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 99%
|
|======================================================================| 100%
#Q2
names(Travis_County)[3] <-"HHIncome"
names(Travis_County)[5] <-"Hispanic"
names(Travis_County)[7] <-"Black"
names(Travis_County)[9] <-"Male"
names(Travis_County)[11] <-"Female"
names(Travis_County)[13] <-"Totalpop"
names(Travis_County)[15] <- "MDage"
Travis_County$B19013_001M <- NULL
Travis_County$B03002_001M <- NULL
Travis_County$B03002_004M <- NULL
Travis_County$B01001_002M <- NULL
Travis_County$B01001_026M <- NULL
Travis_County$B01003_001M <- NULL
Travis_County$B01002_001M <- NULL
#Q3
write.csv(Travis_County, "C:/Users/salom/OneDrive/Desktop/Travis_County.csv")
#Q4
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
ggplot(data = Travis_County, aes(x = Black, y = HHIncome)) +
geom_point(size = 3 , shape=16)
## Warning: Removed 4 rows containing missing values (`geom_point()`).
#Q5
df <- ggplot(Travis_County, aes(x = MDage))
qplot(MDage, data = Travis_County, geom = "histogram",
fill = MDage)
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing non-finite values (`stat_bin()`).
## Warning: The following aesthetics were dropped during statistical transformation: fill
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
#Q6 PDF
qplot(HHIncome, data = Travis_County, geom = "density",
fill = HHIncome)
## Warning: Removed 4 rows containing non-finite values (`stat_density()`).
## The following aesthetics were dropped during statistical transformation: fill
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
#Q7 CDF
df2 <- ggplot(Travis_County, aes(x = HHIncome))
df2 + stat_ecdf()
## Warning: Removed 4 rows containing non-finite values (`stat_ecdf()`).
#Q8 Box plot
df2 + geom_boxplot()
## Warning: Removed 4 rows containing non-finite values (`stat_boxplot()`).
#Q9
library(sf)
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(tmap)
## Warning: package 'tmap' was built under R version 4.2.3
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
tm_shape(Travis_County) +tm_fill(col = "Hispanic")+ tm_layout(title = "Hispanic Pop")
#Q10
Travis_County$pct_male <- 100*Travis_County$Male/Travis_County$Totalpop
Travis_County$pct_female <- 100*Travis_County$Female/Travis_County$Totalpop
tm_shape(Travis_County) +tm_fill(col = "pct_male")+ tm_layout(title = "Percent of Male")
tm_shape(Travis_County) +tm_fill(col = "pct_female")+ tm_layout(title = "Percent of Female")
#Q11
x <- c(2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022,2023)
y <- c(1.030,1.062,1.097,1.122,1.152,1.180,1.206,1.227,1.247,1.274,1.301,1.305,1.326,1.334)
poly.lm1 <- lm(y ~ poly(x, 1))
poly.lm2 <- lm(y ~ poly(x, 2))
poly.lm3 <- lm(y ~ poly(x, 3))
new.x <- c(2024, 2025, 2026, 2027, 2028)
new.df <- data.frame(x=new.x)
new.y1 <- predict(poly.lm1, newdata=new.df)
new.y2 <- predict(poly.lm2, newdata=new.df)
new.y3 <- predict(poly.lm3, newdata=new.df)