``` r
library(sf)
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(scales)
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
library(ggplot2)
library(dplyr)
library(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(mapview)
remotes::install_github('r-tmap/tmap')
## Downloading GitHub repo r-tmap/tmap@HEAD
## Rcpp (1.0.12 -> 1.0.14 ) [CRAN]
## e1071 (1.7-14 -> 1.7-16 ) [CRAN]
## rlang (1.1.4 -> 1.1.5 ) [CRAN]
## classInt (0.4-10 -> 0.4-11 ) [CRAN]
## sf (1.0-16 -> 1.0-19 ) [CRAN]
## colorspace (2.1-0 -> 2.1-1 ) [CRAN]
## glue (1.7.0 -> 1.8.0 ) [CRAN]
## terra (1.7-78 -> 1.8-15 ) [CRAN]
## digest (0.6.36 -> 0.6.37 ) [CRAN]
## raster (3.6-26 -> 3.6-31 ) [CRAN]
## XML (3.99-0.17 -> 3.99-0.18) [CRAN]
## Installing 11 packages: Rcpp, e1071, rlang, classInt, sf, colorspace, glue, terra, digest, raster, XML
## Warning: package 'sf' is in use and will not be installed
## Installing packages into 'C:/Users/drven/AppData/Local/R/win-library/4.4'
## (as 'lib' is unspecified)
## package 'Rcpp' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'Rcpp'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\drven\AppData\Local\R\win-library\4.4\00LOCK\Rcpp\libs\x64\Rcpp.dll to
## C:\Users\drven\AppData\Local\R\win-library\4.4\Rcpp\libs\x64\Rcpp.dll:
## Permission denied
## Warning: restored 'Rcpp'
## package 'e1071' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'e1071'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\drven\AppData\Local\R\win-library\4.4\00LOCK\e1071\libs\x64\e1071.dll
## to C:\Users\drven\AppData\Local\R\win-library\4.4\e1071\libs\x64\e1071.dll:
## Permission denied
## Warning: restored 'e1071'
## package 'rlang' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'rlang'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\drven\AppData\Local\R\win-library\4.4\00LOCK\rlang\libs\x64\rlang.dll
## to C:\Users\drven\AppData\Local\R\win-library\4.4\rlang\libs\x64\rlang.dll:
## Permission denied
## Warning: restored 'rlang'
## package 'classInt' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'classInt'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\drven\AppData\Local\R\win-library\4.4\00LOCK\classInt\libs\x64\classInt.dll
## to
## C:\Users\drven\AppData\Local\R\win-library\4.4\classInt\libs\x64\classInt.dll:
## Permission denied
## Warning: restored 'classInt'
## package 'colorspace' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'colorspace'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\drven\AppData\Local\R\win-library\4.4\00LOCK\colorspace\libs\x64\colorspace.dll
## to
## C:\Users\drven\AppData\Local\R\win-library\4.4\colorspace\libs\x64\colorspace.dll:
## Permission denied
## Warning: restored 'colorspace'
## package 'glue' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'glue'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\drven\AppData\Local\R\win-library\4.4\00LOCK\glue\libs\x64\glue.dll to
## C:\Users\drven\AppData\Local\R\win-library\4.4\glue\libs\x64\glue.dll:
## Permission denied
## Warning: restored 'glue'
## package 'terra' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'terra'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\drven\AppData\Local\R\win-library\4.4\00LOCK\terra\libs\x64\terra.dll
## to C:\Users\drven\AppData\Local\R\win-library\4.4\terra\libs\x64\terra.dll:
## Permission denied
## Warning: restored 'terra'
## package 'digest' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'digest'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\drven\AppData\Local\R\win-library\4.4\00LOCK\digest\libs\x64\digest.dll
## to C:\Users\drven\AppData\Local\R\win-library\4.4\digest\libs\x64\digest.dll:
## Permission denied
## Warning: restored 'digest'
## package 'raster' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'raster'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\drven\AppData\Local\R\win-library\4.4\00LOCK\raster\libs\x64\raster.dll
## to C:\Users\drven\AppData\Local\R\win-library\4.4\raster\libs\x64\raster.dll:
## Permission denied
## Warning: restored 'raster'
## package 'XML' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'XML'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\drven\AppData\Local\R\win-library\4.4\00LOCK\XML\libs\x64\XML.dll to
## C:\Users\drven\AppData\Local\R\win-library\4.4\XML\libs\x64\XML.dll: Permission
## denied
## Warning: restored 'XML'
##
## The downloaded binary packages are in
## C:\Users\drven\AppData\Local\Temp\Rtmp6j3BwN\downloaded_packages
## ── R CMD build ─────────────────────────────────────────────────────────────────
## checking for file 'C:\Users\drven\AppData\Local\Temp\Rtmp6j3BwN\remotes6aec51a3f6f\r-tmap-tmap-457173d/DESCRIPTION' ... ✔ checking for file 'C:\Users\drven\AppData\Local\Temp\Rtmp6j3BwN\remotes6aec51a3f6f\r-tmap-tmap-457173d/DESCRIPTION'
## ─ preparing 'tmap':
## checking DESCRIPTION meta-information ... checking DESCRIPTION meta-information ... ✔ checking DESCRIPTION meta-information
## ─ checking for LF line-endings in source and make files and shell scripts (480ms)
## ─ checking for empty or unneeded directories
## ─ building 'tmap_4.0.tar.gz'
##
##
## Warning: package 'tmap' is in use and will not be installed
library(magrittr)
##
## Attaching package: 'magrittr'
##
## The following object is masked from 'package:purrr':
##
## set_names
##
## The following object is masked from 'package:tidyr':
##
## extract
setwd(‘D:/isair_r/data/haiti’) adm2 <- st_read(‘adm2.geojson’, stringsAsFactors = F, quiet = TRUE)
#plot1
adm2_urb_plot <- ggplot(data = adm2, aes(x = urbanpct, y = dtp3coverage2016/100)) + geom_point() + xlab(“Population living in urban areas”) + ylab(“Population vaccinated with DTP3”) + scale_y_continuous(limits = c(0 , 1), labels = percent_format()) + scale_x_continuous(limits = c(0 , 1) ,labels = percent_format()) + theme_minimal()
adm2 <- mutate(adm2, vacc_fail = vacc_denom - vacc_num)
adm2_binom <- glm(data = adm2, cbind(vacc_num, vacc_fail) ~ urbanpct,
family = ‘binomial’)
summary(adm2_binom)
library (broom) tidy(adm2_binom, conf.int = TRUE) glance(adm2_binom)
head(fitted.values(adm2_binom))# this is the first six of 138 head(predict(adm2_binom, type = ‘response’))
setwd(‘D:/isair_r/data/rats’)
rats_aug <- augment(rats_lm) head(rats_aug)
head(predict(adm2_binom))
#Exercise: Add the predicted values (on the response scale) to the adm2 data frame and make aplot showing the predicted and observed values.
adm2 <- mutate(adm2, coverage_pred = predict(object = adm2_binom, newdata = adm2, type = ‘response’)) ggplot(data= adm2, aes(x = vacc_num/vacc_denom, y = coverage_pred)) + geom_point() + xlab(‘Observed’) + ylab(‘Predicted’) + theme_minimal() + geom_abline(intercept = 0 , slope = 1) + scale_x_continuous(limits = c(0,1), labels = percent_format()) + scale_y_continuous(limits = c(0,1), labels = percent_format()) + coord_equal()
adm2_pred <- data.frame( urbanpct = seq(from = 0, to = 1, length.out = 101))
adm2_pred %<>% mutate(coverage_pred = predict(object = adm2_binom, newdata = ., type =‘response’))
adm2_urb_plot + geom_line(data = adm2_pred, aes(x = urbanpct,y = coverage_pred), color =‘purple’) + coord_equal()
#Exercise: Create a spatial visualisation of the residuals (on the logit scale). Discuss whetherthey appear to be IID with correlation of 0.
adm2_binom_resid <- mutate(adm2, residuals = residuals(adm2_binom))
ggplot(data = adm2_binom_resid) + geom_sf(aes(fill = residuals)) + scale_fill_gradient2(midpoint = 0, name= “Residuals”) + theme_void() + theme(axis.text = element_blank())
RR <- head(adm2_pred\(coverage_pred, 1)/ tail(adm2_pred\)coverage_pred,1)
round(RR, 2)
adm2_poisson <- glm(data = adm2, vacc_num ~ offset(log(vacc_exp)) + urbanpct, family = ‘poisson’)
summary(adm2_poisson)
#Extension exercise: #Calculate the change in risk (relative) associated with a change fromurbanicity of 1 to 0 by exponentiating the coeffi cient.
tidy(adm2_poisson, conf.int = TRUE ) %>% filter(term == ‘urbanpct’) %>% select(estimate, conf.low, conf.high) %>% mutate_all(.funs = ~{1 - exp(-.)})