Skip to content

Instantly share code, notes, and snippets.

@aerdely
Created June 21, 2022 17:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aerdely/0cd8f09d447f97f9da940cb7a8eac876 to your computer and use it in GitHub Desktop.
Save aerdely/0cd8f09d447f97f9da940cb7a8eac876 to your computer and use it in GitHub Desktop.
Procesar datos COVID-19
### Procesar datos SSalud para modelo compartimental
using CSV, DataFrames, Dates
## Cargar datos
@time begin
fechaDate = Date(2022,6,16)
archivo = "220616COVID19MEXICO.csv"
println("Fecha de los datos: ", fechaDate, "\n")
df = DataFrame(CSV.File(archivo))
println(size(df))
println(names(df), "\n")
end # 30 s
## Depurar fecha de las NO defunciones y convertir todo a formato Date
eltype(df.FECHA_DEF)
df.FECHA_DEF
@time begin
i999 = findall(df.FECHA_DEF .== "9999-99-99")
df.FECHA_DEF[i999] .= "2100-01-01"
colFechaDef = fill(Date(2000,1,1), length(df.FECHA_DEF))
for i ∈ 1:length(df.FECHA_DEF)
colFechaDef[i] = Date(df.FECHA_DEF[i], DateFormat("y-m-d"))
end
df.FECHA_DEF = colFechaDef
eltype(df.FECHA_DEF)
end # 55 s
## Valores iniciales y factores
begin
censo2020 = 126_014_024 # Población mexicana 15-marzo-2020 (INEGI censo 2020)
factor_anual_crec_pobl = 1.011 # 1.1% de tasa anual de crecimiento poblacional pre-pandemia
factor_diario_crec = factor_anual_crec_pobl ^ (1/365)
PobMex = Int(round(censo2020 / (factor_diario_crec ^ 75))) # pobl mex estimada al 01-enero-2020
factorPos = 30.0 # factor que mutiplicado por los casos confirmados da los estimados
factorMue = 2.27 # factor que multiplicado por muertes confirmadas da las estimadas (SE09: 05-Marzo-2022)
dact = 14 # número de días que se considera una infección activa a partir del inicio de síntomas
end;
## Clasificación de casos
@time begin
iPos = findall(x -> x ∈ [1,2,3], df.CLASIFICACION_FINAL)
iMue = findall(x -> x ≠ Date(2100,1,1), df.FECHA_DEF) ∩ iPos # muertos positivos
iSos = findall(x -> x ∈ [4,5,6], df.CLASIFICACION_FINAL)
iNeg = findall(df.CLASIFICACION_FINAL .== 7)
println("Positivos: ", length(iPos))
println("Muertos: ", length(iMue))
println("Sospechosos: ", length(iSos))
println("Negativos: ", length(iNeg), "\n")
end # 1.8 s
## Fechas
begin
fechaInicio = Date(2020,1,1) # inicio de la epidemia
fechaFinal = fechaDate # fecha final últimos datos
serieFechas = collect(fechaInicio:Day(1):fechaFinal)
n = length(serieFechas)
end
## Construir series de tiempo históricas
@time begin
P = zeros(Int, n) # Positivos por fecha de inicio de síntomas
PP = zeros(Int, n) # Positivos acumulados (vivos o muertos)
M = zeros(Int, n) # Muertos por fecha de defunción
MM = zeros(Int, n) # Muertos acumulados
S = zeros(Int, n) # Susceptibles
S[1] = PobMex
I = zeros(Int, n) # Infectados activos
RR = zeros(Int, n) # Recuperados acumulados (aliviados + muertos) => R(t) = PP(t) - I(t)
for t ∈ 2:n
d = serieFechas[t]
iFechaPos = findall(df.FECHA_SINTOMAS .== d) ∩ iPos
P[t] = Int(round(length(iFechaPos) * factorPos))
iFechaMue = findall(df.FECHA_DEF .== d) ∩ iMue
M[t] = Int(round(length(iFechaMue) * factorMue))
iInfecActivo = findall(d - Day(dact) .< df.FECHA_SINTOMAS .≤ d) ∩ iPos ∩ findall(df.FECHA_DEF .> d)
I[t] = Int(round(length(iInfecActivo) * factorPos))
S[t] = Int(round(factor_diario_crec * S[t-1])) - (I[t] - I[t-1]) - M[t]
end
PP = cumsum(P); MM = cumsum(M); RR = PP .- I
end; # 26 m
## Archivo de series históricas
begin
covid = DataFrame(fecha = serieFechas, nuevos = P, activos = I, muertos = M, susceptibles = S, recuperados = RR)
CSV.write("SIR.csv", covid)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment