-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSectionB_Supplementary.R
131 lines (78 loc) · 3.46 KB
/
SectionB_Supplementary.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
rm(list=ls())
setwd(dirname(rstudioapi::getSourceEditorContext()$path))
# Load packages
library(sf)
library(leaflet)
library(leafpop)
library(RColorBrewer)
# Load the data
load("../Data/IHD_data.Rdata")
S <- nrow(counts)
T <- ncol(counts)-1
t.from <- 1999
t.to <- 2021
##################################################
# Number of missing observations for each county #
##################################################
x <- rowSums(is.na(counts))
Tab <- data.frame(GEOID=counts$GEOID, X=x)
# Percentage of total observations with NAs
(sum(Tab$X)/(S*T))*100
##################################################
# Counties with at least one missing observation #
##################################################
IDs23 <- Tab$GEOID[Tab$X==23]
IDs15to23 <- Tab$GEOID[Tab$X<23 & Tab$X>=15]
IDs15 <- Tab$GEOID[Tab$X<15 & Tab$X>0]
# 0="no NAs", 1="<15 NAs", 2=[15,23) NAs", 3="23 NAs"
carto$NAs <- rep(0, 3105)
carto$NAs[carto$GEOID %in% IDs23] <- 3
carto$NAs[carto$GEOID %in% IDs15to23] <- 2
carto$NAs[carto$GEOID %in% IDs15] <- 1
############################
# Figure B1: number of NAs #
############################
centroid <- st_centroid(carto)
pnt = st_as_sf(data.frame(x=st_coordinates(centroid)[,1], y=st_coordinates(centroid)[,2]),
coords = c("x", "y"), crs = 4326)
states <- aggregate(carto[, "STATEFP"], by = list(ID = carto$STATEFP),
FUN = unique, dissolve = T)
regions <- aggregate(carto[, "Region"], by = list(ID = carto$Region),
FUN = unique, dissolve = T)
col.NAs <- c("#f0f0f0", "#66c2a5", "#fed976", "#fd8d3c")
val.NAs <- c(0,1,2,3,4)
pal.NAs <- colorBin(col.NAs, domain=carto$NAs, bins=val.NAs)
map <- leaflet(carto) %>% addTiles() %>%
addPolygons(fillColor = ~pal.NAs(NAs), weight = 1,
opacity = 1, color = "black", fillOpacity = 0.7) %>%
addLegend("bottomright", colors=col.NAs,
labels = c("0", "[1, 14]", "[15, 22]", "23"),
title = "Number of NAs", opacity = 1) %>%
addPolylines(data = states, color = "white", opacity = 1, weight = 1) %>%
addPolylines(data = regions, color = "#c2a5cf", opacity = 1, weight = 4)
print(map)
####################################################################
# Contingency table: number of missing observations in each region #
####################################################################
counts <- counts[, -1]
carto$num.NAs <- rowSums(is.na(counts))
cont.table <- with(carto, table(num.NAs, Region))
cont.table.obs <- cont.table*seq(0,23,1)
obs.NAs <- c(colSums(cont.table.obs), sum(is.na(counts)))
obs.total <- c(table(carto$Region)*T, S*T)
obs.noNAs <- obs.total-obs.NAs
Table <- rbind(obs.noNAs, obs.NAs, obs.total)
colnames(Table) <- c("West", "Midwest", "South", "Northeast", "Total")
rownames(Table) <- c("Observed", "NAs", "Total")
print(Table)
################################################################
# Table B1: percentage of missing observations for each region #
################################################################
perc.NAs <- obs.NAs/obs.total*100
perc.noNAs <- obs.noNAs/obs.total*100
perc.total <- obs.total/obs.total*100
TableB1 <- rbind(perc.noNAs, perc.NAs, perc.total)
colnames(TableB1) <- c("West", "Midwest", "South", "Northeast", "Total")
rownames(TableB1) <- c("Observed", "NAs", "Total")
TableB1 <- round(TableB1, digits=4)
print(TableB1)