Created
April 12, 2025 19:29
-
-
Save pedrobrantes/5f9151cc90d6ef4373478e0cba4e6377 to your computer and use it in GitHub Desktop.
This Gist contains an R script that calculates and visualizes the date of Easter over a range of years (2000-2025). It uses the timeDate package to determine Easter dates, dplyr for data manipulation, ggplot2 for creating trend lines, frequency bar charts, and box plots, lubridate for date manipulation, and forecast for analyzing the autocorrela…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Functions to calculate easter day | |
| library(timeDate) | |
| calculate_easter <- function(year) { | |
| easter_date <- as.Date(Easter(year)) | |
| return(easter_date) | |
| } | |
| desired_year <- 2025 | |
| easter_date_year <- calculate_easter(desired_year) | |
| print(paste("Easter in", desired_year, "will be on:", format(easter_date_year, "%d of %B of %Y"))) | |
| previous_years <- seq(2000, 2024) | |
| previous_easter_dates <- lapply(previous_years, calculate_easter) | |
| print("Easter dates in the last years:") | |
| for (i in 1:length(previous_years)) { | |
| print(paste(previous_years[i], ":", format(previous_easter_dates[[i]], "%d of %B of %Y"))) | |
| } | |
| future_years <- seq(2026, 2030) | |
| future_easter_dates <- lapply(future_years, calculate_easter) | |
| print("Easter dates in the next few years:") | |
| for (i in 1:length(future_years)) { | |
| print(paste(future_years[i], ":", format(future_easter_dates[[i]], "%d of %B of %Y"))) | |
| } | |
| # Plotting the easter day trend graph | |
| library(ggplot2) | |
| library(dplyr) | |
| library(lubridate) | |
| fig<-function(x,y){ | |
| options(repr.plot.width = x, repr.plot.height = y) | |
| } | |
| years <- 2000:2025 | |
| easter_dates <- as.Date(Easter(years)) | |
| easter_df <- data.frame( | |
| Year = years, | |
| EasterDate = easter_dates | |
| ) | |
| easter_df$DayOfYear <- yday(easter_df$EasterDate) | |
| y_breaks <- seq(80, 120, by = 5) | |
| labels_march <- paste0(1:31, " Mar") | |
| labels_april <- paste0(1:30, " Apr") | |
| all_labels <- c( | |
| labels_march[which( | |
| 1:31 %in% day(seq( | |
| as.Date("2000-03-01"), | |
| as.Date("2000-03-31"), | |
| by = "day" | |
| )) | |
| )], | |
| labels_april[which( | |
| 1:30 %in% day(seq( | |
| as.Date("2000-04-01"), | |
| as.Date("2000-04-30"), | |
| by = "day" | |
| )) | |
| )] | |
| ) | |
| relevant_breaks_index <- which( | |
| y_breaks >= 80 & y_breaks <= 120 | |
| ) | |
| relevant_breaks <- y_breaks[relevant_breaks_index] | |
| date_sequence <- seq( | |
| as.Date("2000-03-20"), | |
| as.Date("2000-04-30"), | |
| by = "day" | |
| ) | |
| relevant_labels <- format( | |
| date_sequence[which( | |
| yday(date_sequence) %in% relevant_breaks | |
| )], "%d of %b" | |
| ) | |
| fig(12,8) | |
| easter_df %>% | |
| ggplot(aes( | |
| x = Year, | |
| y = DayOfYear | |
| )) + | |
| geom_line( | |
| linewidth = 1 | |
| ) + | |
| scale_y_continuous( | |
| breaks = relevant_breaks, | |
| labels = relevant_labels | |
| ) + | |
| labs( | |
| title = "Easter date trend and variation over the years", | |
| x = "Years", | |
| y = "Easter days" | |
| ) + | |
| theme_minimal() + | |
| theme( | |
| axis.text.x = element_text(angle = 45, hjust = 1), | |
| axis.text.y = element_text(size = 8) | |
| ) + | |
| geom_smooth(se = FALSE, color = "red") | |
| # Plotting easter day monthly frequency | |
| easter_mar_apr <- easter_df %>% | |
| mutate(Month = month(EasterDate)) %>% | |
| filter(Month %in% c(3, 4)) %>% | |
| mutate(MonthName = case_when( | |
| Month == 3 ~ "March", | |
| Month == 4 ~ "Aoril" | |
| )) | |
| monthly_frequency <- easter_mar_apr %>% | |
| group_by(MonthName) %>% | |
| summarize(Frequency = n()) | |
| fig(12,8) | |
| ggplot(monthly_frequency, aes(x = MonthName, y = Frequency)) + | |
| geom_bar(stat = "identity", color = "black") + | |
| labs(title = "Easter frequency in March and April", | |
| x = "Month", | |
| y = "Frequency") + | |
| theme_minimal() | |
| # Plotting the easter distribution | |
| fig(12,8) | |
| ggplot(easter_df, aes(y = DayOfYear)) + | |
| geom_boxplot(color = "black") + | |
| scale_y_continuous(breaks = y_breaks, labels = relevant_labels) + | |
| labs(title = "Distribution of Easter date over the year", | |
| y = "Easter days") + | |
| theme_minimal() + | |
| theme(axis.text.x = element_blank()) | |
| # Analysis of autocorrelation | |
| library(forecast) | |
| easter_ts <- ts(easter_df$DayOfYear, start = min(years), end = max(years), frequency = 1) | |
| values <- list( | |
| acf = acf(easter_ts, plot = FALSE), | |
| pacf = pacf(easter_ts, plot = FALSE) | |
| ) | |
| acf_df <- data.frame( | |
| lag = values$acf$lag, | |
| acf = values$acf$acf | |
| ) | |
| pacf_df <- data.frame( | |
| lag = values$pacf$lag, | |
| pacf = values$pacf$acf | |
| ) | |
| n <- length(easter_ts) | |
| conf_level <- 1.96 / sqrt(n) | |
| # ACF | |
| fig(12,8) | |
| acf_df %>% | |
| ggplot(aes( | |
| x = lag, | |
| y = acf | |
| )) + | |
| geom_bar(stat = "identity") + | |
| geom_hline( | |
| yintercept = 0, | |
| color = "black" | |
| ) + | |
| geom_hline( | |
| yintercept = conf_level, | |
| color = "red", | |
| linetype = "dashed" | |
| ) + | |
| geom_hline( | |
| yintercept = -conf_level, | |
| color = "red", | |
| linetype = "dashed" | |
| ) + | |
| labs( | |
| title = "Autocorrelation of the Easter Date", | |
| x = "Lag (Years)", | |
| y = "Autocorrelation" | |
| ) + | |
| theme_minimal() | |
| # PACF | |
| fig(12,8) | |
| pacf_df %>% | |
| ggplot(aes( | |
| x = lag, | |
| y = pacf | |
| )) + | |
| geom_bar(stat = "identity") + | |
| geom_hline( | |
| yintercept = 0, | |
| color = "black" | |
| ) + | |
| geom_hline( | |
| yintercept = conf_level, | |
| color = "red", | |
| linetype = "dashed" | |
| ) + | |
| geom_hline( | |
| yintercept = -conf_level, | |
| color = "red", | |
| linetype = "dashed" | |
| ) + | |
| labs( | |
| title = "Parcial Autocorrelation of the Easter Date", | |
| x = "Lag (Years)", | |
| y = "Autocorrelation" | |
| ) + | |
| theme_minimal() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment