A provided who.csv dataset contains real-world data from 2008. Reading in this dataset from my desktop, here are the first 6 rows:
path <- "C:/Users/Kavya/Desktop/Kavya/msds/DATA 605 Computational Mathematics/Week 12/who.csv"
who <- read.csv(path, sep = ",", 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 |
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.
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))
When we model life expectancy and healthcare expenditures, we get:
\[ \hat{LifeExp} = 0.000063 * TotExp + 64.8 \]
lm_1 <- lm(who$LifeExp ~ who$TotExp)
summary(lm_1)
##
## Call:
## lm(formula = who$LifeExp ~ who$TotExp)
##
## 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 ***
## who$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
R-squared: An adjusted \(R^2\) 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.71 e^{-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))
When we model the transformed variables, we get:
\[ \hat{LifeExp} = 620 \ 060 \ 216 * TotExp - 736 \ 527 \ 910 \]
lm_2 <- lm(life_exp ~ tot_exp)
summary(lm_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
R-squared: An adjusted \(R^2\) 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 whenTotExp
\(^.06=2.5\).
a <- -736527910
b <- 620060216
x_1 <- 1.5
x_2 <- 2.5
p_1 <- as.integer(x_1*b + a)
p_2 <- as.integer(x_2*b + a)
TotExp
\(^.06 =1.5,\ \) or about 861 USD,\(\ \) LifeExp
\(^{4.6}= 193 \ 562 \ 414\), or about 63 years.# Healthcare expenditure (USD)
sprintf('Healthcare expenditure (USD) = %f', x_1^(50/3))
## [1] "Healthcare expenditure (USD) = 860.704984"
# Adjusted life expectancy (years^4.6)
sprintf('Adjusted life expectancy (years^4.6) = %f', p_1)
## [1] "Adjusted life expectancy (years^4.6) = 193562414.000000"
# Predicted life expectancy (years)
sprintf('Predicted life expectancy (years) = %f', p_1^(5/23))
## [1] "Predicted life expectancy (years) = 63.311533"
TotExp
\(^.06 =2.5,\ \) or about 4.2 million USD,\(\ \) LifeExp
\(^{4.6}= 813 \ 622 \ 630\), or about 87 years.# Healthcare expenditure (USD)
sprintf('Healthcare expenditure (USD) = %f', x_2^(50/3))
## [1] "Healthcare expenditure (USD) = 4288777.125348"
# Adjusted life expectancy (years^4.6)
sprintf('Adjusted life expectancy (years^4.6) = %f', p_2)
## [1] "Adjusted life expectancy (years^4.6) = 813622630.000000"
# Predicted life expectancy (years)
sprintf('Predicted life expectancy (years) = %f', p_2^(5/23))
## [1] "Predicted life expectancy (years) = 86.506448"
Build the following multiple regression model and interpret the F Statistics, \(R^2\), standard error, and p-values. How good is the model?
\(LifeExp = b_0 + (b_1 \times PropMd) + (b_2 \times TotExp) + (b_3 \times PropMD \times TotExp)\)
lm_4 <- lm(who$LifeExp ~ who$PropMD + who$TotExp + (who$PropMD * who$TotExp))
summary(lm_4)
##
## Call:
## lm(formula = who$LifeExp ~ who$PropMD + who$TotExp + (who$PropMD *
## who$TotExp))
##
## 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 ***
## who$PropMD 1.497e+03 2.788e+02 5.371 2.32e-07 ***
## who$TotExp 7.233e-05 8.982e-06 8.053 9.39e-14 ***
## who$PropMD:who$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
R-squared: An adjusted \(R^2\) of about 0.35 means that the variables explain 35% of a country’s life expectancy. This is much lower than the previous model.
Standard error: The model’s standard errors – and ratios to the coefficients – vary. The higher the ratio to the coefficients, the more sure we can be of the variable. The lower the ratio, th
Intercept
\(= 0.7956 \ \) — high ratio, lower variabilitywho$PropMD
\(= 278.8 \ \) — low ratio, higher variabilitywho$TotExp
\(= 0.0000089 \ \) — high ratio, lower variabilitywho$PropMD:who$TotExp
\(= 0.0015 \ \) — low ratio, higher variability \P-Value: The model’s p-values are all tiny. This suggests that there is a high likelihood that they are relevant in the model, and the model more accurately predicts them.
F-Statistic: The F-statistic of this model is very similar to the current R^2, which suggests that removing one variable does not cause a large amount of change in the model’s reliability.
Forecast LifeExp when
PropMD
= 0.03 andTotExp
= 14. Does this forecast seem realistic? Why or why not?
b_0 <- lm_4$coefficients[1]
x_1 <- lm_4$coefficients[2]
x_2 <- lm_4$coefficients[3]
x_3 <- lm_4$coefficients[4]
p_1 <- b_0 + (x_1 * 0.03) + (x_2 * 14) + (x_3 * 0.03 * 14)
p_1
## (Intercept)
## 107.696
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 at over 100 years old.