-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSIDEP_Prep.R
71 lines (58 loc) · 2.04 KB
/
SIDEP_Prep.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
# required libraries
library(lattice)
library(lubridate)
library(sf)
# define local functions
mvavg <- function(tab, njours) {
out <- tab
for (j in njours:ncol(tab)) {
jj <- (j - njours + 1):j
out[, j] <- apply(tab[, jj, drop = FALSE], 1, mean, na.rm = TRUE)
}
out <- out[, -(1:(njours - 1))]
return(out)
}
# define parameters
iskon <- "2021-11-24"
# read the latest file for metropolitan France
dat <- readRDS(file = "data/sidep.rds")
# prepare working data
dat2 <- subset(dat, date > iskon)
# compute population per department
popdep <- aggregate(pop ~ dep, data = dat2, FUN = max)
popinv <- diag(10^6 / popdep$pop)
dimnames(popinv) <- list(popdep$dep, popdep$dep)
# tabulate positives tests per department / date
tabpos1 <- tapply(dat2$P, list(dat2$dep, dat2$date), FUN = min)
# compute 7-day average (and drop first 6 days)
tabpos2 <- mvavg(tabpos1, njours = 7)
# compute values per million
tabpos3 <- popinv %*% tabpos2
tabpos4 <- popinv %*% tabpos1[, -(1:6)]
# build the data.frame
dat3 <- data.frame(dep = rep(row.names(tabpos3), each = ncol(tabpos3)),
date = as.Date(rep(colnames(tabpos3), nrow(tabpos3))),
cpm1 = as.numeric(t(tabpos4)),
cpm7 = as.numeric(t(tabpos3)))
# additional variables
dat3[, "zero"] <- 0
dat3[, "dow"] <- lubridate::wday(dat3$date, week_start = 1)
# departement full name
departements <- readRDS(file = "data/departements.rds")
idx <- match(dat3$dep, departements$code)
dat3$dept <- departements$dep[idx]
# write the file
saveRDS(dat3, "data/sidep7.rds")
# read the map
france <- readRDS(file = "data/map_france.rds")
# read the latest file of 7-day smoothed data for metropolitan France
dat7 <- readRDS(file = "data/sidep7.rds")
# convert to wide format
tab7 <- reshape(dat7[, c("dep", "date", "cpm7")], idvar = "dep", timevar = "date", direction = "wide")
row.names(tab7) <- NULL
# write the file
saveRDS(tab7, "data/tab7.rds")
# merge with map
france7 <- merge(france, tab7, by.x = "code", by.y = "dep", all = TRUE)
# write the file
saveRDS(france7, "data/france7.rds")