We want to focus on ‘age’ and ‘chol’ for this analysis.
heart_data <- read.csv("/Users/joe/Downloads/heart.csv")
summary(heart_data)
## age sex cp trtbps
## Min. :29.00 Min. :0.0000 Min. :0.000 Min. : 94.0
## 1st Qu.:47.50 1st Qu.:0.0000 1st Qu.:0.000 1st Qu.:120.0
## Median :55.00 Median :1.0000 Median :1.000 Median :130.0
## Mean :54.37 Mean :0.6832 Mean :0.967 Mean :131.6
## 3rd Qu.:61.00 3rd Qu.:1.0000 3rd Qu.:2.000 3rd Qu.:140.0
## Max. :77.00 Max. :1.0000 Max. :3.000 Max. :200.0
## chol fbs restecg thalachh
## Min. :126.0 Min. :0.0000 Min. :0.0000 Min. : 71.0
## 1st Qu.:211.0 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:133.5
## Median :240.0 Median :0.0000 Median :1.0000 Median :153.0
## Mean :246.3 Mean :0.1485 Mean :0.5281 Mean :149.6
## 3rd Qu.:274.5 3rd Qu.:0.0000 3rd Qu.:1.0000 3rd Qu.:166.0
## Max. :564.0 Max. :1.0000 Max. :2.0000 Max. :202.0
## exng oldpeak slp caa
## Min. :0.0000 Min. :0.00 Min. :0.000 Min. :0.0000
## 1st Qu.:0.0000 1st Qu.:0.00 1st Qu.:1.000 1st Qu.:0.0000
## Median :0.0000 Median :0.80 Median :1.000 Median :0.0000
## Mean :0.3267 Mean :1.04 Mean :1.399 Mean :0.7294
## 3rd Qu.:1.0000 3rd Qu.:1.60 3rd Qu.:2.000 3rd Qu.:1.0000
## Max. :1.0000 Max. :6.20 Max. :2.000 Max. :4.0000
## thall output
## Min. :0.000 Min. :0.0000
## 1st Qu.:2.000 1st Qu.:0.0000
## Median :2.000 Median :1.0000
## Mean :2.314 Mean :0.5446
## 3rd Qu.:3.000 3rd Qu.:1.0000
## Max. :3.000 Max. :1.0000
Looking at the Residuals, the Q-Q Residuals displays a relatively straight line, with a minor vertial upward tick at the right end of the Q-Q Residual with number 86. And of course,the outlier is seen there as well, which would be higher above the line.
lmscores <- lm(`chol`~`age`, data = heart_data)
plot(lmscores)
A quick snapshot of our data, Age and Cholesterol.The max cholesterol is a whopping 564, however I chose to leave it in the dataframe, just to see how the model would react.
heart_age <- heart_data[['age']]
summary(heart_age)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 29.00 47.50 55.00 54.37 61.00 77.00
chol_heart <- heart_data[['chol']]
summary(chol_heart)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 126.0 211.0 240.0 246.3 274.5 564.0
The cholesterol clearly gets higher the older we become. There is one outlier, but it hasn’t affected our overall conclusion.
ggplot(heart_data, aes(x = age, y = chol))+
geom_point() +
geom_smooth(method = "lm", se = FALSE)+
labs( x = "Age", y = "Cholesterol", title = "Cholesterol vs Age")
## `geom_smooth()` using formula = 'y ~ x'
The slop is 1.2194. The p-value is 0.0001786, which means the line gives us a significant increase in cholesterol as age is increased, even though it had an outlier way above the ‘normal’ range. I think it’s a good graph to understand how much cholesterol we should be at given our age.
model <- lm(chol~ age, data = heart_data)
summary(model)
##
## Call:
## lm(formula = chol ~ age, data = heart_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -123.476 -32.560 -5.745 28.024 302.330
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 179.9675 17.7116 10.161 < 2e-16 ***
## age 1.2194 0.3213 3.795 0.000179 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 50.72 on 301 degrees of freedom
## Multiple R-squared: 0.04566, Adjusted R-squared: 0.04249
## F-statistic: 14.4 on 1 and 301 DF, p-value: 0.0001786