# Load the packages needed
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.2
##
## 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
library(broom)
## Warning: package 'broom' was built under R version 4.2.2
library(ggpubr)
## Warning: package 'ggpubr' was built under R version 4.2.2
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.
who <- read.csv('https://raw.githubusercontent.com/enidroman/Data_605_Fundamentals_of_Computational_Mathematics/main/who.csv')
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
# Plot total expenditures vs life expectancy for who data set
plot(who$TotExp, who$LifeExp, main = "Scatterplot Total Expenditures vs Life Expectancy", xlab = "Total Expenditures", ylab = "Life Expectancy")
# Simple linear regression
who_simple.lm <- lm(LifeExp ~ TotExp, data = who)
# Run summary function to provide the F statistics,
# R-squared, standard error, and p-values
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
F-statistic: 65.26 on 1 and 188 DF, R-squared: 0.2577, Residual standard error: 9.371 on 188 degrees of freedom, p-value: 7.714e-14
For the simple linear regression model between Total Expenditure and Life Expectancy:
In comparing the p-value for the F-test to your significance level. If the p-value associated with the F-statistic is ≥ 0.05: Then there is no relationship between any of the independent variables and the model. The F-statistic suggests we can reject the null hypothesis
https://statisticsbyjim.com/regression/interpret-f-test-overall-significance-regression/
https://quantifyinghealth.com/f-statistic-in-linear-regression/
R-squared is always between 0 and 100%:
0% represents a model that does not explain any of the variation in the response variable around its mean. The mean of the dependent variable predicts the dependent variable as well as the regression model.
100% represents a model that explains all the variation in the response variable around its mean.
The greater R-square the better the model.
In this case the R-squared is at 0.2577, the model explains 25% of variation within the data. This is quite low and not a great fit for this model.
https://statisticsbyjim.com/regression/interpret-r-squared-regression/
https://medium.com/@faisalshahbaz/relationship-between-r-squared-and-p-value-in-a-regression-2378b1e2e9ce#:~:text=The%20greater%20R%2Dsquare%20the,model%20fits%20the%20data%20well.
The standard error on average, the Total Expense score is 9.371 points away from the regression model based on the independent variables. The smaller the value of the standard error of the estimate, the better the fit of the regression model to the data. This standard error is a reasonably small percentage of the coefficient.
https://ecampusontario.pressbooks.pub/introstats/chapter/13-3-standard-error-of-the-estimate/#:~:text=The%20standard%20error%20of%20the%20estimate%2C%20se%20%2C%20measures%20the,regression%20model%20to%20the%20data.
If the P-value of 7.714e-14 is lower than 0.05, we can reject the null hypothesis and conclude that it exist a relationship between the variables. So, we can conclude there is a strong relationship between the Total Expenditure and Life Expectancy.
https://www.w3schools.com/datascience/ds_linear_regression_pvalue.asp
#Scatterplot with model
plot(who$TotExp, who$LifeExp, main = "Scatterplot Total Expenditures vs Life Expectancy with Simple Linear Regression", xlab = "Total Expenditures", ylab = "Life Expectancy")
abline(who_simple.lm$coefficients, col = "red")
In order to find out if the assumptions of simple linear regression has been met we must make several assumptions about the data, such as :
Linearity of the data. The relationship between the predictor (x) and the outcome (y) is assumed to be linear. Normality of residuals. The residual errors are assumed to be normally distributed. Homogeneity of residuals variance. The residuals are assumed to have a constant variance (homoscedasticity) Independence of residuals error terms.
par(mfrow=c(2,2))
plot(who_simple.lm)
The assumtions of a linear model are not met. The scatterplot of the two variables is not linear. The fitted vs residuals plot shows points not uniformly scattered about 0 line and as such does not indicate linearity and homoscedasticity. The qq plot shows that the distribution is not normal and the variables are not linear.
# Create new column with LifeExp^4.6 values
who$LifeExp4.6 <- (who$LifeExp)^4.6
# Create new column with TotExp^4.6 values
who$TotExp0.06 <- (who$TotExp)^0.06
# Plot total expenditures vs life expectancy for who data set
plot(who$TotExp0.06, who$LifeExp4.6, main = "Scatterplot Total Expenditures vs Life Expectancy (Transformed Variables)", xlab = "Total Expenditures ^ 0.06", ylab = "Life Expectancy ^ 4.6")
#Simple Regression LifeExp4.6~TotExp0.06
who_transform.lm <-lm(LifeExp4.6 ~ TotExp0.06, data=who)
summary(who_transform.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
F-statistic: 507.7 on 1 and 188 DF, R-squared: 0.7298, Residual standard error: 90490000 on 188 degrees of freedom, p-value: 2.2e16
For the simple linear regression model between Total Expenditure and Life Expectancy(Transformed):
Compare the p-value of 2.2e16 for the F-test to your significance level of 507.7. If the p-value is less than the significance level, your sample data provide sufficient evidence to conclude that your regression model fits the data better than the model with no independent variables. This finding is good news because it means that the independent variables in your model improve the fit!
In this case the R-squared is at 0.7298, the model explains 73% of variation within the data. This is higher then before and is a better fit for this model.
The standard error on average, the Total Expense score is 90490000 points away from the regression model based on the independent variables. The smaller the value of the standard error of the estimate, the better the fit of the regression model to the data. This standard error is quite large percentage of the coefficient.
If the P-value of 2.2e16 is lower than 0.05, we can reject the null hypothesis and conclude that it exist a relationship between the variables. So, we can conclude there is a strong relationship between the Total Expenditure and Life Expectancy.
This transformed model is much better than the original, non-transformed model. However this model has a higher standard error given the transformations.
#Scatterplot with transformed data model
plot(who$LifeExp4.6~who$TotExp0.06)
abline(who_transform.lm$coefficients, col = "red")
# Create formula for model
le.transformed <- function(totexp) {
(-736527910 + (620060216 * totexp))^(1/4.6)
}
# pass in TotExp^.06 = 1.5
le.transformed(1.5)
## [1] 63.31153
# pass in TotExp^.06 = 2.5
le.transformed(2.5)
## [1] 86.50645
LifeExp = b0+b1 x PropMd + b2 x TotExp +b3 x PropMD x TotExp
# Build multiple regression model using Life Expendinture, PropMD, Total Expendinture, PropMD times Total Expendinture.
who_multiple.lm <- lm(LifeExp ~ PropMD + TotExp + PropMD * TotExp, data=who)
# Run summary function to provide the F statistics,
# R-squared, standard error, and p-values
summary(who_multiple.lm)
##
## Call:
## lm(formula = LifeExp ~ PropMD + TotExp + PropMD * TotExp, data = who)
##
## 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
F-statistic: 34.49 on 3 and 186 DF, R-squared: 0.3574, Residual standard error: 8.765 on 186 degrees of freedom, p-value: 2.2e16
For the simple multiple linear regression model between Life Expendinture, PropMD, Total Expendinture, PropMD times Total Expendinture:
Compare the p-value of 2.2e16 for the F-test to your significance level of 34.49. If the p-value is less than the significance level, your sample data provide sufficient evidence to conclude that your regression model fits the data better than the model with no independent variables. This finding is good news because it means that the independent variables in your model improve the fit!
In this case the R-squared is at 0.3574, the model explains 35% of variation within the data. This is lower then the previous transformed model and is quite low so is not a better fit for this model.
The standard error on average, the Total Expense score is 8.765 points away from the regression model based on the independent variables. The smaller the value of the standard error of the estimate, the better the fit of the regression model to the data. This standard error is a reasonably small percentage of the coefficient.
If the P-value of 2.2e16 is lower than 0.05, we can reject the null hypothesis and conclude that it exist a relationship between the variables. So, we can conclude there is a strong relationship between the Total Expenditure and Life Expectancy.
This transformed model is much better than the original, non-transformed model. However this model has a higher standard error given the transformations.
This model is not good as the origional model, It has a low R-squared and the scatter plot shows that it is non-linear and does not fit the line. While it is a little better than the original model, it performs worst than the transformed model.
#Scatterplot of multiple regression model
plot(who$LifeExp ~ predict(who_multiple.lm, who))
abline(0,1, col = "red")
predict(who_multiple.lm, data.frame(PropMD = 0.03, TotExp = 14))
## 1
## 107.696
This forecast for life expectancy of 107 years old is unrealistic. Just improving on the PropMD and TotExp will not extend life expectancy in reality.