#Q1 

library(tidycensus)
library(ggplot2)


Hidalgodata <- c ("B19013_001E","B03002_012E","B01001B_001E","B01001_002E","B01001_026E","B01001_001E","B01002_001E")
Hidalgodatanew <- get_acs (geography = "tract", variables = Hidalgodata ,
                           state = "TX",county = "Hidalgo",
                           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%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
#Q2
Hidalgodatanew$B19013_001M <- NULL
Hidalgodatanew$B03002_012M <- NULL
Hidalgodatanew$B01001B_001M <- NULL
Hidalgodatanew$B01001_002M <- NULL
Hidalgodatanew$B01002_001M <- NULL
Hidalgodatanew$B01001_001M <- NULL
Hidalgodatanew$B01001_026M <- NULL

names(Hidalgodatanew)[3] <- 'Median income'
names(Hidalgodatanew)[4] <- 'Hispanic population'
names(Hidalgodatanew)[5] <- 'Black population'
names(Hidalgodatanew)[6] <- 'Hidalgo male Pop.'
names(Hidalgodatanew)[7] <- 'Hidalgo female Pop.'
names(Hidalgodatanew)[8] <- 'Hidalgo Total Pop.'
names(Hidalgodatanew)[9] <- 'median age'

#Q3
write.csv(Hidalgodatanew,"/Users/sophiawilson/Desktop/Fall 2023/Methods 1/Projectonesrw.csv")

#Q4
Hidalgodatanew <- na.omit(Hidalgodatanew)
Hidalgodatanew$Percent <- 100*Hidalgodatanew$`Black population`/Hidalgodatanew$`Hidalgo Total Pop.`
ggplot(data = Hidalgodatanew, aes(x = "Median income", y ="Percent" )) + 
  geom_point()

#Q5 
a <- ggplot(Hidalgodatanew, aes(x = Hidalgodatanew$`median age`))
a + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#Q6
a <- ggplot(Hidalgodatanew, aes( x=Hidalgodatanew$`Median income` ))
a + geom_density()

#Q7
a <- ggplot(Hidalgodatanew, aes( x=Hidalgodatanew$`Median income` ))
a + stat_ecdf()

#Q8
a <- ggplot(Hidalgodatanew, aes( x=Hidalgodatanew$`Median income` ))
a + geom_boxplot()

#Q9
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')
tm_shape(Hidalgodatanew) +tm_fill(col = "Hispanic population")+ tm_layout(title = "Hispanic Pop map")

#Q10
Hidalgodatanew$variation <- Hidalgodatanew$`Hidalgo male Pop.` - Hidalgodatanew$`Hidalgo female Pop.`
tm_shape(Hidalgodatanew) +tm_fill(col = "variation")+ tm_layout(title = "Gender Difference")
## Variable(s) "variation" 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