library(RCurl)
library(ggplot2)
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
The attached who.csv dataset contains real-world data from 2008. The variables included follow.
x <- getURL("https://raw.githubusercontent.com/ChristopherBloome/Misc/main/who.csv")
RawData <- read.csv(text = x)
head(RawData)
## 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
RawData %>%
ggplot(aes(x=TotExp, y=LifeExp)) + geom_point()
RD_LM <- lm(data = RawData, LifeExp ~ TotExp)
summary(RD_LM)
##
## Call:
## lm(formula = LifeExp ~ TotExp, data = RawData)
##
## 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
As we see above, the p-value associated with the F-Statistic is 7.714 * 10^-14, very small and significantly significant. #### R^2 The R Squared is .25, This implies that a quarter of the variation can be explained by our independent variable. #### standard error With a standard error of 9.37, we know that the average distance away from the regression line is 9.3%, and that 95% of our observations are between +/- 2 x 9.3% #### P-Values Our one P-Value for TotExp is 7.71 x 10^-14, very significant. We note this is the same as the P-Value associated with our F Statistic, as this model only has one variable.
Overall, we know that there is a correlation between our dependent and independent variable, but this alone does not account for all the variation in our model.
AdjData <-data.frame(RawData$LifeExp^4.6, RawData$TotExp^.06)
names(AdjData) <- c("AdjLifeExp","AdjTotExp")
AdjData %>%
ggplot(aes(x=AdjTotExp, y=AdjLifeExp)) + geom_point()
AdjLM <- lm(data = AdjData, AdjLifeExp ~ AdjTotExp)
summary(AdjLM)
##
## Call:
## lm(formula = AdjLifeExp ~ AdjTotExp, data = AdjData)
##
## 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 ***
## AdjTotExp 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
Just as with the un-adjusted table, the F-Statistic is very small, implying the model is predictive. #### R^2 The R Squared is .73, implying nearly 75% of the variation in the dependent variable can be explained by the independent variable. #### standard error This standard error is significantly higher than in the previous model, due to the fact that we have transformed our dependent variable. #### P-Values Our one P-Value for TotExp are very very small - a quick Google search demonstrated that these are the smallest values R will show.
In all, this model is significantly better.
B <- AdjLM$coefficients[[1]]
M <- AdjLM$coefficients[[2]]
(M*1.5 + B)^(1/4.6)
## [1] 63.31153
(M*2.5 + B)^(1/4.6)
## [1] 86.50645
LM2 <- lm(data = RawData, LifeExp ~ PropMD + TotExp + PropMD*TotExp)
summary(LM2)
##
## Call:
## lm(formula = LifeExp ~ PropMD + TotExp + PropMD * TotExp, data = RawData)
##
## 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 ***
## 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
While each of the PValues is low enough to imply significance, the RSqured is nowhere near as great as the adjusted model from Question 2.
B0 <- LM2$coefficients[[1]]
B1 <- LM2$coefficients[[2]]
B2 <- LM2$coefficients[[3]]
B3 <- LM2$coefficients[[4]]
B0 + .03*B1 + 14*B2 + .03*14*B3
## [1] 107.696
107 years is a long time, so at first glance this seems high. Looking at the data, it is clear that both values are at the extreme of their ranges, which confirms our suspician this value is not realistic.