library(tidyverse)
library(data.table)
data <- fread('https://raw.githubusercontent.com/folushoa/Data-Science/Data-605/HW12/who.csv')
print(head(data, 5))
##        Country LifeExp InfantSurvival Under5Survival  TBFree      PropMD
##         <char>   <int>          <num>          <num>   <num>       <num>
## 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
##         PropRN PersExp GovtExp TotExp
##          <num>   <int>   <int>  <int>
## 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
# Data dimension
dim(data)
## [1] 190  10
# Data structure
str(data)
## Classes 'data.table' and 'data.frame':   190 obs. of  10 variables:
##  $ Country       : chr  "Afghanistan" "Albania" "Algeria" "Andorra" ...
##  $ LifeExp       : int  42 71 71 82 41 73 75 69 82 80 ...
##  $ InfantSurvival: num  0.835 0.985 0.967 0.997 0.846 0.99 0.986 0.979 0.995 0.996 ...
##  $ Under5Survival: num  0.743 0.983 0.962 0.996 0.74 0.989 0.983 0.976 0.994 0.996 ...
##  $ TBFree        : num  0.998 1 0.999 1 0.997 ...
##  $ PropMD        : num  2.29e-04 1.14e-03 1.06e-03 3.30e-03 7.04e-05 ...
##  $ PropRN        : num  0.000572 0.004614 0.002091 0.0035 0.001146 ...
##  $ PersExp       : int  20 169 108 2589 36 503 484 88 3181 3788 ...
##  $ GovtExp       : int  92 3128 5184 169725 1620 12543 19170 1856 187616 189354 ...
##  $ TotExp        : int  112 3297 5292 172314 1656 13046 19654 1944 190797 193142 ...
##  - attr(*, ".internal.selfref")=<externalptr>
# Statistical summary
summary(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

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.

Question 1

Scatter Plot

data %>% ggplot(aes(x = TotExp, y = LifeExp)) +
  geom_point() +
  labs(title = 'Life Expectancy vs Total Expenditure',
       x = 'Total Personal and Government Expenditure',
       y = 'Life Expectancy (yrs)')

#### Linear Rigression

data_lm <- lm(LifeExp ~ TotExp, data = data)
summary(data_lm)
## 
## Call:
## lm(formula = LifeExp ~ TotExp, data = 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

The F-statistic of the linear regression is 65.26. This is significantly high, meaning that there is a significant relationship between the two variables in the model.
The p-value is 7.71e-14, which is very low. This indicates that in this model, TotExp is significantly relevant to the model.
The multiple R-squared is 0.2577. This is saying that the model only accounts for 25% of the variability of the life expectancy can be explained by the total expenditure on healthcare. This isn’t very high, which means that although there is a significant relationship between the two variables, the total expenditure is not enough to explain the variability in life expectancy.
The standard error is 9.371. Which means that using this model 64% of the life expectancy falls between approximately ±9 years.

Question 2

Scatter Plot

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).

data$LifeExp <- data$LifeExp^4.6
data$TotExp <- data$TotExp^0.06

data %>% ggplot(aes(x = TotExp, y = LifeExp)) +
  geom_point() +
  labs(title = 'Life Expectancy vs Total Expenditure',
       x = 'Total Personal and Government Expenditure',
       y = 'Life Expectancy (yrs)')

#### Linear Rigression

data_lm <- lm(LifeExp ~ TotExp, data = data)
summary(data_lm)
## 
## Call:
## lm(formula = LifeExp ~ TotExp, data = 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 ***
## TotExp       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 of the linear regression is 507.7. This is significantly high, meaning that there is a significant relationship between the two variables in the model.
The p-value is 2.2e-16, which is very low. This indicates that in this model, TotExp is significantly relevant to the model.
The multiple R-squared is 0.7298. This is saying that the model only accounts for 73% of the variability of the life expectancy can be explained by the total expenditure on healthcare. This is significantly high.
The standard error is 90490000. Which means that using this model 64% of the life expectancy falls between approximately ±90490000 years.

Comparing the first model (model in question 1) and the second model (model in question 2), the model in the second model is better. This is because the value of the multiple R-square value and the standard error value from the second model is higher than the multiple R-square value and standard error value from the first model

Question 3

intercept <- -736527910
coefficient <- 620060216

# totExp_0.06 = 1.5
totExp_0.06 <- 1.5
forecast <- (intercept + coefficient * totExp_0.06)^(1/4.6)

paste("When total expenditure raised to the power 0.06 is 1.5, life expectancy is forecasted to be", forecast)
## [1] "When total expenditure raised to the power 0.06 is 1.5, life expectancy is forecasted to be 63.3115334478635"
# totExp_0.06 = 2.5
totExp_0.06 <- 2.5
forecast <- (intercept + coefficient * totExp_0.06)^(1/4.6)

paste("When total expenditure raised to the power 0.06 is 1.5, life expectancy is forecasted to be", forecast)
## [1] "When total expenditure raised to the power 0.06 is 1.5, life expectancy is forecasted to be 86.5064484928337"

Question 4

# original data
data <- fread('https://raw.githubusercontent.com/folushoa/Data-Science/Data-605/HW12/who.csv')

data_mlm <- lm(LifeExp ~ PropMD + TotExp + (PropMD * TotExp), data = data)
summary(data_mlm)
## 
## Call:
## lm(formula = LifeExp ~ PropMD + TotExp + (PropMD * TotExp), data = 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 ***
## 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

The F-statistic of the linear regression is 34.49 with a F-statistic p-value of 2.2e-16. This indicates that there is a significant relationship between the predictors and response variables in the model, that is the probability that you would see these results if the predictors had no effect is extremely low.
The p-value of the predictors are all very low; PropMD: 2.32e-07
TotExp: 9.39e-14
PropMD*TotExp: 6.35e-05
This indicates that in this model, that the predictors of this model are all significantly relevant to the model.
The multiple R-squared is 0.3574. This is saying that the model only accounts for 36% of the variability of the life expectancy can be explained by the total expenditure on healthcare. This is a moderate value.
The standard error is 8.765. Which means that using this model 64% of the life expectancy falls between approximately ±9 years.

Based on the F-statistic, this model is only moderately good. The addition of more predictors might increase the effectiveness of the model.

Question 5

intercept <- 6.277e+01
coefficient_1 <- 1.497e+03
coefficient_2 <- 7.233e-05
coefficient_3 <- -6.026e-03

propMd <- 0.03
totExp <- 14
propMd_totExp <- propMd * totExp
forecast <- intercept + coefficient_1 * propMd + coefficient_2 * totExp + coefficient_3 * propMd_totExp

paste("When proportion of the population who are MDs is 0.03 and total expenditure is 14, life expectancy is forecasted to be", forecast)
## [1] "When proportion of the population who are MDs is 0.03 and total expenditure is 14, life expectancy is forecasted to be 107.6784817"

The life expectancy forecasted is ≈ 108 yrs. Although high this is realistic. The oldest person alive in the U.S. is 114 years old.
Source: https://www.nbcnews.com/news/nbcblk/americas-oldest-living-person-114-may-also-fifth-oldest-person-earth-rcna141190