This exercise uses IPUMS USA data to produce survey-based estimates for % of the population that lived in the same house for California PUMAs in 2015-2019. The task uses the 2015-2019 ACS 5-year micro data.

library(ipumsr)
## Warning: package 'ipumsr' was built under R version 4.1.3
ddi <- read_ipums_ddi("C:/Users/anami/OneDrive/Desktop/GIS_CLASSFINAL/Assignment 7/usa_00003.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))

Load some other Packages

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 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.1.1     v 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)

Download geographic data for PUMA

options(tigris_class = "sf")
pumas<-pumas(state = "CA",
             year = 2019,
             cb = T)
plot(pumas["GEOID10"],
     main = "Public Use Microdata Areas in California")

mapview::mapview(pumas, zcol= "GEOID10")

Prepare Variables

data$pwt <- data$perwt
data$hwt <- data$hhwt
data$mig <- Recode(data$migrate1, recodes = "1=1; 0=NA; else=0")

Generate survey design object

des<-svydesign(ids = ~cluster,
               strata = ~strata,
               weights = ~pwt,
               data = data)

Perform survey estimation for PUMAs

puma_est_mig<-svyby(formula = ~mig,
                    by = ~puma,
                    design = des,
                    FUN=svymean,
                    na.rm = TRUE )
head(puma_est_mig)
##     puma       mig          se
## 101  101 0.7442398 0.009366589
## 102  102 0.8259565 0.006778309
## 103  103 0.8749223 0.006679989
## 104  104 0.8904980 0.008340314
## 105  105 0.8888690 0.006785526
## 106  106 0.9227006 0.006058942

Join to geography

pumas$puma<-as.numeric(pumas$PUMACE10)

geo1<-left_join(pumas, puma_est_mig, by=c("puma"= "puma"))
head(geo1)
## Simple feature collection with 6 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -124.4096 ymin: 33.46296 xmax: -116.8412 ymax: 41.46584
## Geodetic CRS:  NAD83
##   STATEFP10 PUMACE10       AFFGEOID10 GEOID10
## 1        06    09702 7950000US0609702 0609702
## 2        06    02300 7950000US0602300 0602300
## 3        06    08506 7950000US0608506 0608506
## 4        06    06506 7950000US0606506 0606506
## 5        06    08900 7950000US0608900 0608900
## 6        06    06103 7950000US0606103 0606103
##                                                                     NAME10
## 1            Sonoma County (South)--Petaluma, Rohnert Park & Cotati Cities
## 2                                                          Humboldt County
## 3 Santa Clara County (East)--Gilroy, Morgan Hill & San Jose (South) Cities
## 4                    Riverside County (Southwest)--Hemet City & East Hemet
## 5                                              Shasta County--Redding City
## 6         Placer County (East/High Country Region)--Auburn & Colfax Cities
##   LSAD10    ALAND10   AWATER10 puma       mig          se
## 1     P0  344472696    7555813 9702 0.8532523 0.009686046
## 2     P0 9241426488 1253864712 2300 0.8019943 0.009648508
## 3     P0 2152449674   13432167 8506 0.8776152 0.009286238
## 4     P0  645481741    4500965 6506 0.8328268 0.010520474
## 5     P0 9778407493  186302040 8900 0.8575320 0.007507578
## 6     P0 3094034997  246117939 6103 0.8741276 0.009498078
##                         geometry
## 1 MULTIPOLYGON (((-122.7418 3...
## 2 MULTIPOLYGON (((-124.4086 4...
## 3 MULTIPOLYGON (((-121.8558 3...
## 4 MULTIPOLYGON (((-117.1456 3...
## 5 MULTIPOLYGON (((-123.0688 4...
## 6 MULTIPOLYGON (((-121.4104 3...

Map estimates

% of the population lived in the same house last year

tmap_mode("view")
## tmap mode set to interactive viewing
geo1%>%
  tm_shape()+
  tm_polygons("mig",
              style="kmeans",title=("% in the same house"), 
              n=8,
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE,
            title = "% of the Population that lived in the same house last year in California PUMAs in 2015-2019") 
tmap_mode("view")
## tmap mode set to interactive viewing
tm_basemap("OpenStreetMap.Mapnik")+
  tm_shape(geo1)+
  tm_polygons("mig",
              style="kmeans",
              title=c("%  in the same house"),
              n=8,
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE,
            title = "% in the same house last year in California PUMAs 2015-2019")