# Load the necessary library

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)
## 
## Attaching package: 'pwrss'
## 
## The following object is masked from 'package:stats':
## 
##     power.t.test
library(boot)
library(broom)
library(lindia)
library(dplyr)
library(ggplot2)
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…
# Create a subset of the dataset for ANOVA
data_anova <- mpg %>%
  select("sex", `age`)

# Rename the columns for clarity
colnames(data_anova) <- c("sex", "age")

# Perform ANOVA
anova_result <- aov(age ~ sex, data = data_anova)
summary(anova_result)
##              Df Sum Sq Mean Sq F value Pr(>F)  
## sex           1    241  241.42   2.946 0.0871 .
## Residuals   301  24669   81.96                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Create a subset of the dataset for regression
data_regression <- mpg %>%
  select(`chol`, `age`)

# Rename the columns for clarity
colnames(data_regression) <- c("chol", "age")

# Build a linear regression model
lm_model <- lm(chol ~ age, data = data_regression)
summary(lm_model)
## 
## Call:
## lm(formula = chol ~ age, data = data_regression)
## 
## 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
lm_model_combined <- lm(chol ~ age + mpg$chol, data = data_regression)
summary(lm_model_combined)
## Warning in summary.lm(lm_model_combined): essentially perfect fit: summary may
## be unreliable
## 
## Call:
## lm(formula = chol ~ age + mpg$chol, data = data_regression)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -5.990e-13 -4.500e-16  2.200e-15  4.640e-15  5.278e-14 
## 
## Coefficients:
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept) -1.078e-13  1.421e-14 -7.585e+00 4.20e-13 ***
## age         -9.004e-16  2.278e-16 -3.953e+00 9.62e-05 ***
## mpg$chol     1.000e+00  3.991e-17  2.506e+16  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.512e-14 on 300 degrees of freedom
## Multiple R-squared:      1,  Adjusted R-squared:      1 
## F-statistic: 3.29e+32 on 2 and 300 DF,  p-value: < 2.2e-16
# Scatter plot of age vs. sex
ggplot(data_regression, aes(x = age, y = chol, color = factor(mpg$sex))) +
  geom_point() +
  labs(title = "Scatter Plot of Sex vs. Age",
       x = "age",
       y = "sex") +
  theme_minimal()

# Box plot of sex by chol
ggplot(data_regression, aes(x = factor(mpg$sex), y = chol, fill = factor(mpg$sex))) +
  geom_boxplot() +
  labs(title = "Box Plot of Sex by Cholestrol ",
       x = "sex",
       y = "chol") +
  theme_minimal()