library(ipumsr)
library(survey, quietly = T)
## 
## Attaching package: 'survey'
## The following object is masked from 'package:graphics':
## 
##     dotchart
library(tidyverse, quietly = T)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.8
## ✓ tidyr   1.2.0     ✓ stringr 1.4.0
## ✓ readr   2.0.1     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x tidyr::expand() masks Matrix::expand()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## x tidyr::pack()   masks Matrix::pack()
## x tidyr::unpack() masks Matrix::unpack()
library(car, quietly = T)
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
library(ggplot2, quietly = T)
library(tigris, quietly = T)
## To enable 
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
library(classInt, quietly = T)
library(tmap, quietly = T)
ddi <- read_ipums_ddi("/Users/drewschaefer/Downloads/GIS-Dem7093/GIS/usa_00005.xml")
data <- read_ipums_micro(ddi)
## Use of data from IPUMS USA is subject to conditions including that users should
## cite the data appropriately. Use command `ipums_conditions()` for more details.
data<-haven::zap_labels(data) #necessary to avoid problems with "labelled" data class
names(data)<-tolower(names(data))
data$pwt <- data$perwt
data$hwt <- data$hhwt

data$samehouse <- Recode(data$migrate1, recodes = "0=NA; 1=1; 2:9=0")
options(tigris_class = "sf")
pumas<-pumas(state = "CA",
             year = 2018,
             cb = T)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |======================================================================| 100%
plot(pumas["GEOID10"],
     main = "Public Use Microdata Areas in California")

mapview::mapview(pumas, zcol= "GEOID10")
## Warning: replacing previous import 'terra::extend' by 'raster::extend' when
## loading 'satellite'
## Warning: replacing previous import 'terra::crop' by 'raster::crop' when loading
## 'satellite'
## Warning: multiple methods tables found for 'crop'
## Warning: multiple methods tables found for 'extend'
des<-svydesign(ids = ~cluster,
               strata = ~ strata,
               weights = ~pwt,
               data = data)
puma_est_house<-svyby(formula = ~samehouse,
                    by = ~puma,
                    design = des,
                    FUN=svymean,
                    na.rm = TRUE )
pumas$puma<-as.numeric(pumas$PUMACE10)
geo1<-left_join(pumas, puma_est_house, by=c("puma"= "puma"))
head(geo1)
## Simple feature collection with 6 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -123.0688 ymin: 32.56534 xmax: -116.0811 ymax: 41.18493
## Geodetic CRS:  NAD83
##   STATEFP10 PUMACE10       AFFGEOID10 GEOID10
## 1        06    04101 7950000US0604101 0604101
## 2        06    01309 7950000US0601309 0601309
## 3        06    07104 7950000US0607104 0607104
## 4        06    08900 7950000US0608900 0608900
## 5        06    05905 7950000US0605905 0605905
## 6        06    07302 7950000US0607302 0607302
##                                                                      NAME10
## 1           Marin County (North & West)--Novato & San Rafael (North) Cities
## 2                     Contra Costa County (East)--Brentwood & Oakley Cities
## 3 San Bernardino County (Southwest)--Phelan, Lake Arrowhead & Big Bear City
## 4                                               Shasta County--Redding City
## 5 Orange County (Northeast)--Lake Forest, Irvine (North) Cities & Silverado
## 6        San Diego County (North & East)--Fallbrook, Alpine & Valley Center
##   LSAD10    ALAND10  AWATER10 puma samehouse          se
## 1     P0 1218249181 765416684 4101 0.8638056 0.011133363
## 2     P0  365953896  56398672 1309 0.8926442 0.010129984
## 3     P0 2957261879  22807769 7104 0.8857259 0.009408765
## 4     P0 9778230074 186479459 8900 0.8575320 0.007507578
## 5     P0  374559889   2183758 5905 0.8663146 0.007305893
## 6     P0 6961523076  32694855 7302 0.8672727 0.009832366
##                         geometry
## 1 MULTIPOLYGON (((-123.0241 3...
## 2 MULTIPOLYGON (((-121.7789 3...
## 3 MULTIPOLYGON (((-117.6817 3...
## 4 MULTIPOLYGON (((-123.0688 4...
## 5 MULTIPOLYGON (((-117.8449 3...
## 6 MULTIPOLYGON (((-117.5097 3...
tmap_mode("view")
## tmap mode set to interactive viewing
geo1%>%
  tm_shape()+
  tm_polygons("samehouse",
              style="kmeans",
              n=8,
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE,
            title = "Living in same house rate in California PUMAs \n 2015-2019")