Loading libraries

Importing municipalities

Loading data

Cleaning admission dataset

## 
## Attaching package: 'arrow'
## The following object is masked from 'package:lubridate':
## 
##     duration
## The following object is masked from 'package:utils':
## 
##     timestamp
## [1] "pregnant versus non-pregnant"
## 
##       0       1 
## 3630874   36180
## [1] "white = white + yellow = 1, non-white = others = 0)"
## 
##       0       1 
## 2137313 1529741
## [1] "urban = 1, rural + suburban = 0"
## 
##       0       1 
##  233913 3038803
## [1] "male = 1 and female = 0"
## 
##       0       1 
## 1701239 1965815
## [1] "Quantity of municipalities"
## [1] 5604

Y = admissions

### null panel

cod_ibge <- unique(admission$cod_ibge)
epi_week <- seq(1, 188)
gender <- c(1,0)
white <- c(1,0)

panel <- expand.grid(cod_ibge = cod_ibge,
                        epi_week = epi_week,
                        gender = gender,
                        white = white)
                        
### Admission panel
Y <- admission %>%
  group_by(cod_ibge, epi_week, gender, white) %>%
  summarise(count = n()) %>%
  rename(Y = count)
## `summarise()` has grouped output by 'cod_ibge', 'epi_week', 'gender'. You can
## override using the `.groups` argument.
panel$cod_ibge <- as.character(panel$cod_ibge)
Y$cod_ibge <- as.character(Y$cod_ibge)

### Join panel
panel <- left_join(panel, Y, by = c("cod_ibge", "epi_week","gender", "white"))
panel$Y[is.na(panel$Y)] <- 0
summary(panel)
##    cod_ibge            epi_week          gender        white    
##  Length:4214208     Min.   :  1.00   Min.   :0.0   Min.   :0.0  
##  Class :character   1st Qu.: 47.75   1st Qu.:0.0   1st Qu.:0.0  
##  Mode  :character   Median : 94.50   Median :0.5   Median :0.5  
##                     Mean   : 94.50   Mean   :0.5   Mean   :0.5  
##                     3rd Qu.:141.25   3rd Qu.:1.0   3rd Qu.:1.0  
##                     Max.   :188.00   Max.   :1.0   Max.   :1.0  
##        Y            
##  Min.   :   0.0000  
##  1st Qu.:   0.0000  
##  Median :   0.0000  
##  Mean   :   0.8702  
##  3rd Qu.:   0.0000  
##  Max.   :1951.0000
rm(Y)

age = mean age of admissions by municipality and epi_week

AGE <- admission %>% group_by(cod_ibge, epi_week, gender, white) %>% summarise(age = mean(age))
## `summarise()` has grouped output by 'cod_ibge', 'epi_week', 'gender'. You can
## override using the `.groups` argument.
AGE$cod_ibge <- as.character(AGE$cod_ibge)
panel <- left_join(panel, AGE, by = c("cod_ibge", "epi_week", "gender", "white"))
panel$age[is.na(panel$age)] <- 0

age2 = square of the mean age of admissions by municipality and epi_week

AGE2 <- AGE
AGE2$age2 <- (AGE2$age)^2
AGE2 <- AGE2 %>% select(-age)

panel <- left_join(panel, AGE2, by = c("cod_ibge", "epi_week", "gender", "white"))
panel$age2[is.na(panel$age2)] <- 0
rm(AGE,AGE2)
gc()
##            used  (Mb) gc trigger  (Mb)  max used  (Mb)
## Ncells  3062279 163.6    7819224 417.6   7819224 417.6
## Vcells 59245031 452.1  129862510 990.8 129428568 987.5

VACCINE

library(arrow)
VACCINE <- read_parquet("d:/Users/thomas.daher/Desktop/Projeto_covid/cleaned_data/vaccines.parquet")

colnames(VACCINE) <- c("cod_ibge", "epi_week","gender","white","cod_vac_group","cod_vac_category","vac_name","vac_given")
VACCINE$cod_ibge <- as.character(VACCINE$cod_ibge)
VACCINE$gender <- if_else(VACCINE$gender == "M", 1, 0)

Xvac <- VACCINE %>% group_by(cod_ibge,epi_week,gender,white) %>% summarise(vac_given = sum(vac_given))
## `summarise()` has grouped output by 'cod_ibge', 'epi_week', 'gender'. You can
## override using the `.groups` argument.
cod_ibge <- unique(admission$cod_ibge)
epi_week <- seq(1, 188)
gender <- c(1,0)
white <- c(1,0)

combine <- expand.grid(cod_ibge = cod_ibge,
                        epi_week = epi_week,
                        gender = gender,
                        white = white)
combine$cod_ibge <- as.character(combine$cod_ibge)

Xvac <- left_join(combine, Xvac, by = c("cod_ibge","epi_week","gender","white"))
Xvac$vac_given[is.na(Xvac$vac_given)] <- 0

Xvac <- Xvac %>%
  arrange(cod_ibge, epi_week,gender,white) %>%
  group_by(cod_ibge) %>%
  mutate(vac_given_cumulative = cumsum(vac_given))
Xvac <- Xvac %>% select(-vac_given)

panel$cod_ibge <- as.character(panel$cod_ibge)
panel <- left_join(panel, Xvac, by = c("cod_ibge","epi_week","gender","white"))

Fixed effects

# Convert time and location variables to factors
colnames(panel)[1] <- "Location"
colnames(panel)[2] <- "Time"

panel$Time <- factor(panel$Time)
panel$Location <- factor(panel$Location)

# Fit the model with fixed effects

formula <- " Y ~ gender + white + age + age2 + vac_given_cumulative"
fixed_effects <- plm(formula = formula, data = (panel %>% drop_na()),index = c("Time","Location"), model = "within")
## Warning in pdata.frame(data, index): duplicate couples (id-time) in resulting pdata.frame
##  to find out which, use, e.g., table(index(your_pdataframe), useNA = "ifany")
summary(fixed_effects)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = formula, data = (panel %>% drop_na()), model = "within", 
##     index = c("Time", "Location"))
## 
## Balanced Panel: n = 188, T = 5603, N = 4213456
## 
## Residuals:
##        Min.     1st Qu.      Median     3rd Qu.        Max. 
## -176.605190   -0.227974    0.061771    0.209675 1943.462189 
## 
## Coefficients:
##                         Estimate  Std. Error   t-value  Pr(>|t|)    
## gender                3.6788e-02  9.5925e-03    3.8351 0.0001255 ***
## white                -1.1795e-01  9.6015e-03  -12.2848 < 2.2e-16 ***
## age                   2.8551e-01  9.4611e-04  301.7777 < 2.2e-16 ***
## age2                 -3.1762e-03  1.3388e-05 -237.2401 < 2.2e-16 ***
## vac_given_cumulative  9.1134e-06  3.2262e-08  282.4777 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    432440000
## Residual Sum of Squares: 408080000
## R-Squared:      0.056321
## Adj. R-Squared: 0.056278
## F-statistic: 50291.1 on 5 and 4213263 DF, p-value: < 2.22e-16