-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5 - dataprerp - lag.R
235 lines (195 loc) · 7.71 KB
/
5 - dataprerp - lag.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
# Set up environment -----------------------------------------------------------
## Libraries
"lme4" %>=>% libInstall %=>% library(.., char = T)
"ggplot2" %>=>% libInstall %=>% library(.., char = T)
"extrafont" %>=>% libInstall %=>% library(.., char = T)
## Import fonts for plots
if (!"Open Sans" %in% fonts()) {
font_import(
path = "/mnt/c/Users/rknx/AppData/Local/Microsoft/Windows/Fonts/",
pattern = "OpenSans",
prompt = F
)
}
# Import data ------------------------------------------------------------------
## Load saved R objects
"/Data/weather.rda" %=>% paste0(getwd(), ..) %=>% load(.., envir = globalenv())
"/Data/bacteria.rda" %=>% paste0(getwd(), ..) %=>% load(.., envir = globalenv())
"/Data/cfu.rda" %=>% paste0(getwd(), ..) %=>% load(.., envir = globalenv())
field = filter(fieldBacteria, year != "2015")
teField = filter(fieldBacteria, year == "2015")
# Graphically visualize the plot -----------------------------------------------
## Get data
# dfField = with(field, aggregate(presence ~ date, FUN = mean))
# df_weather = weather[, c("date", "Rain.sum")] # to remove 2015
# df = merge(dfField, df_weather, by = "date", all = T)
# df$year = substring(df$date, 1, 4)
# df = df[df$year %in% c("2016", "2017"), ]
## Draw the plot
# ggplot(df, aes(date)) +
# geom_point(aes(y = presence, group = date)) +
# geom_line(aes(y = Rain.sum / 8)) +
# facet_wrap(year ~ ., scales = "free_x") +
# scale_y_continuous(sec.axis = sec_axis(~ . * 8, name = "Rain")) +
# scale_x_date("Date", date_breaks = "2 weeks", date_labels = "%b %d") +
# theme_classic() +
# theme(
# text = element_text(family = "Segoe UI")
# ) %->%
# lagplot %=>%
# # Save the plot
# ggsave(
# filename = paste0(getwd(), "/outputs/lag_rain.png"),
# plot = .., width = 10, height = 5, units = "in", dpi = 300
# )
# Correlation between predictors and clustering --------------------------------
## Clustering function for weather predictors
myClust = function(corMat, k) {
corMat %=>% dist %=>% hclust %=>% cutree(.., k = k) %=>%
lapply(unique(..), x ->> names(..)[.. == x])
}
## Compute correlation matrix
weather[substring(weather$date, 1, 4) != 2015, ] %=>%
..[, sapply(.., is.numeric)] %=>% # Only predictor columns, not date
cor(.., use = "complete.obs") %->%
corMat
## Set clusters
corMat %=>%
lapply(seq_len(sqrt(length(..))), x ->> myClust(.., k = x)) %->%
cluster
# Correlation between presence and weather predictors --------------------------
## Point biserial correlation function (drizopoulos/ltm -> biserial.cor)
biserialCor = function(x, y) {
naRow = complete.cases(x, y) # !imp make na index prior to applying
x = x[naRow]
y = y[naRow]
delMu = mean(x[y == 1]) - mean(x[y == 0])
prob = mean(y == 1)
rho = sd(x) * sqrt((length(x) - 1) / length(x))
delMu * sqrt(prob * (1 - prob)) / rho
}
## Correlation over lag periods
env[1:26] %=>>%
{
..[, sapply(.., is.numeric)] %=>% names %->% cols
merge(field, .., by = "date") %=>%
lapply(cols, y ->> biserialCor(..[[y]], ..[["presence"]])) %=>%
do.call(data.frame, ..) %=>%
setNames(.., cols) %=>% # from prerequisites, change colnames
round(.., 2)
} %=>%
do.call(rbind, ..) %->%
corTable
## Function for finding best lag period
bestLag = function(corTable, k = length(corTable)) {
cluster[[k]] %=>% # get clusters by number of clusters
lapply(.., y ->> corTable[, y, drop = F] %=>% abs %=>% rowMeans) %=>%
# lapply(.., y ->> sample(y, 1) %=>% corTable[, .., drop = F]) %=>%
# lapply(.., y ->> corTable[, y[1], drop = F] %=>% abs) %=>%
do.call(data.frame, ..) %=>%
abs %=>%
rowMeans %=>%
which.max %>=>%
cat(paste0("For k = ", k, ", best lag period is ", .. - 1, " days.\n"))
}
## Calculate best lag over various numbers of predictors
3:8 %>=>% # cluster lengths to evaluate over
set.seed(92) %=>%
sapply(.., x ->> bestLag(corTable, k = x)) %=>%
mathMode %>=>% # calculate mode, see prerequisites
cat("Overall, best lag period is", .. - 1, "days.\n") %->%
bLagCor
# Regression method ------------------------------------------------------------
## Prepare list of formulas
formulae = unlist(lapply(3:5, x ->> {
cluster[[x]] %=>% expand.grid %|>% paste(unlist(..), collapse = " + ")
}))
## Execute glm using formulae for each lag
fitTable = lapply(env[1:7], x ->> {
merge(fieldCFU, x, by = "date") %=>% na.omit %->% data
data %<=>% mutate(.., resid = glm(lCFU ~ week + dis) %=>% residuals)
pb = txtProgressBar(min = 0, max = length(formulae), style = 3)
lapply(seq_along(formulae), y ->> {
setTxtProgressBar(pb, y)
glm(as.formula(paste("resid ~", formulae[y])), data = data) %=>%
data.frame(Formula = formulae[y], BIC = round(BIC(..), 2))
}) %=>%
do.call(rbind.data.frame, ..) %>=>%
close(pb) %=>%
arrange(.., BIC)
})
## save(fitTable, file = paste0(getwd(), "/Data/lagFitTableResid.rda"))
## Evaluate fittings by lag periods
fitTable %=>>%
data.frame(
minBIC = min(..$BIC),
meanBIC = mean(..$BIC),
medianBIC = median(..$BIC),
formula = ..$Formula[which.min(..$BIC)]
) %=>%
do.call(rbind, ..) %->%
bestFit
## Best lag period
which.min(bestFit$meanBIC) %>=>%
cat(
"The best lag is", .. - 1, "days using formula: presence ~ ",
as.character(bestFit$formula[..]), "\n"
) %->%
bLagFit
## Plot the fits ##check temp
cols = c(
"#3f8acc", "#ac5e52", "#37bdd1", "#db449c", "#55b948", "#eb4545", "#9c64ca"
)
fills = c(
"#77afe0", "#d38f84", "#7fd8e6", "#ee8ac4", "#8ed385", "#f09090", "#bf96e0"
)
fitTable %=>% seq_along %=>>%
data.frame(Lag = as.character(.. - 1), BIC = fitTable[[..]]$BIC) %=>%
do.call(rbind, ..) %->%
plotOut %=>%
(ggplot(.., aes(Lag, BIC, group = Lag, col = Lag, fill = Lag)) +
stat_boxplot(geom = "errorbar", width = 0.2) +
geom_boxplot(width = 0.4) +
stat_summary(
geom = "line", fun = "mean", group = "1",
size = 0.8, col = "grey30", linetype = "dashed"
) +
stat_summary(
geom = "point", fun = "mean",
pch = 23, size = 3, col = "grey10", fill = cols
) +
scale_color_manual(values = cols, guide = F) +
scale_fill_manual(values = fills, guide = F) +
scale_x_discrete("Lag (days)") +
# geom_hline(yintercept = min(bestFit$medianBIC), linetype = "dashed") +
theme_classic()) %->%
fitPlot %=>%
ggsave(
filename = paste0(getwd(), "/Output/lagFit2.png"),
plot = .., width = 6, height = 3.5, units = "in", dpi = 300
)
adjustcolor()
# Merge best lag period --------------------------------------------------------
## Select preferred approach
if (blagFit == blagCor) {
bLag = bLagFit
} else {
cat("Best lag by correalation and regression vary. Select one to proceed.")
userChoice = select.list(c("Correlation", "Regression"))
bLag = ifelse(userChoice == "Correlation", bLagCor, bLagFit)
}
## Combine field and weather data
dataBinom = merge(field, env[[bLag]], all.x = TRUE)
tedataBinom = merge(teField, env[[bLag]], all.x = TRUE)
# Cleaning up ------------------------------------------------------------------
## Save dataframes as R object
c("dataBinom", "tedataBinom", "cluster") %=>%
save(list = .., file = paste0(getwd(), "/Data/data.rda"))
## Remove old dataframes
rm(
"fieldBacteria", "weather", "weatherScale", "env", "field", "teField",
"corTable", "corMat", "formulae", "fitTable", "bestFit",
"plotOut", "fitPlot"
)
## Load saved .rda
"/Data/data.rda" %=>% paste0(getwd(), ..) %=>% load(.., envir = globalenv())