-
Notifications
You must be signed in to change notification settings - Fork 1
/
MassAnalysis.R
246 lines (182 loc) · 10.8 KB
/
MassAnalysis.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#Sarah V. Leavitt
#Boston University Dissertation
#Paper 2
################################################################################
# This program estimates transmission probabilities and serial interval, and
# reproductive number for the Mass DPH data using the NB transmission method.
# The data are cleaned in MassPrep.R.
# NOTE: Program takes around 5 hours to run completely (with the original dataset).
# Best to run using MassQsub.qsub and run on a shared commuting cluster
################################################################################
#setwd("~/Boston University/Dissertation/nbPaper2")
setwd("/project/sv-thesis/nbPaper2")
rm(list = ls())
######################### Estimating Probabilities ############################
library(dplyr)
library(tidyr)
library(devtools)
load_all("../nbTransmission")
#Reading in cleaned datasets from MassPrep.R
set.seed(103020)
massInd <- readRDS("../Datasets/MassInd.rds")
massPair <- readRDS("../Datasets/MassPair.rds")
#How many pairs are different lineages (52%)
sum(massPair$Lineage == "Different", na.rm = TRUE)
sum(massPair$Lineage == "Different", na.rm = TRUE) / nrow(massPair)
#Creating an ordered dataset that also removes pairs with different lineages
orderedMass <- (massPair
%>% filter(CombinedDiff >= 0, Lineage == "Same" | is.na(Lineage))
%>% select(EdgeID, StudyID.1, StudyID.2, ContactGroup, Lineage.1, Lineage.2,
CombinedDt.1, CombinedDt.2, RecentArrival1.1, RecentArrival1.2,
RecentArrival2.1, RecentArrival2.2, County, Sex, Age, Spoligotype,
MIRUDiff, MIRUDiffG, GENType, PCRType, Lineage, CountryOfBirth,
Smear, SharedResG, AnyImmunoSup, TimeCat, CombinedDiff, CombinedDiffY,
CombinedDiffYM, ContactTrain)
)
print(table(orderedMass$ContactTrain, useNA = "always"))
print(prop.table(table(orderedMass$ContactTrain, useNA = "always")))
#Looking at all contact pairs
contactPairs <- (orderedMass
%>% filter(ContactGroup == TRUE)
%>% select(EdgeID, RecentArrival2.1, RecentArrival2.2, CombinedDt.1, CombinedDt.2,
CombinedDiff, Lineage.1, Lineage.2, Spoligotype, GENType, PCRType,
MIRUDiffG, SharedResG, ContactTrain)
)
#### Estimating Probabilities ####
## NOTE BEGIN RUNNING HERE IF YOU ARE USING THE SIMULATED DATASET "orderedMassSim.rds" ##
# orderedMass <- readRDS("orderedMassSim.rds")
#Estimating the probabilities with time difference
covariates <- c("Sex", "Age", "CountryOfBirth", "County", "Smear", "AnyImmunoSup",
"SharedResG", "GENType", "TimeCat")
resMass <- nbProbabilities(orderedPair = orderedMass, indIDVar = "StudyID", pairIDVar = "EdgeID",
goldStdVar = "ContactTrain", covariates = covariates,
label = "ContactTime", l = 0.5, n = 10, m = 1, nReps = 50,
progressBar = FALSE)
resMassCov <- (orderedMass
%>% full_join(resMass$probabilities, by = "EdgeID")
#Setting probabilities to 0 if infectee was a recent immigrant but not if it was a training link
%>% mutate(pScaledI1 = ifelse(!is.na(RecentArrival1.2) &
RecentArrival1.2 == TRUE &
(is.na(ContactTrain) | ContactTrain != TRUE), 0, pScaled),
pScaledI2 = ifelse(!is.na(RecentArrival2.2) &
RecentArrival2.2 == TRUE &
(is.na(ContactTrain) | ContactTrain != TRUE), 0, pScaled))
)
resMassCoeff <- resMass$estimates
print("Finished estimating probabilities with time difference")
#Estimating probabilities without time difference
covariates2 <- c("Sex", "Age", "CountryOfBirth", "County", "Smear", "AnyImmunoSup",
"SharedResG", "GENType")
resMass2 <- nbProbabilities(orderedPair = orderedMass, indIDVar = "StudyID", pairID = "EdgeID",
goldStdVar = "ContactTrain", covariates = covariates2,
label = "ContactNoTime", l = 0.5, n = 10, m = 1, nReps = 50,
progressBar = FALSE)
resMassCov2 <- (orderedMass
%>% full_join(resMass2$probabilities, by = c("EdgeID"))
#Setting probabilities to 0 if infectee was a recent immigrant but not if it was a training link
%>% mutate(pScaledI1 = ifelse(!is.na(RecentArrival1.2) &
RecentArrival1.2 == TRUE &
(is.na(ContactTrain) | ContactTrain != TRUE), 0, pScaled),
pScaledI2 = ifelse(!is.na(RecentArrival2.2) &
RecentArrival2.2 == TRUE &
(is.na(ContactTrain) | ContactTrain != TRUE), 0, pScaled))
)
resMassCoeff2 <- resMass2$estimates
print("Finished estimating probabilities without time difference")
#Saving the results
saveRDS(resMassCov, "../Datasets/MassResults.rds")
saveRDS(resMassCov2, "../Datasets/MassResults_NoTime.rds")
###################### Serial Interval ########################
siHC1 <- estimateSI(df = resMassCov2, indIDVar = "StudyID",
timeDiffVar = "CombinedDiffYM", pVar = "pScaledI2",
clustMethod = "hc_absolute", cutoffs = seq(0.025, 0.25, 0.025),
initialPars = c(1.2, 2), shift = 0, bootSamples = 1000, progressBar = FALSE)
siHC1$label <- "HC: Excluding 1-month co-prevalent cases"
print("HC: Excluding 1-month co-prevalent cases")
siHC2 <- estimateSI(df = resMassCov2, indIDVar = "StudyID",
timeDiffVar = "CombinedDiffYM", pVar = "pScaledI2",
clustMethod = "hc_absolute", cutoffs = seq(0.025, 0.25, 0.025),
initialPars = c(1.2, 2), shift = 1/12, bootSamples = 1000, progressBar = FALSE)
siHC2$label <- "HC: Excluding 2-month co-prevalent cases"
print("HC: Excluding 2-month co-prevalent cases")
siHC3 <- estimateSI(df = resMassCov2, indIDVar = "StudyID",
timeDiffVar = "CombinedDiffYM", pVar = "pScaledI2",
clustMethod = "hc_absolute", cutoffs = seq(0.025, 0.25, 0.025),
initialPars = c(1.2, 2), shift = 2/12, bootSamples = 1000, progressBar = FALSE)
siHC3$label <- "HC: Excluding 3-month co-prevalent cases"
print("HC: Excluding 3-month co-prevalent cases")
siKD1 <- estimateSI(df = resMassCov2, indIDVar = "StudyID",
timeDiffVar = "CombinedDiffYM", pVar = "pScaledI2",
clustMethod = "kd", cutoffs = seq(0.01, 0.1, 0.01),
initialPars = c(1.2, 2), shift = 0, bootSamples = 1000, progressBar = FALSE)
siKD1$label <- "KD: Excluding 1-month co-prevalent cases"
print("KD: Excluding 1-month co-prevalent cases")
siKD2 <- estimateSI(df = resMassCov2, indIDVar = "StudyID",
timeDiffVar = "CombinedDiffYM", pVar = "pScaledI2",
clustMethod = "kd", cutoffs = seq(0.01, 0.1, 0.01),
initialPars = c(1.2, 2), shift = 1/12, bootSamples = 1000, progressBar = FALSE)
siKD2$label <- "KD: Excluding 2-month co-prevalent cases"
print("KD: Excluding 2-month co-prevalent cases")
siKD3 <- estimateSI(df = resMassCov2, indIDVar = "StudyID",
timeDiffVar = "CombinedDiffYM", pVar = "pScaledI2",
clustMethod = "kd", cutoffs = seq(0.01, 0.1, 0.01),
initialPars = c(1.2, 2), shift = 2/12, bootSamples = 1000, progressBar = FALSE)
siKD3$label <- "KD: Excluding 3-month co-prevalent cases"
print("KD: Excluding 3-month co-prevalent cases")
#Sensitivity analysis for recent immigration definition
siHCI1 <- estimateSI(df = resMassCov2, indIDVar = "StudyID",
timeDiffVar = "CombinedDiffYM", pVar = "pScaledI1",
clustMethod = "hc_absolute", cutoffs = seq(0.025, 0.25, 0.025),
initialPars = c(1.2, 2), shift = 0, bootSamples = 1000, progressBar = FALSE)
siHCI1$label <- "HC: Recent Arrival = 1 Year"
print("HC: Recent Arrival = 1 Year")
siKDI1 <- estimateSI(df = resMassCov2, indIDVar = "StudyID",
timeDiffVar = "CombinedDiffYM", pVar = "pScaledI1",
clustMethod = "kd", cutoffs = seq(0.01, 0.1, 0.01),
initialPars = c(1.2, 2), shift = 0, bootSamples = 1000, progressBar = FALSE)
siKDI1$label <- "KD: Recent Arrival = 1 Year"
print("KD: Recent Arrival = 1 Year")
siAll <- bind_rows(siHC1, siHC2, siHC3, siKD1, siKD2, siKD3, siHCI1, siKDI1)
#Saving the serial interval dataset
saveRDS(siAll, "../Datasets/MassSI.rds")
####################### Reproductive Number ###########################
#Initially calculating reproductive number to decide cut points
rInitial <- estimateR(df = resMassCov, dateVar = "CombinedDt", indIDVar = "StudyID",
pVar = "pScaledI2", timeFrame = "months")
rInitial$RtAvgDf
rt <- rInitial$RtDf
#Cutting the outbreak
totalTime <- max(rt$timeRank) - min(rt$timeRank)
monthCut1 <- ceiling(0.1 * totalTime)
monthCut2 <- ceiling(0.8 * totalTime)
#Plotting where to cut
# ggplot(data = rt) +
# geom_line(aes(x = timeRank, y = Rt)) +
# scale_y_continuous(name = "Rt") +
# geom_vline(aes(xintercept = monthCut1), linetype = 2, size = 0.7, col = "blue") +
# geom_vline(aes(xintercept = monthCut2), linetype = 2, size = 0.7, col = "blue")
#Calculating the reproductive number using 1 year definition for recent immigration
rFinal1 <- estimateR(resMassCov, dateVar = "CombinedDt", indIDVar = "StudyID",
pVar = "pScaledI1", timeFrame = "months",
rangeForAvg = c(monthCut1, monthCut2),
bootSamples = 1000, alpha = 0.05, progressBar = FALSE)
rFinal1$RiDf$label <- "Recent Arrival = 1 Year"
rFinal1$RtDf$label <- "Recent Arrival = 1 Year"
rFinal1$RtAvgDf$label <- "Recent Arrival = 1 Year"
print("Recent Arrival = 1 Year")
#Calculating the reproductive number using 2 year definition for recent immigration
rFinal2 <- estimateR(resMassCov, dateVar = "CombinedDt", indIDVar = "StudyID",
pVar = "pScaledI2", timeFrame = "months",
rangeForAvg = c(monthCut1, monthCut2),
bootSamples = 1000, alpha = 0.05, progressBar = FALSE)
rFinal2$RiDf$label <- "Recent Arrival = 2 Years"
rFinal2$RtDf$label <- "Recent Arrival = 2 Years"
rFinal2$RtAvgDf$label <- "Recent Arrival = 2 Years"
print("Recent Arrival = 2 Years")
RiData <- bind_rows(rFinal1$RiDf, rFinal2$RiDf)
RtData <- bind_rows(rFinal1$RtDf, rFinal2$RtDf)
RtAvg <- bind_rows(rFinal1$RtAvgDf, rFinal2$RtAvgDf)
#Saving the confidence interval datasets
saveRDS(RiData, "../Datasets/MassRi.rds")
saveRDS(RtData, "../Datasets/MassRtCI.rds")
saveRDS(RtAvg, "../Datasets/MassRtAvgCI.rds")