df <- read_csv('who.csv')

Question 1

Provide a scatter plot 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.

p <- ggplot(data = df, aes(x = TotExp, y = LifeExp)) +
  geom_point() +
  ggtitle('Life Expectancy vs. Total Expendature', 'Data from the WHO') +
  xlab('Total Expendature (Government and Private)') +
  ylab('Life Expectancy') +
  theme_tufte() +
  geom_smooth(method='lm', se = FALSE, color = 'grey')
p

mod1 <- lm(formula = LifeExp ~ TotExp, data = df)
sum1 <- summary(mod1)
fstat <- sum1$fstatistic 
fstatVal <- fstat[1] %>% round(4)
fpVal <- pf(fstat[1],fstat[2],fstat[3],lower.tail=F)
rsq <- sum1$r.squared
standErr <- sum1$coefficients[ , 2]
sum1
## 
## 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

The F statistic is 65.2642, tests if any of the variables of OLS are significantly different from zero. Our F statistic was calculated with one independent variable and 188 degrees of freedom. it has a significance of 7.713993110^{-14} which is highly significant. This means that the one included variable is significant. (Similarly a t-test could have been performed.)

The \(R^2\) for the regression is: 0.2576922 which means 25.769216% of the variance in expenditure explains the variance in life expectancy.

The standard error is: 7.794665610^{-6} which is the standard deviation of the sampling distribution. Lower numbers mean more significance (because they directly relate to the t test.).

I did not include the SE of the intercept because intercepts are harder to interpret.

The p-value for the model is 8.078626 which means that it is highly significant. The same logic holds true in that I won’t include the intercept.

Question 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 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?”

p <- ggplot(data = df, aes(x = TotExp^0.06, y = LifeExp^4.6)) +
  geom_point() +
  ggtitle('Life Expectancy vs. Total Expendature', 'Data from the WHO') +
  xlab('Total Expendature (Government and Private)p^0.06') +
  ylab('Life Expectancy^4.6') +
  theme_tufte() +
  geom_smooth(method='lm', se = FALSE, color = 'grey')
p

mod2 <- lm(I(LifeExp^4.6) ~ I(TotExp^0.06), df)
sum2 <- summary(mod2)
fstat2 <- sum2$fstatistic 
fstatVal2 <- fstat2[1] %>% round(4)
fpVal2 <- pf(fstat2[1],fstat2[2],fstat2[3],lower.tail=F)
rsq2 <- sum2$r.squared
standErr2 <- sum2$coefficients[ , 2]
sum2
## 
## Call:
## lm(formula = I(LifeExp^4.6) ~ I(TotExp^0.06), data = df)
## 
## 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 ***
## I(TotExp^0.06)  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

The F statistic is 507.6967, is significant and also higher than the un-transformed model.

2.601428410^{-55} which is highly significant (basically zero) which is much better than the original.

The \(R^2\) for the regression is: 0.7297673 which means 72.9767299% of the variance in expenditure explains the variance in life expectancy. This is far higher than the original.

The standard error is: 2.75189410^{7} which is the standard deviation of the sampling distribution. Again this is better than the original (however it needs to be interpreted differently because the variables are different.)

I did not include the SE of the intercept because intercepts are harder to interpret.

The p-value for the model is 22.5321261 which means that it is highly significant. The same logic holds true in that I won’t include the intercept.

Question 3

Using the results from 3, forecast life expectancy when TotExp^.06 =1.5. Then forecast life expectancy when TotExp^.06=2.5.

new=data.frame(TotExp=c(860.705, 4288777))
predict(mod2, new)^(1/4.6) # Necessary since predict does not transform respons variable.
##        1        2 
## 63.31153 86.50645

Question 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 = b_0+b_1 \cdot PropMd + b_2 \cdot TotExp +b_3 \cdot PropMD \cdot TotExp\]

mod3 <- lm(LifeExp ~ PropMD + TotExp + I(PropMD * TotExp), df)
sum3 <- summary(mod3)

fstat3 <- sum3$fstatistic 
fstatVal3 <- fstat3[1] %>% round(4)
fpVal3 <- pf(fstat3[1],fstat3[2],fstat3[3],lower.tail=F)
rsq3 <- sum3$r.squared
standErr3 <- sum3$coefficients[ , 2]
sum3
## 
## Call:
## lm(formula = LifeExp ~ PropMD + TotExp + I(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 ***
## I(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 (with the interaction term) and additional variable performs better than the original with a higher F statistic: 34.4883. This is better than the un-transformed model but not as good as the transformed model. The p-value for this is 9.024193310^{-18}.

The \(R^2\) is also better than the original but not as good as the transformed: 0.3574352.

Likewise, the SE is not between the two models.

Question 5

Forecast LifeExp when PropMD=.03 and TotExp = 14. Does this forecast seem realistic? Why or why not?

new3 <- data.frame(PropMD = 0.03, TotExp = 14)
predict(mod3, new3)
##       1 
## 107.696

This makes little sense because one would not expect a country with the lowest total expenditure (around 14) to have around the highest percent of MDs. The model should only be interpreted locally.

Question 1 with Linear Algebra

X <- matrix(df$TotExp)
ones <- matrix(rep.int(1, length(X)))
X <- cbind(X,ones)
colnames(X) <- cbind('TotExp', 'Intercept')
y <- df$LifeExp
mod4 <- solve(crossprod(X), crossprod(X,y))
mod4
##                   [,1]
## TotExp    6.297019e-05
## Intercept 6.475337e+01