Load

setwd("/Users/asiyavalidova/Dropbox/Demography/GIS/w10/IPUMS")
library(ipumsr)
ddi <- read_ipums_ddi("usa_00004.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) 
names(data)<-tolower(names(data))
library(survey)
## Loading required package: grid
## Loading required package: Matrix
## Loading required package: survival
## 
## Attaching package: 'survey'
## The following object is masked from 'package:graphics':
## 
##     dotchart
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
library(ggplot2)
library(tigris)
## To enable 
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
library(classInt)
library(tmap)

### Download geographic data for Public Use Microdata Areas

options(tigris_class = "sf")
pumas<-pumas(state = "TX", year = 2019, cb = T)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
plot(pumas["GEOID10"], main = "Public Use Microdata Areas in Texas")

names(pumas)<-tolower(names(pumas))
pumas$puma<-as.numeric(pumas$pumace10)
data<-data%>%
      mutate(
          migrate=Recode(migrate1,recodes="1='not_migrate';2:4='migrate';else=NA")
      )
data<-data%>%
       filter(!is.na(migrate))
data$pwt <- data$perwt/100  

Generate survey design object

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

perform survey estimation

puma_est_migr<-svyby(formula=~migrate,
                     by=~puma,
                     design = des,
                     FUN=svymean,na.rm=T)

join to geography

geo<-geo_join(pumas, puma_est_migr, 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
geo$not_migrate<-geo$migratenot_migrate*100

head(geo)
## Simple feature collection with 6 features and 15 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -98.56392 ymin: 27.82079 xmax: -93.83772 ymax: 32.4076
## geographic CRS: NAD83
##   statefp10 pumace10       affgeoid10 geoid10
## 1        48    04302 7950000US4804302 4804302
## 2        48    05500 7950000US4805500 4805500
## 3        48    05000 7950000US4805000 4805000
## 4        48    05901 7950000US4805901 4805901
## 5        48    06500 7950000US4806500 4806500
## 6        48    01700 7950000US4801700 4801700
##                                                                            name10
## 1                      Jefferson County (Outside Beaumont City)--Port Arthur City
## 2 Golden Crescent Regional Planning Commission (North) & Wilson & Karnes Counties
## 3                                           Houston-Galveston Area Council (West)
## 4                                                      San Antonio City (Central)
## 5                                                    Coastal Bend COG (Northeast)
## 6                    East Texas COG (Southeast)--Rusk, Cherokee & Panola Counties
##   lsad10     aland10   awater10 puma migratemigrate migratenot_migrate
## 1     P0  2072607049  604906509 4302     0.15496671          0.8450333
## 2     P0 16001092181  131878837 5500     0.11821855          0.8817814
## 3     P0 11134586598 1439210499 5000     0.09854454          0.9014555
## 4     P0    49642078     744374 5901     0.14190722          0.8580928
## 5     P0  6724202765  876050758 6500     0.16398316          0.8360168
## 6     P0  7221787148   86900821 1700     0.14791047          0.8520895
##   se.migratemigrate se.migratenot_migrate rank                       geometry
## 1       0.009040789           0.009040789    1 MULTIPOLYGON (((-94.44491 3...
## 2       0.007426341           0.007426341    1 MULTIPOLYGON (((-98.39758 2...
## 3       0.006337391           0.006337391    1 MULTIPOLYGON (((-96.87422 2...
## 4       0.008863613           0.008863613    1 MULTIPOLYGON (((-98.56338 2...
## 5       0.010124744           0.010124744    1 MULTIPOLYGON (((-96.8229 28...
## 6       0.008621732           0.008621732    1 MULTIPOLYGON (((-95.46224 3...
##   not_migrate
## 1    84.50333
## 2    88.17814
## 3    90.14555
## 4    85.80928
## 5    83.60168
## 6    85.20895

Map estimates

tmap_mode("plot")
## tmap mode set to plotting
tm_basemap("OpenStreetMap.Mapnik")+
  tm_shape(geo)+
  tm_polygons("not_migrate",
              style="kmeans",
              n=8,
              legend.hist = TRUE) +
  tm_layout(legend.outside = TRUE,
            title = "Migration rate in Texas PUMAs  2019") 
## Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1