The good folks at IPUMS have created a library to read in their dat1 from the .gz file that you download. Be sure you right click and save the DDI codebook when you create your extract

library(ipumsr)
ddi <- read_ipums_ddi("/Users/garlandcarter/Dropbox/Mac/Desktop/GISHomework/Calipuma.xml")
dat1 <-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.
dat1<-haven::zap_labels(dat1) #necessary to avoid problems with "labelled" dat1 class
names(dat1)<-tolower(names(dat1))
dat1<-haven::zap_labels(dat1)
class(dat1)
## [1] "spec_tbl_df" "tbl_df"      "tbl"         "data.frame"

Load some other packages

library(survey, quietly = T)
library(tidyverse, quietly = T)
## Warning: package 'tidyr' was built under R version 4.1.2
## Warning: package 'readr' was built under R version 4.1.2
library(car, quietly = T)
## Warning: package 'carData' was built under R version 4.1.2
library(ggplot2, quietly = T)
library(tigris, quietly = T)
## Warning: package 'tigris' was built under R version 4.1.2
library(classInt, quietly = T)
library(tmap, quietly = T)
## Warning: package 'tmap' was built under R version 4.1.2
library(ipumsr)

Download geographic dat1 for Public Use Microdat1 Areas

The Public Use Microdat1 Area is the lowest level of geography in the PUMS dat1. They correspond to greographic ares of ~ 100,000 people.

options(tigris_class = "sf")
pumas<-pumas(state = "CA",
             year = 2019,
             cb = T)


plot(pumas["GEOID10"],
     main = "Public Use Microdat1 Areas in California")

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

Prepare variables

Here I recode several demographic variables

dat1$pwt <- dat1$perwt
dat1$hwt <- dat1$hhwt

dat1$mig<- ifelse(dat1$migrate1>1, 1,0 )

Generate survey design object

Here we identify the person weights and the survey design variables.

des<-survey::svydesign(ids = ~cluster,
               strata = ~ strata,
               weights = ~perwt,
               data= dat1)

perform survey estimation for PUMAs

The svyby() function allows us calculate estimates for different sub-domains within the dat1, this could be a demographic characteristic, but we’ll use our geographic level. Of course you could do both….

#test<-svytable(~I(cit=="US born")+puma+sex, design=des )

puma_est_edu<-svyby(formula = ~mig,
                    by = ~puma,
                    design = des,
                    FUN=svymean,
                    na.rm = TRUE )
head(puma_est_edu)
##     puma        mig          se
## 101  101 0.25381149 0.009312584
## 102  102 0.17249132 0.006723822
## 103  103 0.12378144 0.006596332
## 104  104 0.10786312 0.008221215
## 105  105 0.11013835 0.006718592
## 106  106 0.07652232 0.005998268

join to geography

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

geo1<-left_join(pumas, puma_est_edu, 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.1453726 0.009588211
## 2     P0 9241426488 1253864712 2300 0.1960088 0.009523201
## 3     P0 2152449674   13432167 8506 0.1210222 0.009195527
## 4     P0  645481741    4500965 6506 0.1653621 0.010414257
## 5     P0 9778407493  186302040 8900 0.1410088 0.007415338
## 6     P0 3094034997  246117939 6103 0.1249433 0.009398838
##                         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

Employment rates by PUMA

tmap_mode("view")
## tmap mode set to interactive viewing
geo1%>%
  tm_shape()+
 tm_polygons("mig",
             style="kmeans",
            n=8,
            legend.hist = TRUE) +
 tm_layout(legend.outside = TRUE,
           title = "Use Microdat1 Areas in California")