# Q1
library(tidycensus)

#https://api.census.gov/data/2020/acs/acs5/variables.html
#ctrl+F to look for variables
#median household income= B19013_001E
#Hispanic population= B03002_001E
#Non-Hispanic African American population= B03002_004E
#male=B01001_002E
#female= B01001_026E
#total population=B01003_001E
#median age= B01002_001E    Estimate!!Median age --!!Total: MEDIAN AGE BY SEX

var=c('B19013_001E','B03002_001E','B03002_004E','B01001_002E','B01001_026E','B01003_001E','B01002_001E') 

Travis_segregation <- 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%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
names(Travis_segregation)[3] <- 'MHHincome'
names(Travis_segregation)[5] <- 'Hispanic'
names(Travis_segregation)[7] <- 'Black or African American'
names(Travis_segregation)[9] <- 'Male'
names(Travis_segregation)[11] <- 'Female'
names(Travis_segregation)[13] <- 'TotalPop'
names(Travis_segregation)[15] <- 'MAge'

#Q2
Travis_segregation$B19013_001M<- NULL
Travis_segregation$B03002_001M<- NULL
Travis_segregation$B03002_004M<- NULL
Travis_segregation$B01001_002M<- NULL
Travis_segregation$B01001_026M<- NULL
Travis_segregation$B01003_001M<- NULL
Travis_segregation$B01002_001M<- NULL

#Q3
write.csv(Travis_segregation,"C:/Users/haomi/Desktop/Urban Planning Methods I URP-5363-901-Fall 2023/Oct 17 - Week 9/Travis_seg.csv")

#Q4
Travis_segregation$pct_Black <- 100*Travis_segregation$Black/Travis_segregation$TotalPop
library(ggplot2)
ggplot(data = Travis_segregation, aes(x = pct_Black, y = MHHincome)) + geom_point()
## Warning: Removed 4 rows containing missing values (`geom_point()`).

#Q5 Make a histogram to visualize the age distribution of the county
AgeDistrbution <- ggplot(Travis_segregation, aes(x = MAge))
AgeDistrbution + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 1 rows containing non-finite values (`stat_bin()`).

#Q6 Make a PDF (probability density function) chart to show the distribution of median household income
MHHincome <- ggplot(Travis_segregation, aes(x = MHHincome))
MHHincome + geom_density()
## Warning: Removed 4 rows containing non-finite values (`stat_density()`).

#Q7 Make a  CDF (cumulative density function) chart to show the distribution of median household income
MHHincome + stat_ecdf()
## Warning: Removed 4 rows containing non-finite values (`stat_ecdf()`).

#Q8 Make a boxplot to visualize the median household income
MHHincome + geom_boxplot()
## Warning: Removed 4 rows containing non-finite values (`stat_boxplot()`).

#Q9 Make a map to show the spatial distribution of percentage of Hispanic population
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
library(tmap)
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
##      (status 2 uses the sf package in place of rgdal)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
Travis_segregation$pct_Hispanic <- 100*Travis_segregation$Hispanic/Travis_segregation$TotalPop
tm_shape(Travis_segregation) +tm_fill(col = "pct_Hispanic")+ tm_layout(title = "Hispanic Percent")

#Q10 Calculate and map the difference between female and male population to show what census tract has more female population

Travis_segregation$Difference <- Travis_segregation$Female - Travis_segregation$Male
tm_shape(Travis_segregation) +tm_fill(col = "Difference")+ tm_layout(title = "Difference between female and male")
## Variable(s) "Difference" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.

#Q11 Find the population of the county (or the major city within the county) from 2010 to 2023, and predict the population for the next five years (2024-2028) 
x <- c(2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022,2023) #year
y <- c(1.03,1.06,1.09,1.12,1.15,1.18,1.21,1.23,1.25,1.27,1.30,1.31,1.33,1.34) #(million persons) find in https://fred.stlouisfed.org/series/TXTRAV3POP
poly.lm1 <- lm(y ~ poly(x, 1))
new.x <- c(2024, 2025, 2026, 2027,2028)
new.df <- data.frame(x=new.x)
new.y <- predict(poly.lm1, newdata=new.df)
print(new.y)
##        1        2        3        4        5 
## 1.387473 1.411802 1.436132 1.460462 1.484791
#Q12 Publish your project to Rpubs using Rmarkdown and submit the link to Canvas