1. Read the Data and inspect it.
  1. Read ClevelandHeart.csv into R and store it in a variable named clev.
  2. Display the first few rows and the structure of the dataset.
head(clev)
str(clev)
'data.frame':   303 obs. of  12 variables:
 $ Age        : int  63 67 67 37 41 56 62 57 63 53 ...
 $ Sex        : chr  "male" "male" "male" "male" ...
 $ ChestPain  : chr  "typical" "asymptomatic" "asymptomatic" "nonanginal" ...
 $ RestBP     : int  145 160 120 130 130 120 140 120 130 140 ...
 $ Chol       : int  233 286 229 250 204 236 268 354 254 203 ...
 $ Fbs        : logi  TRUE FALSE FALSE FALSE FALSE FALSE ...
 $ RestECG    : int  2 2 2 0 2 0 2 0 2 2 ...
 $ MaxHR      : int  150 108 129 187 172 178 160 163 147 155 ...
 $ ExAng      : chr  "no" "yes" "yes" "no" ...
 $ Fluoroscopy: int  0 3 2 0 0 0 2 0 1 0 ...
 $ Thal       : chr  "fixed" "normal" "reversable" "normal" ...
 $ AHD        : chr  "No" "Yes" "Yes" "No" ...
  1. State which variables are categorical and which are numerical. Integer- Numerical Variables Character- Categirical Variables
sapply(clev, class)
        Age         Sex   ChestPain      RestBP        Chol         Fbs 
  "integer" "character" "character"   "integer"   "integer"   "logical" 
    RestECG       MaxHR       ExAng Fluoroscopy        Thal         AHD 
  "integer"   "integer" "character"   "integer" "character" "character" 
  1. Nonparametric tests.
  1. Use the Wilcoxon rank-sum test to compare Age between males and females.
wilcox.test(Age ~ Sex, data = clev)

    Wilcoxon rank sum test with continuity correction

data:  Age by Sex
W = 11222, p-value = 0.08352
alternative hypothesis: true location shift is not equal to 0
  1. Use binom.test to test whether the proportion of AHD = Yes is equal to 0
yes_count <- sum(clev$AHD == "Yes")
binom.test(x = yes_count, n = nrow(clev), p = 0.5)

    Exact binomial test

data:  yes_count and nrow(clev)
number of successes = 139, number of trials = 303, p-value =
0.1679
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
 0.4016348 0.5166714
sample estimates:
probability of success 
             0.4587459 
  1. Chi-squared tests.
  1. Use table to make a contingency table for Sex and AHD.
contingency_table <- table(clev$Sex, clev$AHD)
contingency_table
  1. Use chisq.test on the contingency table and state the conclusion.
chi_test <- chisq.test(contingency_table)
chi_test
  1. Manually create the same 2 × 2 table using matrix, then run chisq.test(m, correct = F).
manual_matrix <- matrix(c(10, 15, 20, 25), nrow = 2, byrow = TRUE)
dimnames(manual_matrix) <- list(Sex = c("Male", "Female"), AHD = c("No", "Yes"))
chisq.test(manual_matrix, correct = FALSE)
  1. Use chisq.test for a goodness-of-fit test on the counts of AHD if the null model is 50%-50%
ahd_counts <- table(clev$AHD)
chisq.test(ahd_counts, p = c(0.5, 0.5))
  1. One-way ANOVA.
  1. Use ChestPain as the categorical variable and Chol as the numerical response.
  2. Make a side-by-side boxplot with boxplot(Chol ChestPain, data=clev).
boxplot(Chol ~ ChestPain, data = clev)
  1. Fit a one-way ANOVA model using lm and anova.
model <- lm(Chol ~ ChestPain, data = clev)
anova(model)
  1. Check the ANOVA conditions by making a QQ plot of the residuals and a plot of residuals versus fitted values.
qqnorm(residuals(model))
qqline(residuals(model))
plot(fitted(model), residuals(model))
  1. State your conclusion in context. Since the p-value (0.6007) is much greater than the common significance level of 0.05, we fail to reject the null hypothesis.

This means there is no statistically significant difference in mean cholesterol levels across the different types of chest pain in this data set. In simpler terms, the average cholesterol level appears to be similar regardless of the type of chest pain a person experiences.

  1. Two-way ANOVA.
  1. Use Sex and AHD as the two categorical variables, and use Chol as the response.
  2. Make an interaction boxplot using boxplot(Chol Sex+AHD, data=clev).
boxplot(Chol ~ Sex + AHD, data = clev)

  1. Fit the model with interaction using lm(Chol Sex*AHD, data=clev).
model_with_interaction <- lm(Chol ~ Sex * AHD, data = clev)
anova(model_with_interaction)
Analysis of Variance Table

Response: Chol
           Df Sum Sq Mean Sq F value    Pr(>F)    
Sex         1  32357   32357 12.7363 0.0004176 ***
AHD         1  17309   17309  6.8132 0.0095044 ** 
Sex:AHD     1    333     333  0.1310 0.7176778    
Residuals 299 759618    2541                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  1. Fit the model without interaction using lm(Chol Sex+AHD, data=clev).
model_without_interaction <- lm(Chol ~ Sex + AHD, data = clev)
anova(model_without_interaction)
Analysis of Variance Table

Response: Chol
           Df Sum Sq Mean Sq F value    Pr(>F)    
Sex         1  32357   32357  12.773 0.0004095 ***
AHD         1  17309   17309   6.833 0.0094005 ** 
Residuals 300 759950    2533                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  1. Compare the two ANOVA tables and determine whether the interaction term is significant. The p-value for the Sex:AHD interaction term in the model with interaction is 0.7176778. Since this p-value is much greater than 0.05, the interaction term is not statistically significant.

This means that the effect of Sex on Chol does not significantly depend on AHD, and vice-versa. In other words, the relationship between Sex and Chol is consistent across different AHD groups, and the relationship between AHD and Chol is consistent across different Sex groups.

  1. Check the residual plots for the model without interaction. Given that the interaction is not significant, the model without interaction (model_without_interaction) is generally preferred as it is simpler and explains the data almost as well.
  1. Correlation.
  1. Compute the correlation between Age and MaxHR.
correlation_result <- cor.test(clev$Age, clev$MaxHR)
  1. Report the correlation coefficient, confidence interval, and p-value.
correlation_result

    Pearson's product-moment correlation

data:  clev$Age and clev$MaxHR
t = -7.4329, df = 301, p-value = 1.109e-12
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 -0.4849644 -0.2941816
sample estimates:
       cor 
-0.3938058 
  1. Least squares regression.
  1. Use Age as the explanatory variable and MaxHR as the response variable.
  2. Make a scatterplot and add the least squares line using abline.
plot(clev$Age, clev$MaxHR, xlab = "Age", ylab = "MaxHR")
abline(lm(MaxHR ~ Age, data = clev), col = "pink")

  1. Fit the regression line using lm.
model <- lm(MaxHR ~ Age, data = clev)
  1. Display the regression summary using summary.
summary(model)

Call:
lm(formula = MaxHR ~ Age, data = clev)

Residuals:
    Min      1Q  Median      3Q     Max 
-66.088 -12.040   3.965  15.937  44.955 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 203.8634     7.3991  27.553  < 2e-16 ***
Age          -0.9966     0.1341  -7.433 1.11e-12 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 21.06 on 301 degrees of freedom
Multiple R-squared:  0.1551,    Adjusted R-squared:  0.1523 
F-statistic: 55.25 on 1 and 301 DF,  p-value: 1.109e-12
  1. Compare the two ANOVA tables and determine whether the interaction term is significant.
confint(model)
                 2.5 %      97.5 %
(Intercept) 189.302961 218.4238178
Age          -1.260505  -0.7327788
  1. Check the residual plots for the model without interaction.
par(mfrow= c (1, 2))
plot(clev$Age, residuals(model), xlab = "Age", ylab = "Residuals")
abline(h = 0, col = "orange")

qqnorm(residuals(model))
qqline(residuals(model), col = "purple")
par(mfrow = c(1, 1))

LS0tCnRpdGxlOiAiUiBwcm9qZWN0IDMiCm91dHB1dDoKICBodG1sX25vdGVib29rOiBkZWZhdWx0CiAgcGRmX2RvY3VtZW50OiBkZWZhdWx0Ck5hbWU6IFNhcmFoIEhhbWlsdG9uCi0tLQoKMS4gIFJlYWQgdGhlIERhdGEgYW5kIGluc3BlY3QgaXQuCgo8IS0tIC0tPgoKKGEpIFJlYWQgQ2xldmVsYW5kSGVhcnQuY3N2IGludG8gUiBhbmQgc3RvcmUgaXQgaW4gYSB2YXJpYWJsZSBuYW1lZCBjbGV2LgooYikgRGlzcGxheSB0aGUgZmlyc3QgZmV3IHJvd3MgYW5kIHRoZSBzdHJ1Y3R1cmUgb2YgdGhlIGRhdGFzZXQuCgpgYGB7cn0KaGVhZChjbGV2KQpzdHIoY2xldikKYGBgCgooYykgU3RhdGUgd2hpY2ggdmFyaWFibGVzIGFyZSBjYXRlZ29yaWNhbCBhbmQgd2hpY2ggYXJlIG51bWVyaWNhbC4gSW50ZWdlci0gTnVtZXJpY2FsIFZhcmlhYmxlcyBDaGFyYWN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