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(ggplot2)
library(stats)
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…

Task 1 & 2 : Build at least three sets of variable combinations For each set of variables, include at least one column that you created (i.e., calculated based on others) All variables for this data dive should be either continuous (i.e., numeric) or ordered (e.g., [‘small’, ‘medium’, ‘large’] is okay, but [“apples”, “oranges”, “bananas”] is not) For each set, there should be one response variable with the others as explanatory variables

Plot a visualization for each response-explanatory relationship, and draw some conclusions based on the plot

# Create a scatterplot for Set 1
ggplot(mpg, aes(x = `chol`, y = `age`, color = `cp`)) +
  geom_point() +
  labs(title = "Scatterplot of chest pain",
       x = "chol",
       y = "age",
       color = "cp") +
  theme_minimal()

# Create a scatterplot matrix for Set 2
ggplot(mpg, aes(x = target, y = `age`, fill = factor(`sex`))) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Grouped Bar Chart of Heart Disease by target and sex",
       x = "Target",
       y = "age") +
  scale_fill_discrete(name = "sex")

# Create a scatterplot for Set 3
ggplot(mpg, aes(x = `trestbps`, y = age, color = `sex`)) +
  geom_point() +
  labs(title = "Scatterplot of sex",
       x = "trestbps",
       y = "age",
       color = "sex") +
  theme_minimal()

Task 3 & 4 :

Calculate the appropriate correlation coefficient for each of these combinations Explain why the value makes sense (or doesn’t) based on the visualization(s)

Build a confidence interval for each of the response variables. Provide a detailed conclusion of the response variable (i.e., the population) based on your confidence interval.

#Combination 1:

# Correlation matrix
correlation_matrix <- cor(mpg[, c("chol", "age", "cp")])

# Extract correlation coefficients
correlation_coefficients <- correlation_matrix["chol", c("age", "cp")]

# Display correlation coefficients
correlation_coefficients
##         age          cp 
##  0.21367796 -0.07690439

`

# Confidence interval for "chol"
confidence_interval <- t.test(mpg$`chol`)

# Display the confidence interval
confidence_interval
## 
##  One Sample t-test
## 
## data:  mpg$chol
## t = 82.706, df = 302, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  240.4046 252.1235
## sample estimates:
## mean of x 
##   246.264
#Combination 2:
#Response Variable: "target"
#Explanatory Variables: "trestbps," "age," "sex"
# Perform logistic regression
mpg$target <- ifelse(mpg$target == "Heart_Disease", 0, 1)

logistic_model <- glm(target ~ `trestbps` + `age`, data = mpg, family = binomial)
## Warning: glm.fit: algorithm did not converge
# Summary of the logistic regression model
summary(logistic_model)
## 
## Call:
## glm(formula = target ~ trestbps + age, family = binomial, data = mpg)
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)
## (Intercept)  2.657e+01  1.760e+05       0        1
## trestbps    -3.420e-09  1.217e+03       0        1
## age          9.921e-09  2.350e+03       0        1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 0.0000e+00  on 302  degrees of freedom
## Residual deviance: 1.7579e-09  on 300  degrees of freedom
## AIC: 6
## 
## Number of Fisher Scoring iterations: 25
# Assuming your data frame is named "data," create a linear regression model
model <- lm(`target` ~ `age` + `sex`, data = mpg)

# Summarize the regression model
summary(model)
## 
## Call:
## lm(formula = target ~ age + sex, data = mpg)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -1.380e-13  6.900e-17  4.860e-16  8.930e-16  1.884e-15 
## 
## Coefficients:
##               Estimate Std. Error    t value Pr(>|t|)    
## (Intercept)  1.000e+00  2.951e-15  3.389e+14   <2e-16 ***
## age         -5.213e-17  5.094e-17 -1.023e+00    0.307    
## sex         -7.722e-16  9.927e-16 -7.780e-01    0.437    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 8e-15 on 300 degrees of freedom
## Multiple R-squared:  0.4996, Adjusted R-squared:  0.4963 
## F-statistic: 149.8 on 2 and 300 DF,  p-value: < 2.2e-16