library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.4
## ✔ ggplot2 3.4.3 ✔ stringr 1.5.0
## ✔ lubridate 1.9.2 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.0
## ── 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(readr)
library(ggplot2)
glimpse(mpg)
## Rows: 234
## Columns: 11
## $ manufacturer <chr> "audi", "audi", "audi", "audi", "audi", "audi", "audi", "…
## $ model <chr> "a4", "a4", "a4", "a4", "a4", "a4", "a4", "a4 quattro", "…
## $ displ <dbl> 1.8, 1.8, 2.0, 2.0, 2.8, 2.8, 3.1, 1.8, 1.8, 2.0, 2.0, 2.…
## $ year <int> 1999, 1999, 2008, 2008, 1999, 1999, 2008, 1999, 1999, 200…
## $ cyl <int> 4, 4, 4, 4, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 8, 8, …
## $ trans <chr> "auto(l5)", "manual(m5)", "manual(m6)", "auto(av)", "auto…
## $ drv <chr> "f", "f", "f", "f", "f", "f", "f", "4", "4", "4", "4", "4…
## $ cty <int> 18, 21, 20, 21, 16, 18, 18, 18, 16, 20, 19, 15, 17, 17, 1…
## $ hwy <int> 29, 29, 31, 30, 26, 26, 27, 26, 25, 28, 27, 25, 25, 25, 2…
## $ fl <chr> "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p…
## $ class <chr> "compact", "compact", "compact", "compact", "compact", "c…
mpg<- read_delim("C:/Users/Jamil Shaikh/Desktop/heart1.csv", delim = ",",show_col_types = FALSE)
# Grouping by two different numeric_column ranges, and calculating probabilities
grouped_data_1 <- mpg %>%
group_by(chol=cut(chol,breaks=c(0,50,100,150,250)), trestbps=cut(trestbps, breaks = c(0,50,100,150,200))) %>%
summarise(
age_avg_value = mean(`age`),
count_age = n(),
.groups = "drop"
) %>%
mutate(
expected_prob_age = 1 / count_age,
# print(expected_prob_age),
`age_anomaly` = ifelse(expected_prob_age > 0.5, "Normal", "Anomaly"),
)
print(grouped_data_1)
## # A tibble: 7 × 6
## chol trestbps age_avg_value count_age expected_prob_age age_anomaly
## <fct> <fct> <dbl> <int> <dbl> <chr>
## 1 (100,150] (100,150] 55.6 5 0.2 Anomaly
## 2 (150,250] (50,100] 51.4 5 0.2 Anomaly
## 3 (150,250] (100,150] 52.1 152 0.00658 Anomaly
## 4 (150,250] (150,200] 60.6 15 0.0667 Anomaly
## 5 <NA> (50,100] 67 1 1 Normal
## 6 <NA> (100,150] 55.6 106 0.00943 Anomaly
## 7 <NA> (150,200] 61 19 0.0526 Anomaly
grouped_data_2 <- mpg %>%
group_by(chol=cut(chol,breaks=c(0,50,100,150,250)), target =cut(target, breaks = c(0,50,100,150,200))) %>%
summarise(
sex_avg_value = mean(`sex`),
count_sex = n(),
.groups = "drop"
) %>%
mutate(
expected_prob_sex = 1 / count_sex,
`sex_anomaly` = ifelse(expected_prob_sex > 0.5, "Normal", "Anomaly")
)
print(grouped_data_2)
## # A tibble: 6 × 6
## chol target sex_avg_value count_sex expected_prob_sex sex_anomaly
## <fct> <fct> <dbl> <int> <dbl> <chr>
## 1 (100,150] (0,50] 0.333 3 0.333 Anomaly
## 2 (100,150] <NA> 1 2 0.5 Anomaly
## 3 (150,250] (0,50] 0.663 104 0.00962 Anomaly
## 4 (150,250] <NA> 0.853 68 0.0147 Anomaly
## 5 <NA> (0,50] 0.397 58 0.0172 Anomaly
## 6 <NA> <NA> 0.794 68 0.0147 Anomaly
grouped_data_3 <- mpg %>%
group_by(restecg=cut(restecg,breaks=c(0,1)), target =cut(target, breaks = c(0,1))) %>%
summarise(
cp_avg_value = mean(`cp`),
count_cp = n(),
.groups = "drop"
) %>%
mutate(
expected_prob_cp = 1 / count_cp,
`cp_anomaly` = ifelse(expected_prob_cp > 0.5, "Normal", "Anomaly")
)
print(grouped_data_3)
## # A tibble: 4 × 6
## restecg target cp_avg_value count_cp expected_prob_cp cp_anomaly
## <fct> <fct> <dbl> <int> <dbl> <chr>
## 1 (0,1] (0,1] 1.30 96 0.0104 Anomaly
## 2 (0,1] <NA> 0.589 56 0.0179 Anomaly
## 3 <NA> (0,1] 1.48 69 0.0145 Anomaly
## 4 <NA> <NA> 0.402 82 0.0122 Anomaly
barplot(grouped_data_3$expected_prob_cp, names.arg = grouped_data_3$target,
main = "Expected Probability by target", ylab = "Expected Probability by chest pain")
