library(ipumsr)
## Warning: package 'ipumsr' was built under R version 4.0.5
ddi <- read_ipums_ddi("C:/Users/adolp/Desktop/GIS/usa_00001 (DDI).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
library(survey)
library(dplyr)
library(car)
library(ggplot2)
library(tigris)
library(classInt)
library(tmap)
library(mapview)

names(data)<-tolower(names(data))
data$pwt <- data$perwt/100
data$hwt <- data$hhwt/100

data$mig <- recode(data$migrate1, recodes = "0=NA; 9=NA; 1= 1; 2:4=0")
des<-svydesign(ids = ~cluster,
               strata = ~ strata,
               weights = ~pwt,
               data = data)
summary(data$mig)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   0.000   1.000   1.000   0.874   1.000   1.000   17752
puma_est_sameHH<-svyby(formula = ~mig,
                    by = ~puma,
                    design = des,
                    FUN=svymean,
                    na.rm = TRUE )
head(puma_est_sameHH)
##     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
pumas$puma<-as.numeric(pumas$PUMACE10)

geo1<-geo_join(pumas, puma_est_sameHH, 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
## bbox:           xmin: -124.4096 ymin: 33.46296 xmax: -116.8412 ymax: 41.46584
## geographic 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 rank
## 1     P0  344472696    7555813 9702 0.8532523 0.009686046    1
## 2     P0 9241426488 1253864712 2300 0.8019943 0.009648508    1
## 3     P0 2152449674   13432167 8506 0.8776152 0.009286238    1
## 4     P0  645481741    4500965 6506 0.8328268 0.010520474    1
## 5     P0 9778407493  186302040 8900 0.8575320 0.007507578    1
## 6     P0 3094034997  246117939 6103 0.8741276 0.009498078    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...
tmap_mode("view")
## tmap mode set to interactive viewing
tm_basemap("OpenStreetMap.Mapnik")+
  tm_shape(geo1)+
  tm_polygons("mig",
              style="kmeans",
              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 \n 2014-2019") 
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1