Assignment Week 3-3B Codebase

Author

Yalda Azamee

Published

December 1, 2026

Approach

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.

library(tidyverse)

url <- "https://raw.githubusercontent.com/YaldaAzamee/Assignment-Week-3/main/daily_prices.csv"
prices <- read_csv(url, show_col_types = FALSE)

glimpse(prices)
Rows: 1,494
Columns: 3
$ date  <date> 2025-01-02, 2025-01-03, 2025-01-06, 2025-01-07, 2025-01-08, 202…
$ item  <chr> "AAA", "AAA", "AAA", "AAA", "AAA", "AAA", "AAA", "AAA", "AAA", "…
$ close <dbl> 118.25, 120.04, 120.52, 122.56, 121.24, 118.24, 116.12, 118.12, …

Check the Data

# Dates are real dates, there are no missing values, and each item has enough rows
class(prices$date)
[1] "Date"
sum(is.na(prices))
[1] 0
prices |> count(item)
# A tibble: 3 × 2
  item      n
  <chr> <int>
1 AAA     498
2 BBB     498
3 CCC     498
# Dates are in chronological order within each item
prices |>
  group_by(item) |>
  summarise(dates_in_order = !is.unsorted(date))
# A tibble: 3 × 2
  item  dates_in_order
  <chr> <lgl>         
1 AAA   TRUE          
2 BBB   TRUE          
3 CCC   TRUE          

Window Functions in dplyr

  • 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.
results <- prices |>
  arrange(item, date) |>
  group_by(item, year = year(date)) |>
  mutate(ytd_avg = cummean(close)) |>
  group_by(item) |>
  mutate(ma_6day = (close + lag(close, 1) + lag(close, 2) +
                    lag(close, 3) + lag(close, 4) + lag(close, 5)) / 6) |>
  ungroup() |>
  select(date, item, close, ytd_avg, ma_6day)

results |>
  filter(item == "AAA") |>
  head(8) |>
  knitr::kable(digits = 2, caption = "First 8 rows for AAA")
First 8 rows for AAA
date item close ytd_avg ma_6day
2025-01-02 AAA 118.25 118.25 NA
2025-01-03 AAA 120.04 119.15 NA
2025-01-06 AAA 120.52 119.60 NA
2025-01-07 AAA 122.56 120.34 NA
2025-01-08 AAA 121.24 120.52 NA
2025-01-09 AAA 118.24 120.14 120.14
2025-01-10 AAA 116.12 119.57 119.79
2025-01-13 AAA 118.12 119.39 119.47

Verify the Results

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.

aaa <- results |> filter(item == "AAA")

all.equal(aaa$ma_6day[6], mean(aaa$close[1:6]))
[1] TRUE
all.equal(aaa$ytd_avg[10], mean(aaa$close[1:10]))
[1] TRUE

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.