-
Notifications
You must be signed in to change notification settings - Fork 0
/
LatCovid3.R
444 lines (332 loc) · 16 KB
/
LatCovid3.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
## START HERE
### Reading data ###############
Y <-read.csv(file="DailyDeaths_05_20_2020.csv", header=TRUE)
### Initial check up ###########
# summary(Y)
#### Required libraries ###################
library(psd)
library(TSEntropies)
library(fractal)
#######################################################
######## Section II - Daily Death COVID-19 Dynamics ###
#######################################################
#### Calculating the autocorrelation function #########
### Generating time series ####
MexTS=Y[162,115:186]
# MexTS
MexFull=Y[162,68:186]
# MexFull
MXT=as.vector(MexTS)
MXF=as.vector(MexFull)
USTS=Y[1,105:186]
#USTS
USFull=Y[1,68:186]
#USFull
UST=as.vector(USTS)
USF=as.vector(USFull)
CNTS=Y[11,68:182]
#CNTS
CNFull=Y[11,68:186]
#CNFull
CNT=as.vector(CNTS)
CNF=as.vector(CNFull)
BRTS=Y[70,122:186]
#BRTS
BRFull=Y[70,68:186]
#BRFull
BRT=as.vector(BRTS)
BRF=as.vector(BRFull)
INTS = Y[163,133:186]
#INTS
INFull=Y[163,68:186]
#INFull
INT=as.vector(INTS)
INF=as.vector(INFull)
UKTS=Y[2,114:186]
#UKTS
UKFull=Y[2,68:186]
#UKFull
UKT=as.vector(UKTS)
UKF=as.vector(UKFull)
BETS=Y[6,121:186]
#BETS
BEFull=Y[6,68:186]
#BEFull
BET=as.vector(BETS)
BEF=as.vector(BEFull)
##### Calculating the partial autocorrelation function ###############################################
######################################################################################################
# In time series analysis, the partial autocorrelation function (PACF)
# gives the partial correlation of a stationary time series with its own lagged values,
# regressed the values of the time series at all shorter lags.
# It contrasts with the autocorrelation function, which does not control for other lags.
# It is a partial correlation, hence it measures the degree of association between two
# random variables, with the effect of a set of controlling random variables removed.
# If we are interested in finding to what extent there is a numerical relationship between
# two variables of interest, using their correlation coefficient will give misleading results
# if there is another, confounding, variable that is numerically related to both variables of interest.
########################################################################################################
PACFMX = acf(MXT, lag.max = 71,
type = "partial",
plot = FALSE)
#PACFMX
plot(PACFMX, main="Partial autocorrelation daily deaths Mexico")
#### Full series (zero-biased) -> **F ######
PACFMXF = acf(MXF, lag.max = 118,
type = "partial",
plot = FALSE)
#PACFMXF
plot(PACFMXF, main="Partial autocorrelation daily deaths Mexico zero-biased")
PACFUS = acf(UST, lag.max = 81,
type = "partial",
plot = FALSE)
#PACFUS
plot(PACFUS, main="Partial autocorrelation daily deaths US")
PACFUSF = acf(USF, lag.max = 118,
type = "partial",
plot = FALSE)
#PACFUSF
plot(PACFUSF,main="Partial autocorrelation daily deaths US zero-biased")
PACFCN = acf(CNT, lag.max = 114,
type = "partial",
plot = FALSE)
#PACFCN
plot(PACFCN, main="Partial autocorrelation daily deaths China")
PACFCNF = acf(CNF, lag.max = 118,
type = "partial",
plot = FALSE)
#PACFCNF
plot(PACFCNF, main="Partial autocorrelation daily deaths China zero-biased")
PACFBR = acf(BRT, lag.max = 64,
type = "partial",
plot = FALSE)
#PACFBR
plot(PACFBR, main="Partial autocorrelation daily deaths Brazil")
PACFBRF = acf(BRF, lag.max = 118,
type = "partial",
plot = FALSE)
#PACFBRF
plot(PACFBRF, main="Partial autocorrelation daily deaths Brazil zero-biased")
PACFIN = acf(INT, lag.max = 53,
type = "partial",
plot = FALSE)
#PACFIN
plot(PACFIN, main="Partial autocorrelation daily deaths India")
PACFINF = acf(INF, lag.max = 118,
type = "partial",
plot = FALSE)
#PACFINF
plot(PACFINF, main="Partial autocorrelation daily deaths India zero-biased")
PACFUK = acf(UKT, lag.max = 72,
type = "partial",
plot = FALSE)
#PACFUK
plot(PACFUK, main="Partial autocorrelation daily deaths UK")
PACFUKF = acf(UKF, lag.max = 118,
type = "partial",
plot = FALSE)
#PACFUKF
plot(PACFUKF, main="Partial autocorrelation daily deaths UK zero-biased")
PACFBE = acf(BET, lag.max = 65,
type = "partial",
plot = FALSE)
#PACFBE
plot(PACFBE, main="Partial autocorrelation daily deaths Belgium")
PACFBEF = acf(BEF, lag.max = 118,
type = "partial",
plot = FALSE)
#PACFBEF
plot(PACFBEF, main="Partial autocorrelation daily deaths Belgium zero-biased")
############################################################################################
# Calculation of the power spectral density to look up for periodicities in the time series #
#############################################################################################
MXTT = as.numeric(MXT)
USTT = as.numeric(UST)
CNTT= as.numeric(CNT)
BRTT= as.numeric(BRT)
INTT=as.numeric(INT)
UKTT=as.numeric(UKT)
BETT=as.numeric(BET)
MXTF = as.numeric(MXF)
USTF = as.numeric(USF)
CNTF= as.numeric(CNF)
BRTF= as.numeric(BRF)
INTF=as.numeric(INF)
UKTF=as.numeric(UKF)
BETF=as.numeric(BEF)
##################################################################################
### Scatterplots for daily death Epidemics #######################################
##################################################################################
plot(MXTT, main="Daily COVID-19 deaths Mexico")
plot(MXTF, main="Daily COVID-19 deaths Mexico zero-biased")
plot(USTT, main="Daily COVID-19 deaths US")
plot(USTF, main="Daily COVID-19 deaths US zero-biased")
plot(CNTT, main="Daily COVID-19 deaths China")
plot(CNTF, main="Daily COVID-19 deaths China zero-biased")
plot(BRTT, main="Daily COVID-19 deaths Brazil")
plot(BRTF, main="Daily COVID-19 deaths Brazil zero-biased")
plot(INTT, main="Daily COVID-19 deaths India")
plot(INTF, main="Daily COVID-19 deaths India zero-biased")
plot(UKTT, main="Daily COVID-19 deaths UK")
plot(UKTF, main="Daily COVID-19 deaths UK zero-biased")
plot(BETT, main="Daily COVID-19 deaths Belgium")
plot(BETF, main="Daily COVID-19 deaths Belgium zero-biased")
##### Periodogram and autoregressive power spectral densities (linear scale) ####
spectrum(MXTT, log="no",main="Power Spectral Density daily deaths Mexico")
spectrum(MXTT, method="ar", log="no", main="Power Spectral Density daily deaths Mexico autoregressive")
spectrum(MXTF, log="no",main="Power Spectral Density daily deaths Mexico zero-biased")
spectrum(MXTF, method="ar", log="no",main="Power Spectral Density daily deaths Mexico autoregressive zero-biased")
spectrum(USTT, log="no",main="Power Spectral Density daily deaths US")
spectrum(USTT, method="ar", log="no",main="Power Spectral Density daily deaths US autoregressive")
spectrum(USTF, log="no",main="Power Spectral Density daily deaths US zero-biased")
spectrum(USTF, method="ar", log="no",main="Power Spectral Density daily deaths US autoregressive zero-biased")
spectrum(CNTT, log="no",main="Power Spectral Density daily deaths China")
spectrum(CNTT, method="ar", log="no",main="Power Spectral Density daily deaths China autoregressive")
spectrum(CNTF, log="no",main="Power Spectral Density daily deaths China zero-biased")
spectrum(CNTF, method="ar", log="no",main="Power Spectral Density daily deaths China autoregressive zero-biased")
spectrum(BRTT, log="no",main="Power Spectral Density daily deaths Brazil")
spectrum(BRTT, method="ar", log="no",main="Power Spectral Density daily deaths Brazil autoregressive")
spectrum(BRTF, log="no" ,main="Power Spectral Density daily deaths Brazil zero-biased")
spectrum(BRTF, method="ar", log="no",main="Power Spectral Density daily deaths Brazil autoregressive zero-biased")
spectrum(INTT, log="no",main="Power Spectral Density daily deaths India")
spectrum(INTT, method="ar", log="no",main="Power Spectral Density daily deaths India autoregressive")
spectrum(INTF, log="no",main="Power Spectral Density daily deaths India zero-biased")
spectrum(INTF, method="ar", log="no",main="Power Spectral Density daily deaths India autoregressive zero-biased")
spectrum(UKTT, log="no",main="Power Spectral Density daily deaths UK")
spectrum(UKTT, method="ar", log="no",main="Power Spectral Density daily deaths UK autoregressive")
spectrum(UKTF, log="no",main="Power Spectral Density daily deaths UK zero-biased")
spectrum(UKTF, method="ar", log="no",main="Power Spectral Density daily deaths UK autoregressive zero-biased")
spectrum(BETT, log="no",main="Power Spectral Density daily deaths Belgium")
spectrum(BETT, method="ar", log="no",main="Power Spectral Density daily deaths Belgium autoregressive")
spectrum(BETF, log="no",main="Power Spectral Density daily deaths Belgium zero-biased")
spectrum(BETF, method="ar", log="no",main="Power Spectral Density daily deaths Belgium autoregressive zero-biased")
##### Tappered power spectral density ##################################################################################
# Adaptive sine multitaper power spectral density estimation ##########################################################
# This is the primary function to be used in this package: it returns power spectral density estimates of a univariate #
# timeseries, with an optimal number of tapers at each frequency based on iterative reweighted spectral derivatives.####
########################################################################################################################
pspectrum(MXTT,plot=TRUE,main="Power Spectral Density daily deaths tappered Mexico")
pspectrum(USTT,plot=TRUE,main="Power Spectral Density daily deaths tappered US")
pspectrum(CNTT,plot=TRUE,main="Power Spectral Density daily deaths tappered China")
pspectrum(BRTT,plot=TRUE,main="Power Spectral Density daily deaths tappered Brazil")
pspectrum(INTT,plot=TRUE,main="Power Spectral Density daily deaths tappered India")
pspectrum(UKTT,plot=TRUE,main="Power Spectral Density daily deaths tappered UK")
pspectrum(BETT,plot=TRUE,main="Power Spectral Density daily deaths tappered Belgium")
pspectrum(MXTF,plot=TRUE,main="Power Spectral Density daily deaths tappered Mexico zero-biased")
pspectrum(USTF,plot=TRUE,main="Power Spectral Density daily deaths tappered US zero-biased")
pspectrum(CNTF,plot=TRUE,main="Power Spectral Density daily deaths tappered China zero-biased")
pspectrum(BRTF,plot=TRUE,main="Power Spectral Density daily deaths tappered Brazil zero-biased")
pspectrum(INTF,plot=TRUE,main="Power Spectral Density daily deaths tappered India zero-biased")
pspectrum(UKTF,plot=TRUE,main="Power Spectral Density daily deaths tappered UK zero-biased")
pspectrum(BETF,plot=TRUE,main="Power Spectral Density daily deaths tappered Belgium zero-biased")
###### Approximate Entropy ###################################################################
# Approximate entropy (ApEn) is a technique used to quantify the amount of regularity and ###
# the unpredictability of fluctuations over time-series data. ###
# Regularity was originally measured by exact regularity statistics, which has mainly ###
# centered on various entropy measures. However, accurate entropy calculation requires vast ##
# amounts of data, and the results will be greatly influenced by system noise, ###
# therefore it is not practical to apply these methods to experimental data. ###
# ApEn was developed by Steve M. Pincus to handle these limitations by modifying an ###
# exact regularity statistic, Kolmogorov–Sinai entropy. ###
##############################################################################################
MX_entropy=FastApEn_R(MXTT, dim = 2, lag = 1, r = 0.15 * sd(MXTT))
MX_entropy
MX_entropy_F=FastApEn_R(MXTF, dim = 2, lag = 1, r = 0.15 * sd(MXTT))
MX_entropy_F
US_entropy=FastApEn_R(USTT, dim = 2, lag = 1, r = 0.15 * sd(USTT))
US_entropy
US_entropy_F=FastApEn_R(USTF, dim = 2, lag = 1, r = 0.15 * sd(USTT))
US_entropy_F
CN_entropy=FastApEn_R(CNTT, dim = 2, lag = 1, r = 0.15 * sd(CNTT))
CN_entropy
CN_entropy_F=FastApEn_R(CNTF, dim = 2, lag = 1, r = 0.15 * sd(CNTT))
CN_entropy_F
BR_entropy=FastApEn_R(BRTT, dim = 2, lag = 1, r = 0.15 * sd(BRTT))
BR_entropy
BR_entropy_F=FastApEn_R(BRTF, dim = 2, lag = 1, r = 0.15 * sd(BRTT))
BR_entropy_F
IN_entropy=FastApEn_R(INTT, dim = 2, lag = 1, r = 0.15 * sd(INTT))
IN_entropy
IN_entropy_F=FastApEn_R(INTF, dim = 2, lag = 1, r = 0.15 * sd(INTT))
IN_entropy_F
UK_entropy=FastApEn_R(UKTT, dim = 2, lag = 1, r = 0.15 * sd(UKTT))
UK_entropy
UK_entropy_F=FastApEn_R(UKTF, dim = 2, lag = 1, r = 0.15 * sd(UKTT))
UK_entropy_F
BE_entropy=FastApEn_R(BETT, dim = 2, lag = 1, r = 0.15 * sd(BETT))
BE_entropy
BE_entropy_F=FastApEn_R(BETF, dim = 2, lag = 1, r = 0.15 * sd(BETT))
BE_entropy_F
###### SampleEntropy (no self-similarities accounted) #############
MX_fast_entropy=FastSampEn(MXTT, dim = 2, lag = 1, r = 0.15 * sd(MXTT))
MX_fast_entropy
MX_fast_entropy_F=FastSampEn(MXTF, dim = 2, lag = 1, r = 0.15 * sd(MXTT))
MX_fast_entropy_F
US_fast_entropy=FastSampEn(USTT, dim = 2, lag = 1, r = 0.15 * sd(USTT))
US_fast_entropy
US_fast_entropy_F=FastSampEn(USTF, dim = 2, lag = 1, r = 0.15 * sd(USTT))
US_fast_entropy_F
CN_fast_entropy=FastSampEn(CNTT, dim = 2, lag = 1, r = 0.15 * sd(CNTT))
CN_fast_entropy
CN_fast_entropy_F=FastSampEn(CNTF, dim = 2, lag = 1, r = 0.15 * sd(CNTT))
CN_fast_entropy_F
BR_fast_entropy=FastSampEn(BRTT, dim = 2, lag = 1, r = 0.15 * sd(BRTT))
BR_fast_entropy
BR_fast_entropy_F=FastSampEn(BRTF, dim = 2, lag = 1, r = 0.15 * sd(BRTT))
BR_fast_entropy_F
IN_fast_entropy=FastSampEn(INTT, dim = 2, lag = 1, r = 0.15 * sd(INTT))
IN_fast_entropy
IN_fast_entropy_F=FastSampEn(INTF, dim = 2, lag = 1, r = 0.15 * sd(INTT))
IN_fast_entropy_F
UK_fast_entropy=FastSampEn(UKTT, dim = 2, lag = 1, r = 0.15 * sd(UKTT))
UK_fast_entropy
UK_fast_entropy_F=FastSampEn(UKTF, dim = 2, lag = 1, r = 0.15 * sd(UKTT))
UK_fast_entropy_F
BE_fast_entropy=FastSampEn(BETT, dim = 2, lag = 1, r = 0.15 * sd(BETT))
BE_fast_entropy
BE_fast_entropy_F=FastSampEn(BETF, dim = 2, lag = 1, r = 0.15 * sd(BETT))
BE_fast_entropy_F
##################################################################################
#### Detrended Fluctuations Analysis Daily Deaths Time Series ####################
DFA(MXTT, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(MXTT)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(MXTF, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(MXTF)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(USTT, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(USTT)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(USTF, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(USTF)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(CNTT, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(CNTT)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(CNTF, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(CNTF)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(BRTT, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(BRTT)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(BRTF, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(BRTF)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(INTT, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(INTT)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(INTF, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(INTF)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(UKTT, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(UKTT)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(UKTF, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(UKTF)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(BETT, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(BETT)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)
DFA(BETF, detrend="poly1", sum.order=0, overlap=0,
scale.max=trunc(length(BETF)/2), scale.min=NULL,
scale.ratio=2, verbose=FALSE)