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
age <-c(1:19)
SBP <- c(80,103,101,105,111,117,112,116,119,120,118,121,125,129,128,134,131,133,138)
bpstudy <- data.frame(age,SBP)
regression<-lm(SBP~age, data = bpstudy)
summary(regression)
##
## Call:
## lm(formula = SBP ~ age, data = bpstudy)
##
## Residuals:
## Min 1Q Median 3Q Max
## -16.995 -1.596 1.021 2.397 8.365
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 94.6667 2.4885 38.04 < 2e-16 ***
## age 2.3281 0.2183 10.67 5.96e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.211 on 17 degrees of freedom
## Multiple R-squared: 0.87, Adjusted R-squared: 0.8624
## F-statistic: 113.8 on 1 and 17 DF, p-value: 5.964e-09
A t-value of 38.04 provides a strong suspicion that the values are above 0. For the intercept p value I got a very small value that indicates a very strong relationship but for the age variable I got 5.96e-09 which is a large number and indicates there is not a strong relationship. Based on the lecture, the multiple R-squared and Adjusted R-squared value are nearly identical and this value means that 87% of the variations in SBP can be explained by the age.
library(ggplot2)
ggplot(data=bpstudy, aes(x=age, y=SBP))+
geom_point()+
stat_smooth(method="lm", color="green", fill="orange")
## `geom_smooth()` using formula = 'y ~ x'
confint<-predict(regression, interval='confidence', level=0.95)
regression2<- cbind(bpstudy,confint)
ggplot(data=regression2, aes(x=age, y=SBP))+
geom_point()+
geom_line(aes(y=fit), color="green", size=2) +
geom_line(aes(y=lwr), color="orange", size=2) +
geom_line(aes(y=upr), color="orange", size=2)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
predint<-predict(regression, interval='prediction', level=0.95)
## Warning in predict.lm(regression, interval = "prediction", level = 0.95): predictions on current data refer to _future_ responses
regression3<-cbind(bpstudy,predint)
ggplot(data=regression3, aes(x=age, y=SBP))+
geom_point()+
geom_line(aes(y=fit), color="green", size=2) +
geom_line(aes(y=lwr), color="blue", size=2) +
geom_line(aes(y=upr), color="blue", size=2)
The difference between the confidence interval and the prediction interval is that the confidence interval tells you the 95% confidence of your best fit line and the prediction intervals are the lines that include 95% of your data points. The predication intervals are on the boundary of your data points or the range.
The age with the best prediction is age 3 because it is the closest to the green best fit line. Age 1 has the worst prediction because it is the farthest away from the green best fit line.
ggplot(data=bpstudy, aes(y=SBP))+
geom_boxplot()
Yes, Age 1 has a SBP of 80 which is considered an outlier. The point appears outside of the prediction value line boundaries.
bpstudy1<-filter(bpstudy, age > 1)
regression4<-lm(SBP~age, data = bpstudy1)
summary(regression4)
##
## Call:
## lm(formula = SBP ~ age, data = bpstudy1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.094 -2.021 -0.071 1.684 5.921
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 99.1097 1.3496 73.44 < 2e-16 ***
## age 1.9948 0.1152 17.31 8.75e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.536 on 16 degrees of freedom
## Multiple R-squared: 0.9493, Adjusted R-squared: 0.9462
## F-statistic: 299.7 on 1 and 16 DF, p-value: 8.748e-12
The values in the beta coefficients became stronger predictions. The estimate is now 99 and the R-squared value rose to 95%.
The next questions use the file health-status.csv
library(readr)
health_status <- read_csv("health-status.csv")
## Rows: 31568 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (8): patid, health, age, sex, income, education, alcohol, cigperday
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
regression<-lm(health~patid + age + alcohol + education +cigperday, data = health_status)
summary(regression)
##
## Call:
## lm(formula = health ~ patid + age + alcohol + education + cigperday,
## data = health_status)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.1141 -0.7695 -0.0415 0.6549 3.2587
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.201e+00 7.404e-02 29.728 <2e-16 ***
## patid -7.512e-08 6.083e-07 -0.123 0.902
## age 1.400e-02 3.074e-04 45.541 <2e-16 ***
## alcohol -4.963e-02 2.980e-03 -16.653 <2e-16 ***
## education -9.013e-02 2.323e-03 -38.801 <2e-16 ***
## cigperday 2.627e-02 1.236e-03 21.249 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9777 on 31087 degrees of freedom
## (475 observations deleted due to missingness)
## Multiple R-squared: 0.1399, Adjusted R-squared: 0.1397
## F-statistic: 1011 on 5 and 31087 DF, p-value: < 2.2e-16
The P-value is small and indicate a significant correlation for the variables age, alcohol, education, and cigarettes as it relates to health. Patient ID does not have a significant p-value and suggest it has no meaningful relationship with health. There are variables that would benefit from further exploration such as education because the negative estimate shows an inverse relationship and I think that as education increases so should health.
The result for alcohol consumption agrees with my expectation that as alcohol decreases, health increases. In this data, alcohol is being measured by average number of days per week in the past year in which alcoholic beverages were consumed, from 0 to 7. I propose defining binge drinking and then asking participants to identify the number of binge drinking events in a time period such as a month.
A linear regression model is the best choice for explaining self-rated health in terms of the variables being considered. The very small p-values for age, alcohol, and education, and cigarettes indicate there is a strong association between these variables and health. The greatest difference in t-value is seen with age and cigarettes. The R-squared values are .14 which is fairly low and close to 0 showing poor quality in the fitness of the relationship. This could be due to the inclusion of patid.