library(tidycensus)

#Q1
var=c('B19013_001E','B03002_012E','B03002_004E','B01001_002E', 'B01001_026E', 'B01003_001E', 'B01002_001E') 
Maricopa_data <- get_acs(geography = "tract", variables = var, county = "Maricopa",
                             state = "AZ",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%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |======================================================================| 100%
#Q2
Maricopa_data$B19013_001M <- NULL
Maricopa_data$B03002_012M <- NULL
Maricopa_data$B03002_004M <- NULL
Maricopa_data$B01001_002M <- NULL
Maricopa_data$B01001_026M <- NULL
Maricopa_data$B01003_001M <- NULL
Maricopa_data$B01002_001M <- NULL

names(Maricopa_data)[3] <- 'HHIncome'
names(Maricopa_data)[4] <- 'Hispanic'
names(Maricopa_data)[5] <- 'Black'
names(Maricopa_data)[6] <- 'Male'
names(Maricopa_data)[7] <- 'Female'
names(Maricopa_data)[8] <- 'TotalPop'
names(Maricopa_data)[9] <- 'Age'

#Q3
write.csv(Maricopa_data, "/Users/nicomolina/Desktop/UTSA URP Work/Maricopa_data.csv")

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

#Q5
a <- ggplot(Maricopa_data, aes(x=Age))
a + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 13 rows containing non-finite values (`stat_bin()`).

#Q6
i <- ggplot(Maricopa_data, aes(x=HHIncome))
i + geom_density()
## Warning: Removed 23 rows containing non-finite values (`stat_density()`).

#Q7
i + stat_ecdf()
## Warning: Removed 23 rows containing non-finite values (`stat_ecdf()`).

#Q8 
i + geom_boxplot()
## Warning: Removed 23 rows containing non-finite values (`stat_boxplot()`).

#Q9
library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.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')
Maricopa_data$PercentHispanic <- 100*Maricopa_data$Hispanic/Maricopa_data$TotalPop
tm_shape(Maricopa_data) +tm_fill(col = "PercentHispanic")+ tm_layout(title = "Hispanic Population")

#Q10
Maricopa_data$MFDifference <- Maricopa_data$Female - Maricopa_data$Male 
tm_shape(Maricopa_data) +tm_fill(col = "MFDifference") + tm_layout(title = "Male and Female")
## Variable(s) "MFDifference" 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(3.81, 3.86, 3.95, 4.01, 4.09, 4.17, 4.26, 4.32, 4.4, 4.41, 4.42, 4.49, 4.55, 4.6)

plot(x, y, pch=16, xlab='x', ylab='y')

poly.lm1 <- lm(y ~ poly(x, 1))
poly.lm2 <- lm(y ~ poly(x, 2))

new.x <- c(2024, 2025, 2026, 2027, 2028)
new.df <- data.frame(x=new.x)

new.y <- predict(poly.lm1, newdata=new.df)
new.y <- predict(poly.lm2, newdata=new.df)

x_axis <- seq(2010, 2023, length=14)
lines(x, predict(poly.lm1, data.frame(x=x)), col='blue')
lines(x, predict(poly.lm2, data.frame(x=x)), col='red')