library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
med_data <- read.csv("who.csv", header = TRUE)
summary(med_data)
## Country LifeExp InfantSurvival Under5Survival
## Length:190 Min. :40.00 Min. :0.8350 Min. :0.7310
## Class :character 1st Qu.:61.25 1st Qu.:0.9433 1st Qu.:0.9253
## Mode :character Median :70.00 Median :0.9785 Median :0.9745
## Mean :67.38 Mean :0.9624 Mean :0.9459
## 3rd Qu.:75.00 3rd Qu.:0.9910 3rd Qu.:0.9900
## Max. :83.00 Max. :0.9980 Max. :0.9970
## TBFree PropMD PropRN PersExp
## Min. :0.9870 Min. :0.0000196 Min. :0.0000883 Min. : 3.00
## 1st Qu.:0.9969 1st Qu.:0.0002444 1st Qu.:0.0008455 1st Qu.: 36.25
## Median :0.9992 Median :0.0010474 Median :0.0027584 Median : 199.50
## Mean :0.9980 Mean :0.0017954 Mean :0.0041336 Mean : 742.00
## 3rd Qu.:0.9998 3rd Qu.:0.0024584 3rd Qu.:0.0057164 3rd Qu.: 515.25
## Max. :1.0000 Max. :0.0351290 Max. :0.0708387 Max. :6350.00
## GovtExp TotExp
## Min. : 10.0 Min. : 13
## 1st Qu.: 559.5 1st Qu.: 584
## Median : 5385.0 Median : 5541
## Mean : 40953.5 Mean : 41696
## 3rd Qu.: 25680.2 3rd Qu.: 26331
## Max. :476420.0 Max. :482750
# Build a linear model for stopping distance as a function of speed
model <- lm(LifeExp ~ TotExp, data = med_data)
summary(model)
##
## Call:
## lm(formula = LifeExp ~ TotExp, data = med_data)
##
## 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
Looking at the summary, the R-Square of 0.2577 indicates that ~25% of variation in life expectancy can be attributed to the Total Expenses. In addition, the standard error for TotExp is small.
Analyzing the F-Statistic & p-value, we can test the null hypothesis. Since the p-value is less than 0.05, we can reject the null hypothesis, and conclude the model is significant.
plot(LifeExp ~ TotExp, data = med_data, main = "Life Expectancy vs Healthcare Expenditures", xlab = "Total Healthcare Expenses (Per Person)", ylab = "Life Expectancy")
abline(model, col = "blue")
Looking at the data, its clear that there is a wide amount of variability in healthcare costs to outcomes. I find it fascinating to see that there is a an asymptote at approximately 80 years old, and I wonder how much genetics & lifestyle of those countries play into that value.
par(mfrow = c(2, 2))
plot(model, which = 1:4)
Practically, we look for 4 key parts to analyze if a regression is reasonable.
Independence-This is assumed There is a linear relationship, which I
don’t feel is solidily demonstrated in the scatter plot above, as to me
it looks like this is a reciprocal function.
There is constant variance, which is shown by the uniform spread of
residuals. There is a normality of Residuals across a reasonable range,
which is not demonstrated by the chart above.
Also just looking at the normal qq plot, the curve would indicate the sample data is skewed.
Right off the bat, I’m going to say its probably going to get better, but lets check it:
# Build a linear model for stopping distance as a function of speed
model <- lm(I(LifeExp^4.6) ~ I(TotExp^0.06), data = med_data)
summary(model)
##
## Call:
## lm(formula = I(LifeExp^4.6) ~ I(TotExp^0.06), data = med_data)
##
## 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
Looking at the summary, the R-Square significantly improved, from 0.2577 to 0.7298 indicates that significantly more of the variation in life expectancy can be attributed to the Total Expenses. In addition, the standard error for TotExp is small.
Analyzing the F-Statistic & p-value, we can test the null hypothesis. Since the p-value is less than 0.05, we can reject the null hypothesis, and conclude the model is significant.
plot(I(LifeExp^4.6) ~ I(TotExp^0.06), data = med_data, main = "Life Expectancy vs Healthcare Expenditures", xlab = "Total Healthcare Expenses (Per Person)", ylab = "Life Expectancy^4.6")
abline(model, col = "blue")
Honestly, adding in these new exponents appeared to do an effective job of spreading and quashing the data, or make it behave in a more linear fashion.
par(mfrow = c(2, 2))
plot(model, which = 1:4)
Practically, we look for 4 key parts to analyze if a regression is reasonable.
Independence-This is assumed There is some variance, as the chart of residuals vs fitted is still skewed, but it is a significant improvement over the prior chart. There is a normality of Residuals across a reasonable range, which is somewhat demonstrated by the chart above. Finally, the normal qq plot has its lower tails deviate from linear, indicating that there are some extreme values. This observation is further reinforced by the chart of Cook’s distance.
Using the results from 3, forecast life expectancy when TotExp^.06 =1.5. Then forecast life expectancy when TotExp^.06=2.5
predict(model, newdata = data.frame(TotExp = 1.5^(1/0.06)))^(1/4.6)
## 1
## 63.31153
predict(model, newdata = data.frame(TotExp = 2.5^(1/0.06)))^(1/4.6)
## 1
## 86.50645
model <- lm(LifeExp ~ I(TotExp * PropMD) + TotExp + PropMD, data = med_data)
summary(model)
##
## Call:
## lm(formula = LifeExp ~ I(TotExp * PropMD) + TotExp + PropMD,
## data = med_data)
##
## 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 ***
## I(TotExp * PropMD) -6.026e-03 1.472e-03 -4.093 6.35e-05 ***
## TotExp 7.233e-05 8.982e-06 8.053 9.39e-14 ***
## PropMD 1.497e+03 2.788e+02 5.371 2.32e-07 ***
## ---
## 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
par(mfrow = c(2, 2))
plot(model, which = 1:4)
Firstly, we look for 3 key parts to analyze if a regression is reasonable.
Independence-This is assumed There is constant variance, which is shown by the uniform spread of residuals. There is a normality of Residuals across a reasonable range, which is not demonstrated by the chart above.
Also just looking at the normal qq plot, the curve would indicate the sample data is skewed.
Looking at the summary, the R-Square decreased, from 0.7298 to 0.3574. indicates that significantly more of the variation in life expectancy can be attributed to the Total Expenses rather than a combination of expenses and proportion of population that is doctors. In addition, the standard error for TotExp is small.
Analyzing the F-Statistic & p-value, we can test the null hypothesis. Since the p-value is less than 0.05, we can reject the null hypothesis, and conclude the model is significant, although not as good as a fit as the prior models.
Also just looking at the normal qq plot, the curve and the deviation of the tails would indicate the sample data is skewed, but it is not as bad as prior iterations.
predict(model, newdata = data.frame(I(14 * 0.03), TotExp = 14, PropMD=0.03))
## 1
## 107.696
No this does not seem realistic as the expected age is 107, which is super unrealistic. As much as healthcare cost influences age, there are a slew of other factors, ranging from genetics to technology that will impact how long someone lives. Just from the technological standpoint, think of how much medicine has changed since the 1900s!
All in all, its a great predictor that indicates we shold try and improve the PropMD, but at the same time, throwing money at a problem doesn’t solve it.