#Step 1: load csv
df <- read.csv("AllCountries.csv")
#Step 2: create simple linear regresssion
linreg <- lm(LifeExpectancy ~ GDP, df)
summary(linreg)
##
## Call:
## lm(formula = LifeExpectancy ~ GDP, data = df)
##
## 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 (68.42) represents the predicted LifeExpectancy when GDP is 0.
The coefficient for GDP (around 0.0002476) means for every 1 dollar increase in GDP per capita, Life Expectancy increases by about 0.0002476 years.
The R2 value explains 43.04% of the variance in Life Expectancy from GDP alone, which leaves most of the variance unpredicted
#Step 3: create multiple regression model
multiple_model <- lm(LifeExpectancy ~ GDP + Health + Internet, data = df)
multiple_model
##
## Call:
## lm(formula = LifeExpectancy ~ GDP + Health + Internet, data = df)
##
## Coefficients:
## (Intercept) GDP Health Internet
## 5.908e+01 2.367e-05 2.479e-01 1.903e-01
summary(multiple_model)
##
## Call:
## lm(formula = LifeExpectancy ~ GDP + Health + Internet, data = df)
##
## 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:
Health has a coefficient of 0.2479, meaning every increase by 1% of government expenditures directed towards healthcare increases Life Expectancy by about 0.2479.
The Adjusted R2 of the multiple-predictor model is 0.7164, which means about 72% of Life Expectancy variance can be explained by this model. This is a large improvement over the single-predictor model, which could only predict 43.04% of variance.
#Step 4: Checking Assumptions
To check the assumption of Homoscedasticity, we would make a Residuals vs. Fitted plot. Ideally, this plot would have a random scatter of points centered along the horizontal zero line. This would confirm that the model has a linear relationship, variance between errors is equal, and there are no major outliers.
To check normality of residuals we would make a Q-Q plot. Ideally the points on the plot would create a straight diagonal line. An S-curve or heavy tails would indicate that the residuals are not normally distributed, which would make our p-values and confidence intervals less valid.
plot(linreg)
The Q-Q plot for our model has heavy tails at the bottom and top, meaning the residuals are not normally distributed.
The Residuals vs. Fitted plot shows heteroscedasticity. It is funnel-shaped with a much wider spread of residuals at lower fitted values. This indicates that the accuracy of our model is not consistent across the data range.
#Part 5: Calculating RMSE for multiple_model
#Calculate Residuals
residuals_multiple <- resid(multiple_model)
# Calculate RMSE for multiple model
rmse_multiple <- sqrt(mean(residuals_multiple^2))
rmse_multiple
## [1] 4.056417
#Part 6: Multicollinearity in multiple regression