#Q1
library(tidycensus)
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(mapview)
## Warning: package 'mapview' was built under R version 4.3.1
## 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)
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
#census_api_key("0d539976d5203a96fa55bbf4421110d4b3db3648",install="TRUE")
travis_medincome  <- get_acs(geography = "tract", variables = "B19013_001E",
                            state = "TX", 
                            county = "Travis", 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%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |======================================================================| 100%
texas_hispanic  <- get_acs(geography = "tract", variables = "B03002_012E",
                             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)`.
texas_black  <- get_acs(geography = "tract", variables = "B01001B_001E",
                           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)`.
travis_male  <- get_acs(geography = "tract", variables = "B01001_002E",
                             state = "TX", 
                             county = "Travis", 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)`.
travis_female  <- get_acs(geography = "tract", variables = "B01001_026E",
                             state = "TX", 
                             county = "Travis", 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)`.
travis_totalpop  <- get_acs(geography = "tract", variables = "B01001_001E",
                          state = "TX", 
                          county = "Travis", 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)`.
travis_medianage  <- get_acs(geography = "tract", variables = "B01002_001E",
                            state = "TX", 
                            county = "Travis", 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)`.
var=c('B19013_001E','B03002_012E','B01001B_001E','B01001_002E','B01001_026E','B01001_001E','B01002_001E') 
Travisdata <- 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)`.
#Q2
Travisdata$B19013_001M <- NULL
Travisdata$B01001_001M <- NULL
Travisdata$B01001B_001M <- NULL
Travisdata$B01001_002M <- NULL
Travisdata$B01001_026M <- NULL
Travisdata$B01002_001M <- NULL
Travisdata$B03002_012M <- NULL

names(Travisdata)[3] <- "median income"
names(Travisdata)[4] <- "hispanic pop"
names(Travisdata)[5] <- "black pop"
names(Travisdata)[6] <- "male pop"
names(Travisdata)[7] <- "female pop"
names(Travisdata)[8] <- "total pop"
names(Travisdata)[9] <- "median age"


#Q3
write.csv(Travisdata, "/Users/zmagz/Desktop/Desktop - Zack’s MacBook Pro (2)/School:Intership 2023:2024/URP CLASSES/travisdata.csv")

#Q4
Travisdata <- na.omit(Travisdata)
Travisdata$PCTBLK <-100*Travisdata$`black pop`/Travisdata$`total pop`
ggplot(data = Travisdata, aes(x = "median income", y="pctblack")) +
  geom_point()

#Q5
ggplot(data = Travisdata, aes(x = Travisdata$`median age`)) +
  geom_histogram(binwidth = 1, fill = "blue", color = "black") +
  labs(title = "Age Distribution in Travis County",
       x = "Median Age",
       y = "Frequency")

#Q6
ggplot(data = Travisdata, aes(x = Travisdata$`median income`)) +
  geom_density(fill = "blue", color = "black") +
  labs(title = "Probability Density Function of Median Household Income",
       x = "Median Income",
       y = "Density")

#Q7
ggplot(data = Travisdata, aes(x = Travisdata$`median income`)) +
  stat_ecdf(geom = "step", pad = TRUE) +
  labs(title = "Cumulative Density Function of Median Household Income",
       x = "Median Income",
       y = "Cumulative Probability")

#Q8
ggplot(data = Travisdata, aes(x = Travisdata$`median income`)) +
  geom_boxplot(fill = "blue", color = "black") +
  labs(title = "Boxplot of Median Household Income",
       y = "Median Income")

#Q9
Travisdata$PCTHISP <- 100 * Travisdata$`hispanic pop` / Travisdata$`total pop`
Travisdata <- st_as_sf(Travisdata)
tm_shape(Travisdata) +
  tm_borders() +
  tm_fill(col = "PCTHISP", title = "Percentage of Hispanic Population") +
  tm_layout(title = "Spatial Distribution of Percentage of Hispanic Population")

#Q10
Travisdata$Female_Male_Difference <- Travisdata$`female pop` - Travisdata$`male pop`
Travisdata <- st_as_sf(Travisdata)
tm_shape(Travisdata) +
  tm_borders() +
  tm_fill(col = "Female_Male_Difference", title = "Female - Male Population Difference") +
  tm_layout(title = "Population Difference: More Females than Males")
## Variable(s) "Female_Male_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
x <- c(2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020,2021,2022,2023)
y <- c(774769, 757433, 774820, 790646, 806447, 819217, 828344, 839539, 849389,865688, 881987, 898286, 914585, 930855)

poly.lm1 <- lm(y ~ poly(x, 1))
new.x <- c(2023, 2024, 2025, 2026, 2027, 2028)
new.df <- data.frame(x=new.x)


new.y1 <- predict(poly.lm1, newdata=new.df)
print(new.y1)
##        1        2        3        4        5        6 
## 922375.9 935356.7 948337.6 961318.4 974299.3 987280.1