I will use an LLM to generate a simulated time-series dataset of daily closing prices for three fictional stocks, with columns for the date, item, and closing price, covering weekdays from January 2025 to November 2026. Because the data covers two years, I can check that the year-to-date average restarts each January. I will save the dataset as a CSV file on GitHub so the code can be run on any computer.
In R, I will use dplyr window functions to calculate, separately for each item:
Year-to-date (YTD) average: the average closing price from January 1 of the same year up to the current day. I will group by item and year, sort by date, and use cummean().
Six-day moving average: the average of the current day and the five previous trading days. I will group by item, sort by date, and use lag() to get the five previous prices.
Anticipated Challenges
Data quality: I will check that the dates are correctly formatted and in chronological order, and that there are no missing values.
The first days of each item: the first five rows do not have six days of data, so their six-day moving average will be NA instead of an average of fewer days.
Checking the results: I will calculate a few values by hand and confirm that they match the results.
Load the Data
The dataset was generated with an LLM (Claude). It contains simulated end-of-day closing prices for three fictional stocks (AAA, BBB, CCC) on weekdays from January 2, 2025 to November 30, 2026. The prices are not real market data. The file is stored on GitHub so the code runs on any computer.
YTD average:cummean() within each item and year, so it restarts every January 1.
Six-day moving average: the current price plus the five previous prices (lag()), divided by 6, within each item. For the first five rows lag() returns NA, so the moving average is NA.
I check two values by hand for AAA: the six-day moving average on the 6th day should equal the mean of the first six prices, and the YTD average on the 10th day should equal the mean of the first ten prices.
The YTD average restarts at the beginning of 2026 (on the first day of 2026 it equals that day’s price):
results |>filter(item =="AAA", date >=as.Date("2025-12-29"), date <=as.Date("2026-01-06")) |> knitr::kable(digits =2, caption ="AAA around the start of 2026")
AAA around the start of 2026
date
item
close
ytd_avg
ma_6day
2025-12-29
AAA
98.76
112.98
102.71
2025-12-30
AAA
98.09
112.92
101.18
2025-12-31
AAA
98.17
112.87
99.85
2026-01-01
AAA
98.28
98.28
98.76
2026-01-02
AAA
97.83
98.06
98.27
2026-01-05
AAA
96.88
97.66
98.00
2026-01-06
AAA
98.04
97.76
97.88
Visualization
results |>filter(date >=as.Date("2026-01-01")) |>pivot_longer(c(close, ytd_avg, ma_6day), names_to ="measure", values_to ="price") |>mutate(measure =recode(measure, close ="Closing price",ytd_avg ="YTD average", ma_6day ="6-day moving average")) |>ggplot(aes(date, price, color = measure)) +geom_line() +facet_wrap(~ item, ncol =1, scales ="free_y") +labs(title ="Closing Price, YTD Average, and 6-Day Moving Average (2026)",x ="Date", y ="Price ($)", color =NULL) +theme_minimal() +theme(legend.position ="bottom")
Conclusion
The six-day moving average follows the closing price closely and smooths out day-to-day changes. The YTD average changes much more slowly because it includes every day since January 1, and it restarts at the beginning of each year. The hand-calculated checks match the results.
AI Use
Anthropic. (2026). Claude Opus 5.5 [Large language model]. https://claude.ai. Accessed December 1, 2026. Used to generate the simulated price dataset, help understand the assignment,and check the R code.