Problem 1

# Read the data
countries <- read.csv("AllCountries.csv")

# Keep only observations with GDP and LifeExpectancy available
countries2 <- na.omit(countries[, c("GDP", "LifeExpectancy")])

# Fit simple linear regression model
life_model <- lm(LifeExpectancy ~ GDP, data = countries2)

# Display regression results
summary(life_model)
## 
## Call:
## lm(formula = LifeExpectancy ~ GDP, data = countries2)
## 
## 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
## Multiple R-squared:  0.4304, Adjusted R-squared:  0.4272 
## F-statistic: 133.7 on 1 and 177 DF,  p-value: < 2.2e-16
# Extract coefficients
coef(life_model)
##  (Intercept)          GDP 
## 6.842208e+01 2.476441e-04
# R-squared value
summary(life_model)$r.squared
## [1] 0.4303976
# Scatterplot with regression line
plot(countries2$GDP, countries2$LifeExpectancy,
pch = 19,
col = "steelblue",
xlab = "GDP per Capita (US$)",
ylab = "Life Expectancy (Years)",
main = "Life Expectancy vs GDP")
abline(life_model, col = "red", lwd = 2)

Problem 3

# Read the data
countries <- read.csv("AllCountries.csv")

# Keep only variables needed for the model
data_mlr <- na.omit(countries[, c("LifeExpectancy", "GDP", "Health", "Internet")])

# Fit multiple linear regression model
mlr_model <- lm(LifeExpectancy ~ GDP + Health + Internet, data = data_mlr)

# Display results
summary(mlr_model)
## 
## Call:
## lm(formula = LifeExpectancy ~ GDP + Health + Internet, data = data_mlr)
## 
## 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
## Multiple R-squared:  0.7213, Adjusted R-squared:  0.7164 
## F-statistic: 145.8 on 3 and 169 DF,  p-value: < 2.2e-16
# Regression coefficients
coef(mlr_model)
##  (Intercept)          GDP       Health     Internet 
## 5.908027e+01 2.367169e-05 2.478764e-01 1.903116e-01
# Adjusted R-squared
summary(mlr_model)$adj.r.squared
## [1] 0.7163973
# Compare with simple regression model
simple_model <- lm(LifeExpectancy ~ GDP, data = data_mlr)
summary(simple_model)$adj.r.squared
## [1] 0.4158405
summary(mlr_model)$adj.r.squared
## [1] 0.7163973
# Optional diagnostic plots
par(mfrow = c(2,2))
plot(mlr_model)

Problem 4

plot(simple_model$fitted.values,residuals(simple_model),
xlab = "Fitted Values",
ylab = "Residuals",
main = "Residuals vs Fitted Values")
abline(h = 0, col = "red", lwd = 2)

Problem 5

# Read data
countries <- read.csv("AllCountries.csv")

# Keep required variables and remove missing values
data_mlr <- na.omit(countries[, c("Country", "LifeExpectancy", "GDP", "Health", "Internet")])

# Fit multiple regression model
mlr_model <- lm(LifeExpectancy ~ GDP + Health + Internet, data = data_mlr)

# Model summary
summary(mlr_model)
## 
## Call:
## lm(formula = LifeExpectancy ~ GDP + Health + Internet, data = data_mlr)
## 
## 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
## Multiple R-squared:  0.7213, Adjusted R-squared:  0.7164 
## F-statistic: 145.8 on 3 and 169 DF,  p-value: < 2.2e-16
# ---------------------------
# Calculate RMSE
# ---------------------------

# Predicted values
predicted <- predict(mlr_model)

# Residuals
residuals <- data_mlr$LifeExpectancy - predicted

# RMSE
RMSE <- sqrt(mean(residuals^2))

RMSE
## [1] 4.056417
# ---------------------------
# Residual Plot
# ---------------------------
plot(predicted,
residuals,
pch = 19,
col = "steelblue",
xlab = "Predicted Life Expectancy",
ylab = "Residuals",
main = "Residuals vs Predicted Values")
abline(h = 0, col = "red", lwd = 2)

# Countries with largest residuals
data_mlr$Residual <- residuals
head(data_mlr[order(abs(data_mlr$Residual),
decreasing = TRUE), ],10)
##           Country LifeExpectancy   GDP Health Internet   Residual
## 48  Cote d'Ivoire           54.1  1716   4.88     43.8 -14.566175
## 112       Lesotho           54.6  1324  10.90     29.8 -12.884749
## 145       Nigeria           53.9  2028   5.01     27.7 -11.741767
## 171  Sierra Leone           52.2   523   7.91     13.2 -11.365464
## 64       Eswatini           58.3  4140  15.23     30.3 -10.419868
## 178  South Africa           63.4  6340  13.32     56.2  -9.827573
## 16     Bangladesh           72.8  1698   3.38     18.0   9.416106
## 114         Libya           72.1  7235   0.00     21.8   8.699673
## 39           Chad           53.2   730   5.90      6.5  -8.597045
## 97          Italy           83.2 34318  13.47     61.3   8.302370

Probelm 6

# Read the data
countries <- read.csv("AllCountries.csv")

# Keep only variables needed and remove missing values
co2_data <- na.omit(countries[, c("CO2", "Energy", "Electricity")])

# Fit multiple regression model
co2_model <- lm(CO2 ~ Energy + Electricity, data = co2_data)

# Display model results
summary(co2_model)
## 
## Call:
## lm(formula = CO2 ~ Energy + Electricity, data = co2_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12.7559  -1.1406  -0.2020   0.7143   7.3751 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  7.998e-01  2.655e-01   3.012  0.00311 ** 
## Energy       3.122e-03  1.066e-04  29.290  < 2e-16 ***
## Electricity -7.044e-04  5.526e-05 -12.747  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.331 on 131 degrees of freedom
## Multiple R-squared:  0.899,  Adjusted R-squared:  0.8974 
## F-statistic: 582.8 on 2 and 131 DF,  p-value: < 2.2e-16
# -------------------------------------------------
# Check correlation between predictors
# -------------------------------------------------
cor(co2_data$Energy,
co2_data$Electricity)
## [1] 0.7969352
# Correlation matrix
cor(co2_data[, c("Energy", "Electricity")])
##                Energy Electricity
## Energy      1.0000000   0.7969352
## Electricity 0.7969352   1.0000000
# Scatterplot of Energy vs Electricity
plot(co2_data$Energy, co2_data$Electricity, pch = 19, col = "steelblue", main = "Energy vs Electricity Consumption",
xlab = "Energy (kilotons of oil equivalent)",
ylab = "Electricity (kWh per capita)")
abline(lm(Electricity ~ Energy, data = co2_data),
col = "red",
lwd = 2)

# -------------------------------------------------
# Variance Inflation Factor (VIF)
# -------------------------------------------------
library(car)
## Loading required package: carData
vif(co2_model)
##      Energy Electricity 
##     2.74052     2.74052
# -------------------------------------------------
# Diagnostic plots
# -------------------------------------------------
par(mfrow = c(2, 2))
plot(co2_model)