-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunciones.R
151 lines (128 loc) · 4.21 KB
/
funciones.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
# Funciones
library(tidyverse)
descargar.datos <- function() {
# Descarga las últimas bases de datos abiertos
message("Descargando bases de datos...")
curl::curl_download(
"https://cloud.minsa.gob.pe/s/Y8w3wHsEdYQSZRp/download",
"data/positivos_covid.csv", quiet = FALSE
)
curl::curl_download(
"https://cloud.minsa.gob.pe/s/Md37cjXmjT9qYSa/download",
"data/fallecidos_covid.csv", quiet = FALSE
)
curl::curl_download(
"https://cloud.minsa.gob.pe/s/To2QtqoNjKqobfw/download",
"data/vacunas_covid.7z", quiet = FALSE
)
message("Bases de datos descargadas")
message("Descomprimiendo base de datos de vacunación...")
archive::archive_extract("data/vacunas_covid.7z", dir = "data")
message("Base de datos descomprimida.")
}
fechaFix <- function(variable) {
# Convierte clase de variable de números a fecha
variable %>%
as.character() %>%
as.Date(format = "%Y%m%d")
}
plot.diarios <- function(DataFrame, x, y) {
# Gráfico de casos diarios
ggplot(DataFrame, aes({{x}} , {{y}})) +
geom_col() +
theme_minimal() +
geom_line(aes(y = zoo::rollmean({{y}}, 7, fill = NA)),
size = 1.2,
colour = "red1") +
scale_x_date(
labels = scales::label_date_short(),
breaks = scales::breaks_width("1 months")
) +
scale_y_continuous(labels = scales::comma_format(big.mark = " ")) +
theme(legend.position = "none",
legend.title = element_blank()) +
labs(x = element_blank(),
y = element_blank(),
title = element_blank())
}
plot.vacunaciones <- function(DataFrame, x, y, group) {
# Grafico de vacunaciones por tipo
ggplot(DataFrame, aes({{x}}, {{y}}, color = {{group}})) +
theme_minimal() +
geom_line(size = 1) +
scale_y_continuous(labels = scales::comma_format(big.mark = " ")) +
scale_x_date(
labels = scales::label_date_short(),
breaks = scales::breaks_width("1 months"))+
theme(legend.position = "right",
legend.title = element_blank()) +
labs(x = element_blank(),
y = element_blank(),
title = element_blank())
}
filtra_zona <- function(DataFrame, i_departamento, i_provincia, i_distrito) {
# Filtra la base de datos otorgada según los datos ingresados en el input
if (i_departamento != "Todos" &
i_provincia != "Todos" &
i_distrito != "Todos") {
DataFrame <- DataFrame %>%
filter(departamento == i_departamento) %>%
filter(provincia == i_provincia) %>%
filter(distrito == i_distrito)
} else if (i_departamento != "Todos" &
i_provincia != "Todos") {
DataFrame <- DataFrame %>%
filter(departamento == i_departamento) %>%
filter(provincia == i_provincia)
} else if (i_departamento != "Todos") {
DataFrame <- DataFrame %>%
filter(departamento == i_departamento)
} else {
DataFrame <- DataFrame
}
}
sum_casos <- function(DataFrame) {
# Suma los casos del df otorgado
DataFrame %>%
summarize(n = sum(n)) %>%
pull() %>%
as.character() %>%
prettyNum(big.mark = " ")
}
suma_poblacion <- function(DataFrame, value) {
DataFrame <- DataFrame %>%
group_by(departamento, provincia, distrito) %>%
summarise(n = sum({{value}}))
}
filtra_fechas <- function(DataFrame, fecha, fecha_inicio, fecha_fin){
# Filtra DF según las fechas dadas
DataFrame %>%
filter( {{fecha}} >= fecha_inicio & {{fecha}} <= fecha_fin)
}
### Funcion Indicador ###
unifica_tablas <- function(Positivos, Vacunacion, Poblacion, Pobreza){
Positivos <- Positivos %>%
group_by(departamento, provincia, distrito) %>%
summarise(positivos = sum(n))
Vacunacion <- Vacunacion %>%
group_by(departamento, provincia, distrito) %>%
summarise(vacunacion = sum(n))
Poblacion <- Poblacion %>% rename(poblacion = n)
tabla <- left_join(Positivos, Vacunacion) %>%
left_join(Pobreza) %>%
left_join(Poblacion) %>%
ungroup()
}
calculo_indicador <- function(DataFrame) {
a = 0.41
b = -0.17
c = -0.19
d = 0.72
st.d = 135.73
me = 35.18
DataFrame %>%
mutate(raw = d + positivos*a + (vacunacion/poblacion)*c + pobreza*b) %>%
mutate(indice = (raw - me)/(st.d)) %>%
summarise(mean = mean(indice, na.rm = TRUE)) %>%
pull()
}