Loading Data
library(ipumsr)
## Warning: package 'ipumsr' was built under R version 4.0.5
ddi <- read_ipums_ddi("usa_00001.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 "labeled" data
Loading Packages
library(survey)
library(dplyr)
library(car)
library(ggplot2)
library(tigris)
library(classInt)
library(tmap)
Getting Geographies
options(tigris_class = "sf")
pumas<-pumas(state = "CA", year = 2019, cb = T)
plot(pumas["GEOID10"], main = "Public Use Microdata Areas in California")

Preping Data
names(data)<-tolower(names(data))
data$pwt <- data$perwt/100
data$hwt <- data$hhwt/100
binary <- data %>%
mutate(migratehome = ifelse(migrate1 == 1, 1, 0)) #interested in people who stay home
Survey Design Object
des<-svydesign(ids = ~cluster,
strata = ~ strata,
weights = ~pwt,
data = binary)
Survey Estimation for PUMAS
puma_migrate<-svyby(formula = ~migratehome,
by = ~puma,
design = des,
FUN=svymean,
na.rm = TRUE )
head(puma_migrate)
## puma migratehome se
## 101 101 0.7385691 0.009302239
## 102 102 0.8185901 0.006772884
## 103 103 0.8658547 0.006817086
## 104 104 0.8771703 0.008387551
## 105 105 0.8809290 0.006862146
## 106 106 0.9134246 0.006162196
Merging Estimates with Geography
pumas$puma<-as.numeric(pumas$PUMACE10)
geo1<-geo_join(pumas, puma_migrate, by_sp="puma",by_df= "puma")
## Warning: `group_by_()` was deprecated in dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
tm_basemap("OpenStreetMap.Mapnik")+
tm_shape(geo1)+
tm_polygons("migratehome",
style="kmeans",
n=8,
legend.hist = TRUE) +
tm_layout(legend.outside = TRUE,
title = "Stationary rate in California PUMAs")
## Warning: package 'sf' was built under R version 4.0.4
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1

## [1] "#FFFDDB" "#FEF1AF" "#FED97B" "#FEB441" "#F68820" "#DB5D0A" "#AF3E03"
## [8] "#7A2A05"