library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
df <- read.csv("who.csv")
The attached who.csv dataset contains real-world data from 2008. The variables included follow. Country: name of the country LifeExp: average life expectancy for the country in years InfantSurvival: proportion of those surviving to one year or more Under5Survival: proportion of those surviving to five years or more TBFree: proportion of the population without TB. PropMD: proportion of the population who are MDs PropRN: proportion of the population who are RNs PersExp: mean personal expenditures on healthcare in US dollars at average exchange rate GovtExp: mean government expenditures per capita on healthcare, US dollars at average exchange rate TotExp: sum of personal and government expenditures.
1. Provide a scatterplot of LifeExp~TotExp, and run simple linear regression. Do not transform the variables. Provide and interpret the F statistics, R^2, standard error,and p-values only. Discuss whether the assumptions of simple linear regression met.
ggplot(data=df, aes(x=TotExp, y=LifeExp)) +
geom_point()
lm1 <- lm(LifeExp~TotExp, data=df)
summary(lm1)
##
## Call:
## lm(formula = LifeExp ~ TotExp, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -24.764 -4.778 3.154 7.116 13.292
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.475e+01 7.535e-01 85.933 < 2e-16 ***
## TotExp 6.297e-05 7.795e-06 8.079 7.71e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.371 on 188 degrees of freedom
## Multiple R-squared: 0.2577, Adjusted R-squared: 0.2537
## F-statistic: 65.26 on 1 and 188 DF, p-value: 7.714e-14
plot(lm1, which=1)
Charting the residuals, we see a linear pattern. This suggests we may not meet a standard of linearity, independence of errors, or homoscedasity we would want in order to trust a simple linear regression.
2. Raise life expectancy to the 4.6 power (i.e., LifeExp^4.6). Raise total expenditures to the 0.06 power (nearly a log transform, TotExp^.06). Plot LifeExp^4.6 as a function of TotExp^.06, and re-run the simple regression model using the transformed variables. Provide and interpret the F statistics, R^2, standard error, and p-values. Which model is “better?”
df$LifeExp_trfm <- df$LifeExp^4.6
df$TotExp_trfm <- df$TotExp^0.6
ggplot(data=df, aes(x=TotExp_trfm, y=LifeExp_trfm)) +
geom_point()
lm2 <- lm(LifeExp_trfm~TotExp_trfm, data=df)
summary(lm2)
##
## Call:
## lm(formula = LifeExp_trfm ~ TotExp_trfm, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -257351739 -82599957 14030425 93896945 237720335
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 211907647 10234512 20.70 <2e-16 ***
## TotExp_trfm 238461 15021 15.88 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 113800000 on 188 degrees of freedom
## Multiple R-squared: 0.5728, Adjusted R-squared: 0.5705
## F-statistic: 252 on 1 and 188 DF, p-value: < 2.2e-16
The higher \(R^2\) in this version of the model suggests a better fit given the adjusted variables.
plot(lm2, which=1)
The residuals plot shows a slightly less (but still somewhat) linear pattern, which adds some question to to appropriateness of a simple linear regression.
3. Using the results from 3, forecast life expectancy when TotExp^.06 =1.5. Then forecast life expectancy when TotExp^.06=2.5.
intercept_lm <- coef(lm2)[1]
slope_lm <- coef(lm2)[2]
forecast_1.5 <- slope_lm * 1.5 + intercept_lm
forecast_2.5 <- slope_lm * 2.5 + intercept_lm
forecast_1.5
## TotExp_trfm
## 212265338
forecast_2.5
## TotExp_trfm
## 212503799
4. Build the following multiple regression model and interpret the F Statistics, R^2, standard error, and p-values. How good is the model?
LifeExp = b0+b1 x PropMd + b2 x TotExp +b3 x PropMD x TotExp
lm3 <- lm(LifeExp~PropMD + TotExp + PropMD * TotExp, data=df)
summary(lm3)
##
## Call:
## lm(formula = LifeExp ~ PropMD + TotExp + PropMD * TotExp, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -27.320 -4.132 2.098 6.540 13.074
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.277e+01 7.956e-01 78.899 < 2e-16 ***
## PropMD 1.497e+03 2.788e+02 5.371 2.32e-07 ***
## TotExp 7.233e-05 8.982e-06 8.053 9.39e-14 ***
## PropMD:TotExp -6.026e-03 1.472e-03 -4.093 6.35e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 8.765 on 186 degrees of freedom
## Multiple R-squared: 0.3574, Adjusted R-squared: 0.3471
## F-statistic: 34.49 on 3 and 186 DF, p-value: < 2.2e-16
The adjusted \(R^2\) suggests a little more than a third of the variance of life expectancy is explained by our model features.
coef(lm3)[4]
## PropMD:TotExp
## -0.006025686
intercept_lm3 <- coef(lm3)[1]
PropMD_coef <- coef(lm3)[2]
TotExp_coef <- coef(lm3)[3]
PropMD_TotExp_coef <- coef(lm3)[4]
forecast_lm3 <- intercept_lm3 + PropMD_coef*(0.3) + TotExp_coef*(14) + PropMD_TotExp_coef*(0.3)*(14)
forecast_lm3
## (Intercept)
## 511.9966
The life expectancy value I got here is around 512 years–a nonsensical amount of years to live (at least for now!)