##Slide 1: Introduction
## Loading required package: sp
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, were retired in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## [1] "list"
## county cases population race gender age
## 1 adams 0 1492 o f Under.40
## 2 adams 0 365 o f 40.59
## 3 adams 1 68 o f 60.69
## 4 adams 0 73 o f 70+
## 5 adams 0 23351 w f Under.40
## 6 adams 5 12136 w f 40.59
## county smoking
## 1 adams 0.234
## 2 allegheny 0.245
## 3 armstrong 0.250
## 4 beaver 0.276
## 5 bedford 0.228
## 6 berks 0.249
##
## 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
# Group data by county and calculate sum of cases
d <- group_by(pennLC$data, county) %>% summarise(Y = sum(cases))
# Order data based on relevant variables
pennLC$data <- pennLC$data[order(pennLC$data$county, pennLC$data$population, pennLC$data$race, pennLC$data$gender, pennLC$data$age),]
# Calculate expected cases using the expected function
E <- expected(population = pennLC$data$population, cases = pennLC$data$cases, n.strata = 16)
d$E <- E[match((d$county), unique(pennLC$data$county))]
# Merge smoking information into the dataset
d <- merge(d, pennLC$smoking, by = "county")
# Calculate Standardized Mortality Ratio (SMR)
d$smr <- d$Y / d$E
# Add SMR to the spatial map
library(sp)
rownames(d) <- d$county
map <- SpatialPolygonsDataFrame(map, d, match.ID = TRUE)## Loading required package: spData
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
## Loading required package: sf
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
## Loading required package: Matrix
## Loading required package: foreach
## Loading required package: parallel
## This is INLA_22.12.16 built 2022-12-23 13:43:44 UTC.
## - See www.r-inla.org/contact-us for how to get help.
## - To enable PARDISO sparse library; see inla.pardiso()
# Create neighborhood matrix and convert it for INLA
nb <- poly2nb(map)
nb2INLA("map.adj", nb)
g <- inla.read.graph(filename = "map.adj")
# Fit the model using INLA
map$re_u <- 1:nrow(map@data)
map$re_v <- 1:nrow(map@data)
# Define model formula for INLA
formula <- Y ~ smoking + f(re_u, model = "besag", graph = g, scale.model = TRUE) + f(re_v, model = "iid")
res <- inla(formula, family = "poisson", data = map@data, E = E, control.predictor = list(compute = TRUE))
# Display summary of the model results
summary(res)##
## Call:
## c("inla.core(formula = formula, family = family, contrasts = contrasts,
## ", " data = data, quantiles = quantiles, E = E, offset = offset, ", "
## scale = scale, weights = weights, Ntrials = Ntrials, strata = strata,
## ", " lp.scale = lp.scale, link.covariates = link.covariates, verbose =
## verbose, ", " lincomb = lincomb, selection = selection, control.compute
## = control.compute, ", " control.predictor = control.predictor,
## control.family = control.family, ", " control.inla = control.inla,
## control.fixed = control.fixed, ", " control.mode = control.mode,
## control.expert = control.expert, ", " control.hazard = control.hazard,
## control.lincomb = control.lincomb, ", " control.update =
## control.update, control.lp.scale = control.lp.scale, ", "
## control.pardiso = control.pardiso, only.hyperparam = only.hyperparam,
## ", " inla.call = inla.call, inla.arg = inla.arg, num.threads =
## num.threads, ", " blas.num.threads = blas.num.threads, keep = keep,
## working.directory = working.directory, ", " silent = silent, inla.mode
## = inla.mode, safe = FALSE, debug = debug, ", " .parent.frame =
## .parent.frame)")
## Time used:
## Pre = 1.52, Running = 0.175, Post = 0.0161, Total = 1.71
## Fixed effects:
## mean sd 0.025quant 0.5quant 0.975quant mode kld
## (Intercept) -0.408 0.230 -0.859 -0.409 0.046 -0.410 0
## smoking 1.967 0.961 0.073 1.969 3.851 1.974 0
##
## Random effects:
## Name Model
## re_u Besags ICAR model
## re_v IID model
##
## Model hyperparameters:
## mean sd 0.025quant 0.5quant 0.975quant mode
## Precision for re_u 14538.00 16058.43 489.48 9206.52 57498.17 991.63
## Precision for re_v 52.90 15.02 29.58 50.82 88.24 46.89
##
## Marginal log-Likelihood: -305.08
## is computed
## Posterior summaries for the linear predictor and the fitted values are computed
## (Posterior marginals needs also 'control.compute=list(return.marginals.predictor=TRUE)')
# Load ggplot2 library
library(ggplot2)
# Visualize the marginal distribution of the smoking variable
marginal <- inla.smarginal(res$marginals.fixed$smoking)
marginal <- data.frame(marginal)
ggplot(marginal, aes(x = x, y = y)) +
geom_line() +
labs(x = expression(beta[1]), y = "Density") +
geom_vline(xintercept = 0, col = "blue") +
theme_bw()## mean sd 0.025quant 0.5quant 0.975quant
## fitted.Predictor.01 0.9801809 0.09444246 0.8066716 0.9760759 1.177236
## fitted.Predictor.02 1.0329846 0.02835081 0.9784856 1.0325993 1.089677
## fitted.Predictor.03 1.0222765 0.10154864 0.8355213 1.0179397 1.233965
## fitted.Predictor.04 1.1459813 0.07727443 1.0018079 1.1434048 1.304842
## fitted.Predictor.05 1.0612933 0.11464296 0.8540301 1.0551050 1.304025
## fitted.Predictor.06 1.0133880 0.05333826 0.9127113 1.0120159 1.121883
## mode
## fitted.Predictor.01 0.9681003
## fitted.Predictor.02 1.0318269
## fitted.Predictor.03 1.0095461
## fitted.Predictor.04 1.1382895
## fitted.Predictor.05 1.0432326
## fitted.Predictor.06 1.0092793
# Add estimated Relative Risk (RR) to the spatial map
map$RR <- res$summary.fitted.values[, "mean"]
map$LL <- res$summary.fitted.values[, "0.025quant"]
map$Ul <- res$summary.fitted.values[, "0.975quant"]
# Create leaflet map with RR values
pal <- colorNumeric(palette = "YlOrRd", domain = map$RR)
labels <- sprintf("<strong>%s</strong><br/> Observed: %s <br/> Expected: %s <br/> Smokers proportion: %s <br/>SMR: %s <br/>RR: %s (%s, %s)",
map$county, map$Y, round(map$E, 2), map$smoking, round(map$smr, 2),
round(map$RR, 2), round(map$LL, 2), round(map$Ul, 2)) %>%
lapply(htmltools::HTML)
# Create an interactive map with RR values
leaflet(map) %>% addTiles() %>%
addPolygons(color = "grey", weight = 1, fillColor = ~pal(RR), fillOpacity = 0.5,
highlightOptions = highlightOptions(weight = 4),
label = labels,
labelOptions = labelOptions(style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px", direction = "auto")) %>%
addLegend(pal = pal, values = ~RR, opacity = 0.5, title = "RR", position = "bottomright")