Window Functions:
The data set that I have decided to use for this lab was a
synthetic data set generated by ChatGPT. I prompted ChatGPT to generate
a data set that included a time series for two or more items, I added
the window functions and asked that the table be NBA themed because I
wanted to base it around a topic that I find interesting. The generated
data set was called NBA Daily Team Strength and features NBA team scores
from 2022-2024. The set also includes a column called
Team_Strength_Index which is a synthetic stat that is updated after
every game. This column is the one that I will be using Window Functions
on. I believe that the hardest part of the problem was figuring out what
data set I could find that would fit the criteria, and now I believe the
rest of the lab will be straightforward. I will update my conclusions if
there are any changes.
nba_daily_team_strength_df = read.csv("https://raw.githubusercontent.com/Renagade316/DATA607-Labs/refs/heads/main/Lab3/Lab3B/NBA%20Daily%20Team%20Strength%202022%20-%202024%20-%20Daily%20Team%20Data.csv")
nba_daily_team_strength_df <- select(nba_daily_team_strength_df, observation_date, calendar_year, team_name, opponent_abbreviation, is_game_day, win_flag, team_strength_index )
# Year to Date Average
nba_daily_team_strength_df <- nba_daily_team_strength_df |>
group_by(team_name) |>
mutate(year_to_date_avg = cummean(team_strength_index))
#6 Day Average
#slide_dbl(1:5, ~mean(.x), .before = 2)
nba_daily_team_strength_df <- nba_daily_team_strength_df |>
group_by(team_name) |>
mutate(six_day_avg = slide_mean(team_strength_index, before=5))
ex <- select(nba_daily_team_strength_df, team_name, team_strength_index, year_to_date_avg, six_day_avg)
ex <- filter(ex, team_name=="Boston Celtics")
ex
## # A tibble: 1,096 × 4
## # Groups: team_name [1]
## team_name team_strength_index year_to_date_avg six_day_avg
## <chr> <dbl> <dbl> <dbl>
## 1 Boston Celtics 110. 110. 110.
## 2 Boston Celtics 108. 109. 109.
## 3 Boston Celtics 109. 109. 109.
## 4 Boston Celtics 110. 109. 109.
## 5 Boston Celtics 109. 109. 109.
## 6 Boston Celtics 108. 109. 109.
## 7 Boston Celtics 109. 109. 109.
## 8 Boston Celtics 109. 109. 109.
## 9 Boston Celtics 110. 109. 109.
## 10 Boston Celtics 109. 109. 109.
## # ℹ 1,086 more rows
In conclusion, as I stated in my introduction, the project was
relatively straightforward after getting my data set. I did have to
spend extra time researching Window Functions. I now understand that
Window Functions are useful for keeping track of data as it appears.
Problems that I did have trouble with, was if the before in slide_mean
was inclusive or not. To test my code, I created a data frame called
ex. I only selected the team name, the team strength index, the
year to date average and the six day average as these were the only
values necessary for this purpose. Originally I had written,
“six_day_avg = slide_mean(team_strength_index, before=6”, but when I ran
the code, I noticed that the 7th row the year to date average and six
day average were the same number, this meant that the average for six
days include the first date in the table. I double checked the average
with my calculator to ensure that it was in fact calculating the
average, which confirmed that the issue was with my before clause. I
then switched the clause with before = 5, and ran the code again. This
time the numbers were different, and the 6 day average ws from the 2nd
to 7th date respectively.
Link to ChatGPT prompt: https://chatgpt.com/s/t_6aadef467bf08191ad9bdbcd35a5b6cb
OpenAI. (2026). Generate an NBA time-series dataset for window-function analysis [ChatGPT conversation]. ChatGPT.