Reading Data:
AllCountries <- read.csv("AllCountries.csv")
head(AllCountries)
## Country Code LandArea Population Density GDP Rural CO2 PumpPrice
## 1 Afghanistan AFG 652.86 37.172 56.9 521 74.5 0.29 0.70
## 2 Albania ALB 27.40 2.866 104.6 5254 39.7 1.98 1.36
## 3 Algeria DZA 2381.74 42.228 17.7 4279 27.4 3.74 0.28
## 4 American Samoa ASM 0.20 0.055 277.3 NA 12.8 NA NA
## 5 Andorra AND 0.47 0.077 163.8 42030 11.9 5.83 NA
## 6 Angola AGO 1246.70 30.810 24.7 3432 34.5 1.29 0.97
## Military Health ArmedForces Internet Cell HIV Hunger Diabetes BirthRate
## 1 3.72 2.01 323 11.4 67.4 NA 30.3 9.6 32.5
## 2 4.08 9.51 9 71.8 123.7 0.1 5.5 10.1 11.7
## 3 13.81 10.73 317 47.7 111.0 0.1 4.7 6.7 22.3
## 4 NA NA NA NA NA NA NA NA NA
## 5 NA 14.02 NA 98.9 104.4 NA NA 8.0 NA
## 6 9.40 5.43 117 14.3 44.7 1.9 23.9 3.9 41.3
## DeathRate ElderlyPop LifeExpectancy FemaleLabor Unemployment Energy
## 1 6.6 2.6 64.0 50.3 1.5 NA
## 2 7.5 13.6 78.5 55.9 13.9 808
## 3 4.8 6.4 76.3 16.4 12.1 1328
## 4 NA NA NA NA NA NA
## 5 NA NA NA NA NA NA
## 6 8.4 2.5 61.8 76.4 7.3 545
## Electricity Developed
## 1 NA NA
## 2 2309 1
## 3 1363 1
## 4 NA NA
## 5 NA NA
## 6 312 1
simple_model <- lm(LifeExpectancy ~ GDP, data = AllCountries)
summary(simple_model)
##
## Call:
## lm(formula = LifeExpectancy ~ GDP, data = AllCountries)
##
## 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
Interpretation:
The intercept of 68.42 means that when GDP is $0, the predicted life expectancy is about 68.42 years. The GDP coefficient is about 0.0002472. Since a $1 increase is very small, we can say that a $1,000 increase in GDP per capital is associated with about a 0.247-year increase in predicted life expectancy. The R² is 0.430, GDP is about 43% of the variation in life expectancy across the countries in the model.
multiple_model <- lm(
LifeExpectancy ~ GDP + Health + Internet,
data = AllCountries )
summary(multiple_model)
##
## Call:
## lm(formula = LifeExpectancy ~ GDP + Health + Internet, data = AllCountries)
##
## 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
Interpretation:
The Health coefficient is 0.2479. This means that for every 1 percentage-point increase in government healthcare spending, predicted life expectancy increases by about 0.25 years, and GDP and Internet access stay constant. Health is statistically significant with a p-value of 0.000247. The adjusted R² is 0.7164; this shows that the model explains about 71.6% of the variation in life expectancy. This is higher than the simple regression model’s R² of about 43%, showing that adding Health and Internet improves the model’s ability to explain differences in life expectancy across countries.
plot(simple_model, which = 1)
Homoscedasticity Reflection:
The residuals are not randomly scattered around zero. The graph shows a curved pattern, and the spread of the residuals changes across the fitted values. Therefore, the homoscedasticity assumption is not fully met. This suggests that the simple linear regression model may not predict life expectancy equally well across all GDP levels.
rmse <- sqrt(mean(residuals(multiple_model)^2))
rmse
## [1] 4.056417
The RMSE of 4.06 shows that the model’s predictions of life expectancy are typically off by about 4 years. Countries with large residuals have actual life expectancies that are very different from what the model predicts. This can lower confidence in predictions for those countries. Other factors such as poverty, disease, nutrition, or other social and economic conditions could be investigated.
If Energy and Electricity have a good correlation, we can use multicollinearity. Multicollinearity makes it difficult to determine the individual effect of Energy and Electricity on CO2 emissions because the two predictors contain similar information. Their regression coefficients can become unstable or difficult to interpret. The model may still make useful predictions, but we should be careful when interpreting the individual Energy and Electricity coefficients.