library(ggplot2)
library(tidyverse)
library(kableExtra)
who <- read.csv("who.csv", header = T)
kable(head(who)) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = F)
Country | LifeExp | InfantSurvival | Under5Survival | TBFree | PropMD | PropRN | PersExp | GovtExp | TotExp |
---|---|---|---|---|---|---|---|---|---|
Afghanistan | 42 | 0.835 | 0.743 | 0.99769 | 0.0002288 | 0.0005723 | 20 | 92 | 112 |
Albania | 71 | 0.985 | 0.983 | 0.99974 | 0.0011431 | 0.0046144 | 169 | 3128 | 3297 |
Algeria | 71 | 0.967 | 0.962 | 0.99944 | 0.0010605 | 0.0020914 | 108 | 5184 | 5292 |
Andorra | 82 | 0.997 | 0.996 | 0.99983 | 0.0032973 | 0.0035000 | 2589 | 169725 | 172314 |
Angola | 41 | 0.846 | 0.740 | 0.99656 | 0.0000704 | 0.0011462 | 36 | 1620 | 1656 |
Antigua and Barbuda | 73 | 0.990 | 0.989 | 0.99991 | 0.0001429 | 0.0027738 | 503 | 12543 | 13046 |
##Question 1
Provide a scatterplot of LifeExp ~ TotExp, and run simple linear regression. Do not transform the variables. Provide and interpret the F statistics, R2, standard error,and p-values only. Discuss whether the assumptions of simple linear regression met.
qplot(who$TotExp, who$LifeExp, main="Total Country Healthcare Expenditures vs. Life Expectancy", xlab = "Total Country Expenditures on Healthcare (USD)", ylab = "Avg. Life Expectancy (Years)") +
scale_x_continuous(labels = function(x) format(x, scientific = FALSE))
model_who <- lm(LifeExp~TotExp,who)
plot(model_who)
summary(model_who)
##
## Call:
## lm(formula = LifeExp ~ TotExp, data = who)
##
## 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
model_who
##
## Call:
## lm(formula = LifeExp ~ TotExp, data = who)
##
## Coefficients:
## (Intercept) TotExp
## 6.475e+01 6.297e-05
R-squared: An adjusted R2 of about 0.25 means that healthcare expenditure per country explains 25% of its life expectancy. This is fairly low and suggests that a linear model with just healthcare expenditures alone cannot fully explain life expectancy.
Standard error: The model has a standard error of 0.753 for the intercept and 0.00000078 for the slope. These are small errors relative to the coefficients, which suggests that there is less variability in the model’s estimates for the parameters.
P-Value: A p-value of 7.71e−14 is tiny and very significant. This means there is a high likelihood that total healthcare expenditure is relevant in the model, and the model more accurately predicts it.
F-Statistic: The F-test reports the p-value of a model that has one fewer parameters than the current one. Since we are only considering one variable in this model, the F-statistic is not meaningful here.
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 r 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?”
life_exp <- who$LifeExp^4.6
tot_exp <- who$TotExp^0.06
qplot(tot_exp, life_exp, main="Total Country Healthcare Expenditures vs. Life Expectancy (Transformed)", xlab = "Total Country Expenditures on Healthcare (USD^0.06)", ylab = "Avg. Life Expectancy (Years^4.6)") + geom_smooth(method="lm") +
scale_y_continuous(labels = function(x) format(x, scientific = FALSE))
model_who_2 <- lm(life_exp ~ tot_exp)
summary(model_who_2)
##
## Call:
## lm(formula = life_exp ~ tot_exp)
##
## Residuals:
## Min 1Q Median 3Q Max
## -308616089 -53978977 13697187 59139231 211951764
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -736527910 46817945 -15.73 <2e-16 ***
## tot_exp 620060216 27518940 22.53 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 90490000 on 188 degrees of freedom
## Multiple R-squared: 0.7298, Adjusted R-squared: 0.7283
## F-statistic: 507.7 on 1 and 188 DF, p-value: < 2.2e-16
model_who_2
##
## Call:
## lm(formula = life_exp ~ tot_exp)
##
## Coefficients:
## (Intercept) tot_exp
## -736527909 620060216
R-squared: An adjusted R2 of about 0.73 means that healthcare expenditure per country explains 73% of a country’s life expectancy. This is much higher than the previous model.
Standard error: The model has a standard error of 46,817,945 for the intercept and 27,518,940 for the slope. These are pretty big errors relative to the coefficients, which suggests that the model is not estimating them very well.
P-Value: The model’s p-value of 2e−16 is tiny. This suggests that there is a high likelihood that total healthcare expenditure is relevant in the model, and the model more accurately predicts it. However, this doesn’t align with the model’s other parameters.
F-Statistic: The F-test reports the p-value of a model that has one fewer parameters than the current one. Since we are only considering one variable in this model, the F-statistic is not meaningful here.
The second model has a higher \(R^2\) at the same level of significance, which suggests that it is better at explaining the variance in life expectancy. However, it also has a bigger margin of error, adding more uncertainty to the predicted values.
Using the results from 2, forecast life expectancy when \(TotExp^.06 = 1.5\). Then forecast life expectancy when \(TotExp^.06 = 2.5\).
forecasted_Life_expectancy <- function(x){
ans <- model_who_2$coefficients[1] + model_who_2$coefficients[2] * x
return(ans ** (1/4.6)) # Must transform the data into a sensible number
}
print(paste0("The forecasted life expectancy when TotExp^.06 = 1.5 is: ", round(forecasted_Life_expectancy(1.5),2), " years."))
## [1] "The forecasted life expectancy when TotExp^.06 = 1.5 is: 63.31 years."
print(paste0("The forecasted life expectancy when TotExp^.06 = 2.5 is : ", round(forecasted_Life_expectancy(2.5),2), " years."))
## [1] "The forecasted life expectancy when TotExp^.06 = 2.5 is : 86.51 years."
Build the following multiple regression model and interpret the F Statistics, R2, standard error, and p-values. How good is the model?
model_who_4 <- lm(LifeExp ~ PropMD + TotExp + (PropMD * TotExp), who)
summary(model_who_4)
##
## Call:
## lm(formula = LifeExp ~ PropMD + TotExp + (PropMD * TotExp), data = who)
##
## 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
This model has an F-statistic of 34.49 and a statistically significant p-value < 2.2e-16, a residual standard error of 8.765, and adjusted R-squared 0.3471. This model is better than the first model(model_who) but worse than the second model(model_who_2). The only thing improved here compared to the first model is that the precision is slightly better with the standard error being at 8.765. The adjusted R-squared suggests that 34.71% of the variation in this data could be interpreted from this model.
Therefore this model is not as good as the second model.
Forecast LifeExp when PropMD = 0.03 and TotExp = 14. Does this forecast seem realistic? Why or why not?
forecast_life_expectancy <- function(PropMD, TotExp){
d <- data.frame(PropMD = c(PropMD), TotExp = c(TotExp))
prediction <- predict(model_who_4, d)
cat(paste("Life Expectancy:", round(unname(prediction),1), "years"))
}
forecast_life_expectancy(0.03, 14)
## Life Expectancy: 107.7 years
This model assumes that 3% of the population of a country are doctors (PropMD) and the country’s total expenditure on healthcare is 14 USD (TotExp). The resulting predicted life expectancy is 107 years.
This doesn’t make sense for a lot of reasons – the total expenditure on healthcare is absurdly low at 14 USD, and the average life expectancy is absurdly high as above 100 years old.