Keep AllCountries.csv in the same folder as this R
Markdown file. The original dataset has 217 countries. Some variables
have missing values, so R only uses rows with all required values for
each model.
countries <- read.csv("AllCountries.csv")
dim(countries)
## [1] 217 26
Question: Can GDP per person help predict life expectancy?
simple_model <- lm(LifeExpectancy ~ GDP, data = countries)
summary(simple_model)
##
## Call:
## lm(formula = LifeExpectancy ~ GDP, data = countries)
##
## Residuals:
## Min 1Q Median 3Q Max
## -16.352 -3.882 1.550 4.458 9.330
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.842e+01 5.415e-01 126.36 <2e-16 ***
## GDP 2.476e-04 2.141e-05 11.56 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.901 on 177 degrees of freedom
## (38 observations deleted due to missingness)
## Multiple R-squared: 0.4304, Adjusted R-squared: 0.4272
## F-statistic: 133.7 on 1 and 177 DF, p-value: < 2.2e-16
coef(simple_model)
## (Intercept) GDP
## 6.842208e+01 2.476441e-04
Answer: The intercept is about 68.42 and the slope is about 0.000248. The predicted life expectancy is 68.42 years when GDP per person is $0. This is mainly a starting point for the model and may not be realistic. For every $1 increase in GDP per person, predicted life expectancy increases by about 0.000248 years. A $1,000 increase in GDP per person is linked to about 0.248 more years of life expectancy, on average.
The R-squared is about 0.4304. This means GDP explains about 43.0% of the differences in life expectancy between the countries in this model. The rest may be related to other factors. This is an association, not proof that GDP causes people to live longer.
multiple_model <- lm(LifeExpectancy ~ GDP + Health + Internet, data = countries)
summary(multiple_model)
##
## Call:
## lm(formula = LifeExpectancy ~ GDP + Health + Internet, data = countries)
##
## Residuals:
## Min 1Q Median 3Q Max
## -14.5662 -1.8227 0.4108 2.5422 9.4161
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.908e+01 8.149e-01 72.499 < 2e-16 ***
## GDP 2.367e-05 2.287e-05 1.035 0.302025
## Health 2.479e-01 6.619e-02 3.745 0.000247 ***
## Internet 1.903e-01 1.656e-02 11.490 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.104 on 169 degrees of freedom
## (44 observations deleted due to missingness)
## Multiple R-squared: 0.7213, Adjusted R-squared: 0.7164
## F-statistic: 145.8 on 3 and 169 DF, p-value: < 2.2e-16
coef(multiple_model)
## (Intercept) GDP Health Internet
## 5.908027e+01 2.367169e-05 2.478764e-01 1.903116e-01
Answer: The Health coefficient is about 0.248. When GDP and Internet stay the same, a one-percentage-point increase in the share of government spending going toward healthcare is associated with about 0.248 additional years of predicted life expectancy.
The multiple regression’s adjusted R-squared is about 0.7164 (71.6%). The simple model’s adjusted R-squared is about 0.4272 (42.7%). The multiple regression explains more variation in life expectancy, even after accounting for the number of predictors. This suggests Health and Internet add useful information. The two models use slightly different sets of countries because of missing data, so the comparison is not perfectly like-for-like.
Homoscedasticity means the residuals should have a similar spread at low and high predicted life expectancy values. I can check this using a residuals-versus-fitted plot. Ideally, the dots should be scattered around zero without a clear pattern or funnel shape. If the spread changes a lot, the model’s uncertainty estimates may be less reliable.
Normality means the residuals should be approximately normally distributed. I can check this with a normal Q-Q plot. Ideally, the dots should follow the diagonal line. If they move far away from the line, especially at the ends, the normality assumption may not hold, and some statistical conclusions may be less reliable.
plot(simple_model, which = 1)
plot(simple_model, which = 2)
Reflection: After knitting the file, I can use the first graph to see whether the residual spread looks even and the second graph to see whether the dots follow the straight line. Any funnel shape, curved pattern, or large departures from the Q-Q line would show that the assumptions may not be fully met. These graphs are important because GDP alone cannot explain every difference in life expectancy.
rmse <- sqrt(mean(residuals(multiple_model)^2))
rmse
## [1] 4.056417
# Countries with the five largest absolute residuals
model_rows <- as.integer(rownames(model.frame(multiple_model)))
results <- data.frame(
Country = countries$Country[model_rows],
Actual = model.response(model.frame(multiple_model)),
Predicted = fitted(multiple_model),
Residual = residuals(multiple_model)
)
results[order(abs(results$Residual), decreasing = TRUE)[1:5], ]
## Country Actual Predicted Residual
## 48 Cote d'Ivoire 54.1 68.66617 -14.56617
## 112 Lesotho 54.6 67.48475 -12.88475
## 145 Nigeria 53.9 65.64177 -11.74177
## 171 Sierra Leone 52.2 63.56546 -11.36546
## 64 Eswatini 58.3 68.71987 -10.41987
Answer: The RMSE is about 4.06 years. This means the model’s predictions are typically off by around four years, measured using the root mean squared error. Larger errors count more heavily in RMSE.
If some countries have very large residuals, I would be less confident about predictions for those countries. I would check whether they have unusual conditions, such as differences in healthcare access, poverty, disease, or conflict. I would also check for missing information or possible data errors. The model might be missing important predictors.
Question: What happens if Energy and Electricity are highly correlated when predicting CO2 emissions?
Answer: Multicollinearity happens when predictor variables are strongly related to each other. If Energy and Electricity are highly correlated, it becomes hard to tell how much each one separately affects CO2 emissions. Their regression coefficients might change a lot if the data changes, and their standard errors could be larger. This makes it harder to trust the individual coefficients, even if the model predicts CO2 reasonably well. I would check the correlation between the two predictors and consider removing one of them if they give almost the same information.
# Optional check using the actual data (the question itself is hypothetical)
cor(countries$Energy, countries$Electricity, use = "complete.obs")
## [1] 0.7970054
GDP is related to life expectancy, but GDP alone does not explain all the differences between countries. Adding healthcare spending and internet access improves the model’s fit. The residual plots and RMSE help us understand how well the model works and where its predictions may be less accurate.