Loading data

Importing municipalities

Loading data

Y = admissions

library(arrow)
## 
## Attaching package: 'arrow'
## The following object is masked from 'package:lubridate':
## 
##     duration
## The following object is masked from 'package:utils':
## 
##     timestamp
admission <- read_parquet("d:/Users/thomas.daher/Desktop/Projeto_covid/cleaned_data/admission.parquet")
admission <- drop_na(admission, EVOLUCAO)
gc()
##            used  (Mb) gc trigger (Mb)  max used   (Mb)
## Ncells  7270462 388.3   15315428  818   7702429  411.4
## Vcells 66618400 508.3  172490691 1316 143675576 1096.2
Y <- admission %>%
  group_by(CO_MUN_NOT, SEM_DTMIN) %>%
  summarise(count = n()) %>%
  rename(Y = count)
## `summarise()` has grouped output by 'CO_MUN_NOT'. You can override using the
## `.groups` argument.
panel <- Y

X1 = Idade media dos habitantes internados dos municipios

X1 <- group_by(admission,CO_MUN_NOT,SEM_DTMIN) %>% summarise(mean_age = mean(NU_IDADE_N)) %>% rename(X1 = mean_age)
## `summarise()` has grouped output by 'CO_MUN_NOT'. You can override using the
## `.groups` argument.
panel <- joined_df <- left_join(panel, X1, by = c("CO_MUN_NOT", "SEM_DTMIN"))

X2 = renda media

df_population <- read_excel("d:/Users/thomas.daher/Desktop/Projeto_covid/Vacinas/estimativa_populacao_2021.xlsx")

colnames(df_population) <- c("uf","cod_uf","cod_ibge","nm_mun","pop")
aux <- (as.numeric(df_population$pop))
## Warning: NAs introduzidos por coerção
df_population$pop <- aux
df_population$cod_ibge <- paste(df_population$cod_uf, df_population$cod_ibge, sep = "")
df_population <- df_population %>% select(cod_ibge,pop)
rm(aux)

pib <- fread("d:/Users/thomas.daher/Desktop/Projeto_covid/data/pib_municipal_serie_historica.csv")
colnames(pib)[4]  <- "pib_municipal"
colnames(pib)[1]  <- "cod_ibge"
pib <- na.omit(pib)
pib <-pib %>% select(cod_ibge,Ano,pib_municipal)

pib <-left_join(pib,df_population, by = "cod_ibge")
pib$pib_municipal <- as.numeric(pib$pib_municipal)
## Warning: NAs introduzidos por coerção
pib$pib_per_capta <- (pib$pib_municipal/pib$pop)

X2 <- pib %>%
  filter(Ano == 2020) %>%
  select(cod_ibge, pib_per_capta)
X2$cod_ibge <- substr(X2$cod_ibge, 1, 6)

colnames(panel)[1] <- "cod_ibge"
panel$cod_ibge <- as.character(panel$cod_ibge)
colnames(X2)[2] <- "X2"
panel <- left_join(panel, X2, by = "cod_ibge")

X3 = precipitacao

X4 = temperatura

X5 = vacina per capita

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

colnames(X5)[2]<- "SEM_DTMIN"
X5$cod_ibge <- as.character(X5$cod_ibge)
colnames(X5)[3] <- "X5"

panel <- left_join(panel, X5, by = c("cod_ibge","SEM_DTMIN"))
panel$X5 <- replace_na(panel$X5,0)

X6 = Densidade populacional

### calculo area

brazil_municipalities$area <- st_area(brazil_municipalities)
names(brazil_municipalities)[1] <- "cod_ibge"
df_population$cod_ibge <- as.numeric(df_population$cod_ibge)
## Warning: NAs introduzidos por coerção
brazil_municipalities <- left_join(brazil_municipalities,df_population, "cod_ibge")
brazil_municipalities$area <- (brazil_municipalities$area/1000000) 
brazil_municipalities$density <- (brazil_municipalities$pop/brazil_municipalities$area)

colnames(brazil_municipalities)[11] <- "density_1/km^2"
X6 <- brazil_municipalities %>% 
  select(cod_ibge,`density_1/km^2`)

X6$cod_ibge <- substr(X6$cod_ibge, 1, 6)
colnames(X6)[2] <- "X6"

panel <- left_join(panel, X6, by = "cod_ibge")
panel <- panel %>% select(-geom)

The OLS Model

## Y = admissions 
## X1 = idade media dos habitantes do municipio
## X2 = renda media
## X3 = precipitacao
## X4 = temperatura
## X5 = vacina per capita
## X6 = Densidade populacional

## the model

ols <- lm(Y ~ X1 + X2 + X5 + X6, data = panel)
summary(ols)
## 
## Call:
## lm(formula = Y ~ X1 + X2 + X5 + X6, data = panel)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
##  -510.2   -16.2   -11.6     1.4 10298.3 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.316e+01  1.513e+00   8.696   <2e-16 ***
## X1          -1.006e-02  2.328e-02  -0.432    0.666    
## X2           1.328e-01  1.139e-02  11.663   <2e-16 ***
## X5          -3.886e+01  1.566e+00 -24.816   <2e-16 ***
## X6           3.407e-02  2.947e-04 115.594   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 155 on 156239 degrees of freedom
##   (770 observations deleted due to missingness)
## Multiple R-squared:  0.08252,    Adjusted R-squared:  0.0825 
## F-statistic:  3513 on 4 and 156239 DF,  p-value: < 2.2e-16

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
fixed_effects <- plm(Y ~ X1 + X2 + X5 + X6 , data = panel,index = c("Location","Time"), model = "within")
summary(fixed_effects)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = Y ~ X1 + X2 + X5 + X6, data = panel, model = "within", 
##     index = c("Location", "Time"))
## 
## Unbalanced Panel: n = 3485, T = 1-154, N = 156244
## 
## Residuals:
##       Min.    1st Qu.     Median    3rd Qu.       Max. 
## -2863.2381   -11.3419    -2.4788     7.4836  7665.6511 
## 
## Coefficients:
##      Estimate Std. Error t-value  Pr(>|t|)    
## X1   0.431329   0.018705  23.060 < 2.2e-16 ***
## X5 -60.712824   1.203119 -50.463 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    2017600000
## Residual Sum of Squares: 1976200000
## R-Squared:      0.020547
## Adj. R-Squared: -0.0018043
## F-statistic: 1602.3 on 2 and 152757 DF, p-value: < 2.22e-16
stargazer(ols, fixed_effects, title = "Regression Results", align = TRUE, type = "text")
## 
## Regression Results
## ===============================================================================
##                                         Dependent variable:                    
##                     -----------------------------------------------------------
##                                                  Y                             
##                                  OLS                          panel            
##                                                              linear            
##                                  (1)                           (2)             
## -------------------------------------------------------------------------------
## X1                             -0.010                       0.431***           
##                                (0.023)                       (0.019)           
##                                                                                
## X2                            0.133***                                         
##                                (0.011)                                         
##                                                                                
## X5                           -38.856***                    -60.713***          
##                                (1.566)                       (1.203)           
##                                                                                
## X6                            0.034***                                         
##                               (0.0003)                                         
##                                                                                
## Constant                      13.159***                                        
##                                (1.513)                                         
##                                                                                
## -------------------------------------------------------------------------------
## Observations                   156,244                       156,244           
## R2                              0.083                         0.021            
## Adjusted R2                     0.082                        -0.002            
## Residual Std. Error     154.969 (df = 156239)                                  
## F Statistic         3,513.172*** (df = 4; 156239) 1,602.299*** (df = 2; 152757)
## ===============================================================================
## Note:                                               *p<0.1; **p<0.05; ***p<0.01