url <- 'https://github.com/mianshariq/SPS-Work/raw/dc3680c96416440e59ea32f2b6e793690e34268a/Data%20605/who.csv'
who <- read.csv(url)
head(who)
## Country LifeExp InfantSurvival Under5Survival TBFree PropMD
## 1 Afghanistan 42 0.835 0.743 0.99769 0.000228841
## 2 Albania 71 0.985 0.983 0.99974 0.001143127
## 3 Algeria 71 0.967 0.962 0.99944 0.001060478
## 4 Andorra 82 0.997 0.996 0.99983 0.003297297
## 5 Angola 41 0.846 0.740 0.99656 0.000070400
## 6 Antigua and Barbuda 73 0.990 0.989 0.99991 0.000142857
## PropRN PersExp GovtExp TotExp
## 1 0.000572294 20 92 112
## 2 0.004614439 169 3128 3297
## 3 0.002091362 108 5184 5292
## 4 0.003500000 2589 169725 172314
## 5 0.001146162 36 1620 1656
## 6 0.002773810 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.
plot(who$TotExp, who$LifeExp, xlab = "Total Expenditures", ylab = "Life Expectancy")
who_simple.lm <- lm(LifeExp ~ TotExp, data = who)
summary(who_simple.lm)
##
## 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
The F-table value for 1 regression degree of freedom and 120 residual degrees of freedom is 6.851. Since the F-statistic from our model, which has 1 regression degree of freedom and 188 residual degrees of freedom, is 65.26, is much greater than the F-table value, the F-statistic suggests we can reject the null hypothesis (a regression model with a zero coefficient). Similarly, the p-value is well below typical α thresholds. The R2=0.2577 value is not strong, it tells us that 25.77% of the variation in the data is accounted for in our model, which means the model does not strongly fit the data. The standard error is a reasonably small percentage of the coefficient.
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?”
who$LifeExp4.6 <- (who$LifeExp)^4.6
who$TotExp0.06 <- (who$TotExp)^0.06
plot(who$TotExp0.06, who$LifeExp4.6, xlab = "Total Expenditures ^ 0.06", ylab = "Life Expectancy ^ 4.6")
who_transformed.lm <- lm(LifeExp4.6 ~ TotExp0.06, data = who)
summary(who_transformed.lm)
##
## Call:
## lm(formula = LifeExp4.6 ~ TotExp0.06, data = who)
##
## 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 ***
## TotExp0.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 from our transformed model is 507.7 (same degrees of freedom as model from 1.) is much better vs. the F-table value than the prior model. Similarly, the p-value is even better. Finally, the R2=0.7298 value is dramatically superior to our model from 1. The transformed model is the better model. The standard error is a reasonably small percentage of the coefficient. ## 3. Using the results from 3, forecast life expectancy when TotExp^.06 =1.5. Then forecast life expectancy when TotExp^.06=2.5.
Our model is LifeExp4.6=−736527910+620060216∗TotExp0.06
le.transformed <- function(totexp) {
return((-736527910 + (620060216 * totexp))^(1/4.6))
}
# Example usage
result1 <- le.transformed(1.5)
result2 <- le.transformed(2.5)
result1
## [1] 63.31153
result2
## [1] 86.50645
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
who_multiple.lm <- lm(who$LifeExp4.6 ~ who$PropMD + who$TotExp0.06 + who$PropMD:who$TotExp0.06)
summary(who_multiple.lm)
##
## Call:
## lm(formula = who$LifeExp4.6 ~ who$PropMD + who$TotExp0.06 + who$PropMD:who$TotExp0.06)
##
## Residuals:
## Min 1Q Median 3Q Max
## -296470018 -47729263 12183210 60285515 212311883
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -7.244e+08 5.083e+07 -14.253 <2e-16 ***
## who$PropMD 4.727e+10 2.258e+10 2.094 0.0376 *
## who$TotExp0.06 6.048e+08 3.023e+07 20.005 <2e-16 ***
## who$PropMD:who$TotExp0.06 -2.121e+10 1.131e+10 -1.876 0.0622 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 88520000 on 186 degrees of freedom
## Multiple R-squared: 0.7441, Adjusted R-squared: 0.74
## F-statistic: 180.3 on 3 and 186 DF, p-value: < 2.2e-16
The F-statistic from our multiple regression model is 180.3 which is well above the F-table value of 3.949 for 3 regression degrees of freedom and 120 residual degrees of freedom and α=0.01. Similarly, the p-value is strong for the model, and strong for all of the variables except PropMD x TotExp0.06, which is still pretty good (0.0622). Finally, the R2=0.7441 value is quite good. The standard error values for PropMD and PropMD x TotExp could be better.
##5. Forecast LifeExp when PropMD=.03 and TotExp = 14. Does this forecast seem realistic? Why or why not? Our model is LifeExp4.6=−724418697+(47273338389∗PropMD)+(604795792∗TotExp0.06)−(21214671638∗PropMD∗TotExp0.06)
le.transformed_mutiple <- function(propmd, totexp) {
(-724418697 + (47273338389 * propmd) + (604795792 * totexp) - (21214671638 * propmd * totexp))^(1/4.6)
}
pmd <- 0.03
te <- 14
te4.6 <- te^0.06
le.transformed_mutiple(pmd, te4.6)
## [1] 82.56958
The PropMD ratio being used for the prediction is only in-line with a few outlying data points. If we plot PropMD against Life Expectancy, we can see the overwhelming majority of data points are between 0.000 and 0.005. Then we see two major outlier data points around 0.325 and 0,035. I always get nervous when predicting values that are not in the range of the values for an overwhelming percentage of observations. The Total Expenditure is also towards the bottom end of the range, though a lot of values exist in that range. plot(who\(PropMD, who\)LifeExp, xlab = “Proportion MDs”, ylab = “Life Expectancy”)