This function compares numeric variables (e.g., new case numbers) across specified months and years. It supports aggregation by day or ISO week, optional cross-year logic (e.g., combining Dec:Jan), automated visualization (trend line, dot plot, boxplot), and group-wise faceting. Statistical tests (e.g., t-test, ANOVA) are automatically selected and executed.
compare_monthly_cases(
df,
datum_col,
value_col,
group_col = NULL,
years,
months,
granularity = "day",
agg_fun = "sum",
shift_month = "none",
save_plot = FALSE,
save_path = "."
)A list with the following elements:
Aggregated and annotated data frame
Line plot showing daily/weekly trends
Dot plot by year and month
Boxplot comparing distributions across months and years
Result of statistical test (from [run_group_tests()])
Frequency table of observations per year
Data frame with at least a date and value column. / Data Frame mit Datum und Wert
Name of the date column.. / Name der Datums-Spalte
Name of the value column. / Name der Wertespalte
Optional grouping variable(s) for faceting. / Optionale Gruppierung
Vector of years to include. E.g., c(2023, 2024). / Zu vergleichende Jahre
Vector of months to include (1:12). / Zu vergleichende Monate
Aggregation level: "day" or "week". / Aggregationsebene
Aggregation function: "sum", "mean", or "median". / Aggregationsfunktion
Cross-year adjustment for Dec/Jan: "none", "mth_to_next", "mth_to_prev". / Jahreswechsel-Logik
Logical. Whether to save the plots as PNG files. / Plots speichern?
Path to folder where plots should be saved. / Speicherpfad
Diese Funktion vergleicht numerische Variablen (z.B. Fallzahlen) ueber ausgewaehlte Monate und Jahre hinweg. Sie unterstuetzt Aggregation nach Tag oder ISO-Woche, optionale Jahreswechsel-Logik (z.B. Dezember:Januar), automatische Visualisierung (Linien-, Punkt- und Boxplot) sowie Facetierung nach Gruppenvariablen. Die passenden statistischen Tests (z.B. t-Test, ANOVA) werden automatisch durchgefuehrt.
Function Behavior and Notes: - The function compares a numeric variable (e.g., case counts) across selected months and years. - Aggregation can be done at the "day" or "week" level (ISO week, Monday start). - When shift_month is set to "mth_to_next" or "mth_to_prev", months like December and January can be merged across year boundaries: - "mth_to_next": assigns months to the *next* year group (e.g., Dec 2023 to 2024). - "mth_to_prev": assigns monthd to the *previous* year group (e.g., Jan 2024 to 2023). - All plots (trend_plot, monthly_trend_plot, box_plot) are automatically colored by year and faceted if group_col is provided. - Statistical tests are performed automatically based on the number of groups (e.g., t.test, Wilcoxon, ANOVA, Kruskal-Wallis).
---
Funktionsverhalten und Hinweise: - Die Funktion vergleicht eine numerische Variable (z.B. Fallzahlen) ueber Monate und Jahre hinweg. - Die Aggregation erfolgt auf "day"- oder "week"-Ebene (ISO-Woche, Montag-basiert). - Mit shift_month = "mth_to_next" oder "mth_to_prev" koennen Monate ueber Jahresgrenzen hinweg zugeordnet werden: - "mth_to_next": Monat zum Folgejahr (z.B. Dez. 2023 → 2024) - "mth_to_prev": Monat zum Vorjahr (z.B. Jan. 2024 → 2023) - Alle Plots sind nach Jahr eingefaerbt; bei Angabe von group_col erfolgt eine Facetierung. - Die geeigneten statistischen Tests werden automatisch ausgewaehlt und durchgefuehrt.
---
[run_group_tests()], [check_continuity_by_window()], [standardize_case_columns()], [infer_value_type()]
set.seed(123)
test_df <- data.frame(
datum = seq.Date(from = as.Date("2024-12-15"), to = as.Date("2025-01-20"), by = "day"),
value = sample(0:50, size = 37, replace = TRUE)
)
compare_monthly_cases(
df = test_df,
datum_col = "datum",
value_col = "value",
years = c(2024, 2025),
months = c(12, 1),
granularity = "day",
agg_fun = "sum",
shift_month = "mth_to_next",
save_plot = FALSE
)
Run the code above in your browser using DataLab