Regression Models
Problem Statement
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.
LOad Packages
library(ggplot2)
library(tidyverse)
library(knitr)
library(data.table)Before answering the questions, let’s read the who.csv, and make some basic observations.
who_csv <- read.csv('./who.csv')
head(who_csv)## 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
dim(who_csv)## [1] 190 10
So, we verified that there are indeed 10 columns, and 190 rows of data.
Question 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.
Answer:
life_expectancy <- lm(who_csv$LifeExp ~ who_csv$TotExp)
plot(who_csv$LifeExp ~ who_csv$TotExp, xlab="sum of personal and government expenditures", ylab="average life expectancy for the country in years",
main="Life Expectancy vs sum of personal and government expenditures", col = "blue")
abline(life_expectancy)summary(life_expectancy)##
## Call:
## lm(formula = who_csv$LifeExp ~ who_csv$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_csv$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
We observe in the summary that:
F-statistic = 65.26
R^2 = 0.2577, which accounts for 25.77% of the variation in data.
A cardinal assumption of linear regression is linearity of data. We observe that the scatter points, after ascending vertically upwards, turns to the right, and proceeds in linear manner.
So, while the horizontal component is linear, the entire plot is not linear. So, the assumption is partially met.
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?”
Answer:
LifeExp4.6 <- (who_csv$LifeExp)^(4.6)
TotExp0.06 <- (who_csv$TotExp)^(0.06)
life_expectancy_power <- lm(LifeExp4.6 ~ TotExp0.06)
plot(LifeExp4.6, TotExp0.06, xlab="sum of personal and government expenditures ^4.6", ylab="average life expectancy for the country in years ^0.06",
main="Life Expectancy vs sum of personal and government expenditures", col = "blue")summary(life_expectancy_power)##
## Call:
## lm(formula = LifeExp4.6 ~ TotExp0.06)
##
## 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
F-statistic = 507.7 is better than previous value of 65.26.
R^2 = 0.7298 is better than previous value of 0.2577.
p-value < 2.2e-16 is also better.
standard errors are 46817945 & 27518940
So, all in all the second model is better than the previous one.
Question 3:
Using the results from 3, forecast life expectancy when TotExp^.06 =1.5. Then forecast life expectancy when TotExp^.06=2.5.
Answer:
predict.df <- data.frame(TotExp0.06 = c(1.5,2.5))
predict(life_expectancy_power, predict.df, interval = "predict")^(1/4.6)## fit lwr upr
## 1 63.31153 35.93545 73.00793
## 2 86.50645 81.80643 90.43414
So, for 1.5 units of expenditure, the life expectancy = 63.31153, with 95% confidence interval between 35.93545 and 73.00793. And for 2.5 units of expenditure, the life expectancy = 86.50645, with 95% confidence interval between 81.80643 and 90.43414.
Predictions can also be directly computed with the equation of the regression line as follows:
Equation of regression line is: expectancy^4.6 = (-736527909 + 620060216 * spending)
Now, plugging in 1.5 and 2.5 for spending, we get:
(-736527909 + 620060216 * 1.5)^(1/4.6)## [1] 63.31153
(-736527909 + 620060216 * 2.5)^(1/4.6)## [1] 86.50645
The results of both methods of computation do match.
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 = b0+b1 x PropMd + b2 x TotExp +b3 x PropMD x TotExp
Answer:
It’s not stated, which one I should pick (question 1 or question 2). So, I am choosing input from question 2, for multi regression model.
life_expectancy_mulreg2 <- lm(LifeExp4.6 ~ who_csv$PropMD + TotExp0.06 + who_csv$PropMD:TotExp0.06)
summary(life_expectancy_mulreg2)##
## Call:
## lm(formula = LifeExp4.6 ~ who_csv$PropMD + TotExp0.06 + who_csv$PropMD: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_csv$PropMD 4.727e+10 2.258e+10 2.094 0.0376 *
## TotExp0.06 6.048e+08 3.023e+07 20.005 <2e-16 ***
## who_csv$PropMD: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
F-statistic = 180.3
R^2 = 0.7441, which accounts for 74.41% of variability.
p-value: < 2.2e-16.
I ran the summary, with input from question 1. The model is not as good as the second one.
Question 5:
Forecast LifeExp when PropMD=.03 and TotExp = 14. Does this forecast seem realistic? Why or why not?
Answer:
In this question, I am choosing input from question 1.
life_expectancy_mulreg1 <- lm(LifeExp ~ PropMD + TotExp + TotExp:PropMD, data=who_csv)
longivity.df <- data.frame(PropMD = 0.03, TotExp = 14)
predict(life_expectancy_mulreg1, longivity.df, interval = "predict")## fit lwr upr
## 1 107.696 84.24791 131.1441
The predicted longivity is 107.696, with 95% confidence interval between 84.24791 and 131.1441 107.696 years, with a total expenditure of $14 doesn’t seem very realistic. Is it?
Marker: 605-12