Load some other packages

library(survey)
library(dplyr)
library(car)
library(ggplot2)
library(tigris)
library(classInt)
library(tmap)
library(ipumsr)
## Warning: package 'ipumsr' was built under R version 4.0.5
ddi <- read_ipums_ddi("C:/Users/chris/University of Texas at San Antonio/SPRING 2021/DEM 7093 GEOGRAPHIC INFORMATION FOR POPULATION SCIENCE/Homeworks/GIS HMWK/HOMEWORK 7/usa_00002.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

Download geographic data for Public Use Microdata Areas

options(tigris_class = "sf")
pumas<-pumas(state = "CA", year = 2019, cb = T)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |======================================================================| 100%
plot(pumas["GEOID10"], main = "Public Use Microdata Areas in California")

names(data)<-tolower(names(data))

Recode variables

#weight variables, these have implied decimal places so we have to divde by 100, following the codebook
data$pwt <- data$perwt/100
data$hwt <- data$hhwt/100


# Migration
data$migration <- Recode(data$migrate1, recodes ="1=0; 2:4=1; else=0")

#race/ethnicity
data$hisp <- Recode(data$hispan, recodes = "9=NA; 1:4='Hispanic'; 0='NonHispanic'")
data$race_rec <- Recode(data$race, recodes = "1='White'; 2='Black'; 3='Other'; 4:6='Asian'; 7:9='Other'")
data$race_eth <- interaction(data$hisp, data$race_rec, sep = "_")
data$race_eth  <- as.factor(ifelse(substr(as.character(data$race_eth),1,8) == "Hispanic", "Hispanic", as.character(data$race_eth)))
data$race_eth <- relevel(data$race_eth, ref = "NonHispanic_White")

#sex
data$male <- ifelse(data$sex == 1,1,0)

#education
data$educ_level<- Recode(data$educd, recodes = "2:61='0LT_HS';62:64='1_HSD/GED';65:80='2_somecoll';90:100='2_somecoll'; 81:83='3_AssocDegree';101='4_bachelordegree'; 110:116='4_BAplus_GradDegree'; else=NA")

#education
data$educ_level<- Recode(data$educd, recodes = "2:61='0LT_HS';62:64='1_HSD/GED';65:80='2_somecoll';90:100='2_somecoll'; 81:83='3_AssocDegree';101='4_bachelordegree'; 110:116='4_BAplus_GradDegree'; else=NA")

Survey design

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

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

Survey Estimation for PUMAs

puma_est_edu<-svyby(formula = ~educ_level,
                    by = ~puma,
                    design = subset(des, age>25),
                    FUN=svymean,
                    na.rm = TRUE )

puma_est_migrate<-svyby(formula = ~migration,
                       by = ~puma,
                       design=des,
                       FUN=svymean,
                       na.rm = TRUE )

head(puma_est_edu)
##     puma educ_level0LT_HS educ_level1_HSD/GED educ_level2_somecoll
## 101  101       0.03771076          0.06569605            0.1166131
## 102  102       0.16054167          0.15357159            0.1579987
## 103  103       0.05152757          0.08422222            0.1280499
## 104  104       0.31012651          0.24563983            0.2083160
## 105  105       0.13347136          0.18646181            0.1905626
## 106  106       0.14333595          0.23360220            0.2146757
##     educ_level3_AssocDegree educ_level4_bachelordegree
## 101              0.04322687                  0.3299605
## 102              0.06094609                  0.2850304
## 103              0.05430587                  0.3451345
## 104              0.05510831                  0.1160381
## 105              0.07115222                  0.2688053
## 106              0.07644186                  0.2248899
##     educ_level4_BAplus_GradDegree se.educ_level0LT_HS se.educ_level1_HSD/GED
## 101                    0.40679279         0.003905639            0.004448080
## 102                    0.18191149         0.006521332            0.006326229
## 103                    0.33675991         0.004472209            0.005309984
## 104                    0.06477123         0.011587978            0.009193415
## 105                    0.14954676         0.006885012            0.007559093
## 106                    0.10705441         0.007508320            0.008802248
##     se.educ_level2_somecoll se.educ_level3_AssocDegree
## 101             0.005691604                0.003405890
## 102             0.005625829                0.003542165
## 103             0.005791771                0.004021030
## 104             0.008470772                0.004609917
## 105             0.006335683                0.004081108
## 106             0.007564748                0.004487001
##     se.educ_level4_bachelordegree se.educ_level4_BAplus_GradDegree
## 101                   0.008030658                      0.008809076
## 102                   0.007069620                      0.005803563
## 103                   0.008150823                      0.008192403
## 104                   0.006230842                      0.004944989
## 105                   0.007454848                      0.005514219
## 106                   0.008090311                      0.005358965
head(puma_est_migrate)
##     puma  migration          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<-geo_join(pumas, puma_est_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
head(geo1)
## Simple feature collection with 6 features and 12 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 migration          se rank
## 1     P0  344472696    7555813 9702 0.1453726 0.009588211    1
## 2     P0 9241426488 1253864712 2300 0.1960088 0.009523201    1
## 3     P0 2152449674   13432167 8506 0.1210222 0.009195527    1
## 4     P0  645481741    4500965 6506 0.1653621 0.010414257    1
## 5     P0 9778407493  186302040 8900 0.1410088 0.007415338    1
## 6     P0 3094034997  246117939 6103 0.1249433 0.009398838    1
##                         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...

Migration rates by PUMA

tmap_mode("plot")
## tmap mode set to plotting
tm_basemap("OpenStreetMap.Mapnik")+
  tm_shape(geo1)+
  tm_polygons("migration",
              style="kmeans",
              n=8,
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE,
            title = "Migration rate in California PUMAs \n 2015-2019")
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1

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