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
library(boot)
library(broom)
library(lindia)
library(dplyr)
library(ggplot2)
library(dplyr)
library(glmnet)
## Warning: package 'glmnet' was built under R version 4.3.2
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## Loaded glmnet 4.1-8
library(tsibble)
## Warning: package 'tsibble' was built under R version 4.3.2
## 
## Attaching package: 'tsibble'
## 
## The following object is masked from 'package:lubridate':
## 
##     interval
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, union
library(lubridate)
mpg<- read_delim("C:/Users/Jamil Shaikh/Desktop/heart.csv", delim = ",",show_col_types = FALSE)
glimpse(mpg)
## Rows: 303
## Columns: 14
## $ 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…
## $ Date     <chr> "07-11-2022", "10-01-2022", "23-08-2022", "29-11-2022", "21-0…
mpg$Date <- seq(as.Date("2022-01-01"), by = "days", length.out = nrow(mpg))
heart_tsibble <- mpg %>%
  select(Date, cp) %>%
  as_tsibble(index = Date)
ggplot(heart_tsibble, aes(x = Date, y = cp)) +
  geom_line() +
  labs(title = "Different Chest pain at different Dates",
       x = "Date",
       y = "Chest Pain") +
  theme_minimal()

heart_monthly <- heart_tsibble  %>%
  index_by(Month = floor_date(Date, unit = "month")) %>%
  summarise(Total_cp = sum(cp))

ggplot(heart_monthly, aes(x = Month, y = Total_cp)) +
  geom_line() +
  labs(title = "Monthly Chest Pains",
       x = "Month",
       y = "Chest Pains") +
  theme_minimal()

heart_tsibble$Year <- year(heart_tsibble$Date)

# Fit a linear model for the entire dataset
model <- lm(cp ~ Date, data = heart_tsibble)
print(summary(model))
## 
## Call:
## lm(formula = cp ~ Date, data = heart_tsibble)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.6582 -0.6731 -0.3134  0.6968  2.7289 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 90.9824918 11.9179281   7.634 3.03e-13 ***
## Date        -0.0047020  0.0006225  -7.553 5.13e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9478 on 301 degrees of freedom
## Multiple R-squared:  0.1593, Adjusted R-squared:  0.1565 
## F-statistic: 57.05 on 1 and 301 DF,  p-value: 5.128e-13
unique_years <- unique(heart_tsibble$Year)
counter <- 0

for (year in unique_years) {
  if (counter >= 10) {
    break
  }
  
  subset_data <- filter(heart_tsibble, Year == year)
  subset_model <- lm(cp ~ Date, data = subset_data)
  print(paste("Year:", year))
  print(summary(subset_model))
  
  counter <- counter + 1
}
## [1] "Year: 2022"
## 
## Call:
## lm(formula = cp ~ Date, data = subset_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.6582 -0.6731 -0.3134  0.6968  2.7289 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 90.9824918 11.9179281   7.634 3.03e-13 ***
## Date        -0.0047020  0.0006225  -7.553 5.13e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9478 on 301 degrees of freedom
## Multiple R-squared:  0.1593, Adjusted R-squared:  0.1565 
## F-statistic: 57.05 on 1 and 301 DF,  p-value: 5.128e-13
library(zoo)
## Warning: package 'zoo' was built under R version 4.3.2
## 
## Attaching package: 'zoo'
## The following object is masked from 'package:tsibble':
## 
##     index
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
heart_tsibble$chestpain_rate <- rollmean(heart_tsibble$cp, 30, fill = NA)

ggplot(heart_tsibble, aes(x = Date)) +
  geom_line(aes(y = cp), color = "blue") +
  geom_line(aes(y = chestpain_rate), color = "red") +
  labs(title = "Chestpain with Chestpain_rate",
       x = "Date",
       y = "Chestpain_rate") +
  theme_minimal()
## Warning: Removed 29 rows containing missing values (`geom_line()`).

acf(heart_tsibble$cp, main = "ACF for Chest Pain")

pacf(heart_tsibble$cp, main = "PACF for Chest Pain")