This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

#load dataset
heart <- read.csv("ClevelandHeart.csv")
#count males
table(heart$Sex)

female   male 
    97    206 
sum(is.na(heart$Thal))
[1] 2
# Histogram
hist(heart$Chol, main="Histogram of Cholesterol", xlab="Cholesterol (mg/dl)", col="skyblue")


# Summary statistics
mean(heart$Chol, na.rm=TRUE)
[1] 246.6931
median(heart$Chol, na.rm=TRUE)
[1] 241
sd(heart$Chol, na.rm=TRUE)
[1] 51.77692
IQR(heart$Chol, na.rm=TRUE)
[1] 64
# 95% CI for mean
t.test(heart$Chol)$conf.int
[1] 240.8397 252.5465
attr(,"conf.level")
[1] 0.95
max(heart$Chol, na.rm=TRUE)
[1] 564
# Fractions
normal <- mean(heart$Chol < 200, na.rm=TRUE)
high <- mean(heart$Chol > 240, na.rm=TRUE)

normal
[1] 0.1617162
high
[1] 0.5016502
plot(heart$Age, heart$MaxHR,
     xlab="Age (years)", ylab="Max Heart Rate",
     main="Age vs MaxHR", pch=19, col="darkgreen")


table(heart$AHD)

 No Yes 
164 139 
# Split
hd_yes <- subset(heart, AHD=="Yes")
hd_no  <- subset(heart, AHD=="No")

boxplot(MaxHR ~ AHD, data=heart,
        xlab="Heart Disease", ylab="MaxHR",
        main="MaxHR by Heart Disease Status", col=c("orange","lightblue"))

qqnorm(hd_yes$MaxHR); qqline(hd_yes$MaxHR, col="red")

qqnorm(hd_no$MaxHR); qqline(hd_no$MaxHR, col="blue")

t.test(MaxHR ~ AHD, data=heart)

    Welch Two Sample t-test

data:  MaxHR by AHD
t = 7.8579, df = 272.27, p-value = 9.106e-14
alternative hypothesis: true difference in means between group No and group Yes is not equal to 0
95 percent confidence interval:
 14.32900 23.90912
sample estimates:
 mean in group No mean in group Yes 
          158.378           139.259 
wilcox.test(MaxHR ~ AHD, data=heart)

    Wilcoxon rank sum test with continuity correction

data:  MaxHR by AHD
W = 16990, p-value = 1.861e-13
alternative hypothesis: true location shift is not equal to 0
boxplot(Chol ~ AHD, data=heart,
        xlab="Heart Disease", ylab="Cholesterol",
        main="Cholesterol by Heart Disease Status", col=c("pink","lightgreen"))

qqnorm(hd_yes$Chol); qqline(hd_yes$Chol, col="red")

qqnorm(hd_no$Chol); qqline(hd_no$Chol, col="blue")

t.test(Chol ~ AHD, data=heart)

    Welch Two Sample t-test

data:  Chol by AHD
t = -1.4924, df = 298.64, p-value = 0.1366
alternative hypothesis: true difference in means between group No and group Yes is not equal to 0
95 percent confidence interval:
 -20.484170   2.815018
sample estimates:
 mean in group No mean in group Yes 
         242.6402          251.4748 
wilcox.test(Chol ~ AHD, data=heart)

    Wilcoxon rank sum test with continuity correction

data:  Chol by AHD
W = 9798.5, p-value = 0.03536
alternative hypothesis: true location shift is not equal to 0

Question 1

There are 206 male patients in the dataset and 2 NAs. ## Question 2 (mean, median, SD, IQR, CI) 246.6931, 241, 51,66, 64, (240.84, 252.44) ## Question 3 564, 0.1617, 0.501 ## Question 4 As age goes down max heart rate goes down slightly but the correlation is light and can barely see anything ##Question 7 There are a few lower outliers byut no larger ones ##Question 10 Both tests agree and say that the true difference in means are not equal to 0 ##Question 15 both tests do agree and say the true difference is not equal to 0

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

LS0tDQp0aXRsZTogIlIgTm90ZWJvb2siDQpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sNCi0tLQ0KDQpUaGlzIGlzIGFuIFtSIE1hcmtkb3duXShodHRwOi8vcm1hcmtkb3duLnJzdHVkaW8uY29tKSBOb3RlYm9vay4gV2hlbiB5b3UgZXhlY3V0ZSBjb2RlIHdpdGhpbiB0aGUgb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