-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.output.R
138 lines (110 loc) · 4.3 KB
/
11.output.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
################################################################################
# Updated version of the R code for the analysis in:
#
# "Scortichini M, Schneider Dos Santos R, De' Donato F, De Sario M,
# Michelozzi P, Davoli M, Masselot P, Sera F, Gasparrini A.
# Excess mortality during the COVID-19 outbreak in Italy: a two-stage
# interrupted time-series analysis.
# International Journal of Epidemiology. 2020. DOI: 10.1093/ije/dyaa169."
# http://www.ag-myresearch.com/italyCOVIDdeath.html
#
# Update: 21 October 2020
# * an updated version of this code, compatible with future versions of the
# software, is available at:
# https://github.com/gasparrini/ItalyCOVIDdeath
################################################################################
################################################################################
# OUTPUT
################################################################################
################################################################################
# BY PROVINCE
# CREATE LIST OF DATASET
outprovlist <- lapply(seq(dim(excprov)[2]), function(i) {
# COMPUTE TOTAL AND EXCESS (N AND %)
exc <- excprov[,i,,,]
tot <- totprov[,i,,]
excper <- exc/(-exc+c(tot))*100
excper[is.na(excper)] <- 0
# DEFINE TABLE
dim <- dim(excprov)[-c(2,5)]
dimnames <- dimnames(excprov)[-c(2,5)]
tab <- data.frame(Province=rep(dimnames[[1]], prod(dim[-1])),
Region=rep(labreg[seqregprov], prod(dim[-1])),
Area=rep(areareg[seqregprov], prod(dim[-1])),
Sex=rep(rep(dimnames[[2]], dim[3]), each=dim[1]),
Age=rep(dimnames[[3]], each=prod(dim[-3])))
# ADD ESTIMATES (NB: EXPLOIT ORDER OF ARRAYS)
tab$Deaths <- round(c(tot))
tabest <- matrix(c(round(c(exc)), round(c(excper),1)),nrow=nrow(tab))
colnames(tabest) <- c(t(outer(c("Excess","ExcessPer"),
c("","95eCIlow","95eCIhigh"),paste0)))
tab <- cbind(tab,tabest)
# ORDER
for(i in c(1,4,5)) tab[,i] <- factor(tab[,i], levels=unique(tab[,i]))
tab <- arrange(tab, Province, Sex, Age)
# RETURN
tab
})
# NAMES AND WRITE
names(outprovlist) <- dimnames(excprov)[[2]]
write.xlsx(outprovlist, file = "output/byprovince.xlsx")
################################################################################
# BY REGION
# CREATE LIST OF DATASET
outreglist <- lapply(seq(dim(excreg)[2]), function(i) {
# COMPUTE TOTAL AND EXCESS (N AND %)
exc <- excreg[,i,,,]
tot <- totreg[,i,,]
excper <- exc/(-exc+c(tot))*100
excper[is.na(excper)] <- 0
# DEFINE TABLE
dim <- dim(excreg)[-c(2,5)]
dimnames <- dimnames(excreg)[-c(2,5)]
tab <- data.frame(Region=rep(dimnames[[1]], prod(dim[-1])),
Area=rep(areareg, prod(dim[-1])),
Sex=rep(rep(dimnames[[2]], dim[3]), each=dim[1]),
Age=rep(dimnames[[3]], each=prod(dim[-3])))
# ADD ESTIMATES (NB: EXPLOIT ORDER OF ARRAYS)
tab$Deaths <- round(c(tot))
tabest <- matrix(c(round(c(exc)), round(c(excper), 1)), nrow=nrow(tab))
colnames(tabest) <- c(t(outer(c("Excess","ExcessPer"),
c("","95eCIlow","95eCIhigh"), paste0)))
tab <- cbind(tab,tabest)
# ORDER
for(i in c(1,3,4)) tab[,i] <- factor(tab[,i], levels=unique(tab[,i]))
tab <- arrange(tab, Region, Sex, Age)
# RETURN
tab
})
# NAMES AND WRITE
names(outreglist) <- dimnames(excreg)[[2]]
write.xlsx(outreglist, file = "output/byregion.xlsx")
################################################################################
# ITALY
# CREATE LIST OF DATASET
outitalylist <- lapply(seq(dim(excitaly)[1]), function(i) {
# COMPUTE TOTAL AND EXCESS (N AND %)
exc <- excitaly[i,,,]
tot <- totitaly[i,,]
excper <- exc/(-exc+c(tot))*100
excper[is.na(excper)] <- 0
# DEFINE TABLE
dim <- dim(excitaly)[-c(1,4)]
dimnames <- dimnames(excitaly)[-c(1,4)]
tab <- data.frame(Sex=rep(dimnames[[1]], dim[2]),
Age=rep(dimnames[[2]], each=dim[1]))
# ADD ESTIMATES (NB: EXPLOIT ORDER OF ARRAYS)
tab$Deaths <- round(c(tot))
tabest <- matrix(c(round(c(exc)), round(c(excper), 1)), nrow=nrow(tab))
colnames(tabest) <- c(t(outer(c("Excess","ExcessPer"),
c("","95eCIlow","95eCIhigh"), paste0)))
tab <- cbind(tab,tabest)
# ORDER
for(i in 1:3) tab[,i] <- factor(tab[,i], levels=unique(tab[,i]))
tab <- arrange(tab, Sex, Age)
# RETURN
tab
})
# NAMES AND WRITE
names(outitalylist) <- dimnames(excitaly)[[1]]
write.xlsx(outitalylist, file = "output/italy.xlsx")