Breakdown of the Data - Dive:
# Load libs
library(tidyverse)
library(broom)
library(car)
library(MASS)
# Make alsis 'data' for the COVID dataset
data <- read.csv("../data/COVID_country.csv")
head(data)
## country iso3 year sex age_group pop type expected.mean
## 1 Afghanistan AFG 2020 Female 0-24 12485568 predicted 49084
## 2 Afghanistan AFG 2020 Female 25-34 2734917 predicted 6453
## 3 Afghanistan AFG 2020 Female 35-44 1684126 predicted 6118
## 4 Afghanistan AFG 2020 Female 45-54 1141134 predicted 7712
## 5 Afghanistan AFG 2020 Female 55-64 699319 predicted 10062
## 6 Afghanistan AFG 2020 Female 65-74 379159 predicted 13955
## acm.mean excess.mean. X
## 1 49103 0 NA
## 2 6691 237 NA
## 3 6977 860 NA
## 4 9330 1622 NA
## 5 12458 2401 NA
## 6 17144 3195 NA
summary(data)
## country iso3 year sex
## Length:6208 Length:6208 Min. :2020 Length:6208
## Class :character Class :character 1st Qu.:2020 Class :character
## Mode :character Mode :character Median :2020 Mode :character
## Mean :2020
## 3rd Qu.:2021
## Max. :2021
## age_group pop type expected.mean
## Length:6208 Min. : 0 Length:6208 Min. : 0.0
## Class :character 1st Qu.: 35177 Class :character 1st Qu.: 370.8
## Mode :character Median : 267600 Mode :character Median : 2438.0
## Mean : 2520267 Mean : 17998.0
## 3rd Qu.: 1194828 3rd Qu.: 9056.5
## Max. :324033981 Max. :1578937.0
## acm.mean excess.mean. X
## Min. : 0 Min. :-100092.0 Mode:logical
## 1st Qu.: 411 1st Qu.: 0.0 NA's:6208
## Median : 2720 Median : 84.0
## Mean : 20403 Mean : 2394.2
## 3rd Qu.: 10440 3rd Qu.: 799.2
## Max. :1733563 Max. : 588931.0
The linear model is based on the expected.mean, as
the response variable because the expected.mean is often
used in studies as the target variable to predict or understand expected
outcomes and how it is influenced by various factors.
The explanatory variables year, pop,
acm.mean, and excess.mean.. I selected these
variables based on an assumption that these factors could have a
quantitative relationship with the expected mean. For instance;
year might capture temporal trends, pop
represents the population which often correlates with many outcome
measures, acm.mean could directly affect the expected mean
and excess.mean. may offer insights towards the deviations
from the norm.
# Building the linear model
model <- lm(expected.mean ~ year + pop + acm.mean + `excess.mean.`, data = data)
# View coefficients
summary(model)
##
## Call:
## lm(formula = expected.mean ~ year + pop + acm.mean + excess.mean.,
## data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12583.4 6.6 9.4 12.0 22910.8
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.625e+03 1.998e+04 0.432 0.6659
## year -4.273e+00 9.886e+00 -0.432 0.6656
## pop 1.040e-06 4.341e-07 2.396 0.0166 *
## acm.mean 9.990e-01 7.616e-05 13117.671 <2e-16 ***
## excess.mean. -9.936e-01 3.612e-04 -2750.742 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 388.7 on 6203 degrees of freedom
## Multiple R-squared: 1, Adjusted R-squared: 1
## F-statistic: 6.78e+07 on 4 and 6203 DF, p-value: < 2.2e-16
Findings:
The coefficients for acm.mean and
excess.mean. are extremely significant with p-values much
less than 0.01. This suggests a strong linear relationship with
expected.mean.
acm.mean has a coefficient very close to 1. This
indicates that for every unit increase in acm.mean,
expected.mean increases by nearly the same amount, this is
assuming that other variables are held constant.
Conversely, excess.mean. has a negative coefficient
also close to -1. This implies that higher excess means are associated
with a decrease in the expected mean.
The pop variable, which represents the population,
has a small positive coefficient. This suggests a very slight increase
in expected.mean with larger populations, and it is
statistically significant (p < 0.05).
The year variable is not significant. This indicates
that within the limited year range of this data, time does not have a
discernible linear effect on expected.mean.
# Checking for multi-collinearity using VIF
vif(model)
## year pop acm.mean excess.mean.
## 1.003847 1.234611 1.977745 1.682883
Findings:
# Plotting residuals to assess the normal distribution of errors
par(mfrow=c(2,2))
plot(model)
null_model <- lm(expected.mean ~ 1, data=data)
# Perform Anova Test:
anova(null_model, model)
## Analysis of Variance Table
##
## Model 1: expected.mean ~ 1
## Model 2: expected.mean ~ year + pop + acm.mean + excess.mean.
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 6207 4.0981e+13
## 2 6203 9.3735e+08 4 4.098e+13 67796941 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Findings:
expected.mean, as shown by the extremely high F-statisitc
and p-value less than 2.2e-16.# Interpret the coefficients (focusing on `pop`)
coef(summary(model))
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 8.624516e+03 1.997549e+04 0.4317549 0.66593455
## year -4.273189e+00 9.886415e+00 -0.4322283 0.66559050
## pop 1.040008e-06 4.341468e-07 2.3955211 0.01662622
## acm.mean 9.990362e-01 7.615958e-05 13117.6707970 0.00000000
## excess.mean. -9.936306e-01 3.612227e-04 -2750.7421968 0.00000000
Findings:
pop: The positive
coefficient of approx 1.04e-06 suggests that for each additional person
in the population, the expected mean increases by a small amount.
Thought the effect size is rather small, it is significant. Ergo, in
large populations, this could accumulate to a noticeable effect on the
expected mean.The residual standard error is quite large relative to the magnitude of the coefficients, indicating potential variability in the model predictions.
The residuals show a wide range, from -12583.4 to 22910.8, this could suggest that the presence of outliers or extreme values are influencing models. We can think of “Niue” influence here from previous data dives.
The Multiple R-Squared and Adjusted R-Squared values are both 1, which is very unusual and may suggest overfitting.
The model appears to it the data exceptionally well according to the
R-squared values, yet, the large residuals and perfect fit raise
concerns regarding overfitting and data quality issues. With
acm.mean and excess.mean. are highly
significant predictors of expected.mean. This are practical
significance of these findings, but require careful consideration.
Hopefully in the next week’s data dives, I might be able to discover the
outliers or extreme values that may be influencing the model. My future
investigations need to include a deeper dive into data quality,
outliers, and additional validation of the model’s predictive power on
new or held-out data.