library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggthemes)
library(ggrepel)
library(effsize)
library(pwrss)
## Warning: package 'pwrss' was built under R version 4.3.2
## 
## Attaching package: 'pwrss'
## 
## The following object is masked from 'package:stats':
## 
##     power.t.test

The purpose of the assignment is to 1) Devise at least two different null hypotheses based on two different aspects (e.g., columns) of your data. For each hypothesis: i)Come up with an alpha level, power level, and minimum effect size, and explain why you chose each value. ii) Determine if you have enough data to perform a Neyman-Pearson hypothesis test. If you do, perform one and interpret results. If not, explain why. iii) Perform a Fisher’s style test for significance, and interpret the p-value. So, technically, you have two hypothesis tests for each hypothesis, equating two four total tests.

  1. Build two visualizations that best illustrate the results from the two pairs of hypothesis tests, one for each null hypothesis.

Loading Data

mpg<- read_delim("C:/Users/Jamil Shaikh/Desktop/heart1.csv", delim = ",",show_col_types = FALSE)
glimpse(mpg)
## Rows: 303
## Columns: 13
## $ age      <dbl> 63, 37, 41, 56, 57, 57, 56, 44, 52, 57, 54, 48, 49, 64, 58, 5…
## $ sex      <dbl> 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1…
## $ cp       <dbl> 3, 2, 1, 1, 0, 0, 1, 1, 2, 2, 0, 2, 1, 3, 3, 2, 2, 3, 0, 3, 0…
## $ trestbps <dbl> 145, 130, 130, 120, 120, 140, 140, 120, 172, 150, 140, 130, 1…
## $ chol     <dbl> 233, 250, 204, 236, 354, 192, 294, 263, 199, 168, 239, 275, 2…
## $ fbs      <dbl> 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0…
## $ restecg  <dbl> 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1…
## $ thalach  <dbl> 150, 187, 172, 178, 163, 148, 153, 173, 162, 174, 160, 139, 1…
## $ exang    <dbl> 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0…
## $ oldpeak  <dbl> 2.3, 3.5, 1.4, 0.8, 0.6, 0.4, 1.3, 0.0, 0.5, 1.6, 1.2, 0.2, 0…
## $ slope    <dbl> 0, 0, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 0, 2, 2, 1…
## $ ca       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0…
## $ target   <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…

##Hypothesis Test 1:

Alpha Level (α): 0.05 Power Level: 0.80 Minimum Effect Size (Cohen’s d): 0.50 (medium effect size)

alpha <- 0.05
power <- 0.80
effect_size <- 0.50

result_t_test <- t.test(mpg$age ~ mpg$target)



# Visualization
boxplot(`age` ~ target, data = mpg, main = "Age at which people havehigh rate for heart disease",
        xlab = "target", ylab = "age", col = c("blue", "green"))
legend("topright", legend = c("trestbps", "thalach"), fill = c("blue", "green"))

# Interpretation of Results
if (result_t_test$p.value < alpha) {
  cat("H0 is rejected. There is a significant difference in age and target.")
} else {
  cat("H0 is not rejected. There is no significant difference in age and target.")
}
## H0 is rejected. There is a significant difference in age and target.

##Hypothesis Test 2:

Alpha Level (α): 0.05 Power Level: 0.80 Minimum Effect Size (Cohen’s d): 0.50 (medium effect size)

# Hypothesis Test 2: 
alpha <- 0.05
power <- 0.80
effect_size <- 0.50


result_t_test <- t.test(mpg$target ~ mpg$sex)

print (result_t_test$p.value)
## [1] 2.439839e-07
# Visualization
boxplot(`target` ~ `sex`, data = mpg, main = "target vs sex ",
        xlab = "target", ylab = "sex", col = c("blue", "green"))
legend("topright", legend = c("target", "sex"), fill = c("blue", "green"))

# Interpretation of Results
if (result_t_test$p.value < alpha) {
  cat("H0 is rejected. There is a significant difference in age and target.")
} else {
  cat("H0 is not rejected. There is no significant difference in  age and target.")
}
## H0 is rejected. There is a significant difference in age and target.