Countries <- read.csv("/Users/danielmedlin/Downloads/AllCountries.csv")

##Qeustion 1

#Model

model_1 <- lm(LifeExpectancy ~ GDP, data = Countries)

summary(model_1)
## 
## 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

#Conclusion The coefficients show that for every $1000 increase in the GPD, life expectancy would increase by about 0.248 years. The R^2 shows that GPD explains about 43% of the variation in life expectanccy across countries.

##Question 2

#Model

model_2 <- lm(LifeExpectancy ~ GDP + Health + Internet, data = Countries)
summary(model_2)
## 
## 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

#Conclusion While controlling for GDP and Interner access, a one-percent increase in government healthcare expenditures results in an approximate 0.248 year increase in life expectancy. As for R^2, the model shows a multiple r^2 of 0.7213, meaning the model explains about 73% of the variance in life expectancy, not accounting for the other 30%.

##Question 3

#Model

par(mfrow = c(1,1))

plot(model_1, which = 1)

plot(model_1, which = 2)

#Conclusion The residuals vs fitted is not ideal, with the red line following a curve instead of a straight line around zero. The residuals are also not randomly scattered around zero. This suggests that the homoscedasticity assumption may be violated. The Q-Q residuals is also not ideal. While the residuals near the center follow the diagonal line, the ends show a noitceable curve away from the diagonal. This could mean the residuals aren’t normally distributed.

##Question 4

#Model

rmse <- sqrt(mean(residuals(model_2)^2))

rmse
## [1] 4.056417

#Conclusion Countries with large residuals cound have life expectancies that are highly different from the model’s predictions for those countries. A further analysis could investigate characteristics that aren’t shown in the model, such as healthcare conditions or geographic conditions, to see if that can explain the difference.

##Question 5

Hypothetical Example: (Multicollinearity in Multiple Regression) Suppose you are analyzing the AllCountries dataset and fit a multiple linear regression model to predict CO2 emissions (metric tons per capita) using Energy (kilotons of oil equivalent) and Electricity (kWh per capita) as predictors. You notice that Energy and Electricity are highly correlated. Explain how this multicollinearity might affect the interpretation of the regression coefficients and the reliability of the model.

#Conclusion In that hypothetical, having energy and electricity be highly correlated makes it difficult to find the individual effect each has on CO2 emissions. Regression coefficients could become difficult to interpret since the predictors would have similar information. This could reduce confidence in the individual coefficients whether the model predicts well or not.