rm(list=ls()) ##Life Expectancy (WHO) ##https://www.kaggle.com/datasets/kumarajarshi/life-expectancy-who?ref=hackernoon.com ## Linear and Loess estimation for ## Life Expct. vs %Expenditure on Health (PE) D = read.csv("WHO_Life_expectancy.csv",header=TRUE) D = D[D$Year == 2007,] D = D[,c(1,4,7,8,17,22)] D = na.omit(D) Country = D[,1] D = D[,-1] attach(D) n = nrow(D) pdf("WHOLinear1.pdf") lifeExp = Life.expectancy PE = percentage.expenditure plot(PE,lifeExp,xlab="",ylab="", pch=21,cex=2.2,bg="pink",cex.axis=1.5) out = lm(lifeExp ~ PE) abline(out,col="purple",lwd=4) LL1 = loess(lifeExp ~ PE) o=order(PE) lines(PE[o],LL1$fitted[o],col="red",lwd=4) title(ylab="Life Expectancy",line=2, cex.lab=2) title(xlab="Percent. Health Expend. (PE)",line=2.6, cex.lab=2) dev.off() logPE = log(PE) pdf("WHOLinear2.pdf") plot(logPE,lifeExp,xlab="",ylab="", pch=21,cex=2.2,bg="pink",cex.axis=1.5) out = lm(lifeExp ~ logPE) abline(out,col="purple",lwd=4) LL2 = loess(lifeExp ~ logPE) o=order(logPE) lines(logPE[o],LL2$fitted[o],col="red",lwd=4) title(ylab="Life Expectancy",line=2, cex.lab=2) title(xlab="log(PE)",line=2.6, cex.lab=2) dev.off() #### Estimates and confidence intervals for the four covariates logSchooling = log(Schooling) logGDP = log(GDP) outout = lm(lifeExp ~ Alcohol + logPE + logSchooling + logGDP) summary(outout) ## Call: ## ## outout = lm(lifeExp~Alcohol+logPE+logSchooling+logGDP) ## summary(outout) ## ## Call: ## lm(formula = lifeExp ~ Alcohol + logPE + logGDP + logSchooling) ## ## Residuals: ## Min 1Q Median 3Q Max ## -18.6711 -2.1579 0.2749 3.8964 15.1584 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 9.9925 4.9812 2.006 0.0466 * ## Alcohol -0.2968 0.1512 -1.963 0.0515 . ## logPE 1.1062 0.6699 1.651 0.1008 ## logGDP -0.1052 0.7724 -0.136 0.8919 ## logSchooling 22.8483 2.3185 9.855 <2e-16 *** ## --- ## Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 ## ## Residual standard error: 6.119 on 151 degrees of freedom ## Multiple R-squared: 0.6141, Adjusted R-squared: 0.6039 ## F-statistic: 60.07 on 4 and 151 DF, p-value: < 2.2e-16 print(confint(outout)) ## 2.5 % 97.5 % ## (Intercept) 0.1505836 19.834316992 ## Alcohol -0.5955614 0.001956565 ## logPE -0.2173624 2.429732098 ## logGDP -1.6312304 1.420914581 ## logSchooling 18.2673598 27.429274189 ####### ####### WHO ANOVA with 4 covariates ####### TableAOV = aov(Life.expectancy ~ Alcohol + logPE + logGDP + logSchooling) summary(TableAOV) ## Df Sum Sq Mean Sq F value Pr(>F) ## Alcohol 1 2317 2317 61.89 6.47e-13 *** ## logPE 1 2825 2825 75.45 5.72e-15 *** ## logGDP 1 218 218 5.82 0.017 * ## logSchooling 1 3636 3636 97.11 < 2e-16 *** ## Residuals 151 5654 37 ## --- ## Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 ## SS_regr = 2317+2825+218+3636 = 8966 ## SS_tot = 8966+5654 = 14620