Introduction

In this analysis, I explore the historical trends of gold prices. By visualizing the actual prices alongside a Six-Year Moving Average using data sourced from: datahub.io

The aim is to gain insights into the long-term behavior of gold prices and identify patterns or deviations in recent years. The moving average helps smooth out short-term fluctuations and provides a clearer view of overarching trends.

# Load necessary packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(slider)
library(ggplot2)
# Specify the URL
url <- "https://raw.githubusercontent.com/hbedros/gold_price_index/main/annual_csv.csv"

# Fetch the data
gold_price <- read_csv(url)
## Rows: 70 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Date
## dbl (1): Price
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# View the first few rows of the data
head(gold_price)
## # A tibble: 6 × 2
##   Date    Price
##   <chr>   <dbl>
## 1 1950-12  34.7
## 2 1951-12  34.7
## 3 1952-12  34.8
## 4 1953-12  34.8
## 5 1954-12  35.0
## 6 1955-12  35.0
# Arrange the data based on year in descending order.
gold_price <- gold_price %>%
  mutate(Date = as.Date(paste0(Date, "-01"), format = "%Y-%m-%d")) %>%
  arrange(desc(Date))
# Process the data
gold_price_processed <- gold_price %>%
  arrange(Date) %>%
  mutate(
    YTD_avg = cummean(Price),
    SixYear_MA = slide_dbl(.x = Price, .f = ~mean(.), .before = 5, .complete = TRUE)
  )

# Print the processed data
print(gold_price_processed)
## # A tibble: 70 × 4
##    Date       Price YTD_avg SixYear_MA
##    <date>     <dbl>   <dbl>      <dbl>
##  1 1950-12-01  34.7    34.7       NA  
##  2 1951-12-01  34.7    34.7       NA  
##  3 1952-12-01  34.8    34.7       NA  
##  4 1953-12-01  34.8    34.8       NA  
##  5 1954-12-01  35.0    34.8       NA  
##  6 1955-12-01  35.0    34.8       34.8
##  7 1956-12-01  34.9    34.8       34.9
##  8 1957-12-01  35.0    34.9       34.9
##  9 1958-12-01  35.1    34.9       35.0
## 10 1959-12-01  35.0    34.9       35.0
## # ℹ 60 more rows

The slide_dbl function from the slider package is used to compute a rolling average.
* .before = 5 means the calculation will consider the current year and the 5 years before it, giving us a total of 6 years for each average calculation.
* .complete = TRUE ensures we only get an average for periods where we have data for all 6 years. If there’s data for fewer than 6 years, the result for that period will be NA (meaning “Not Available”).

ggplot(data = gold_price_processed, aes(x = Date)) +
  geom_line(aes(y = Price, color = "Price")) +
  geom_line(aes(y = YTD_avg, color = "YTD Average")) +
  geom_line(aes(y = SixYear_MA, color = "Six-Year MA")) +
  labs(title = "Gold Price Over Time",
       y = "Price in USD",
       x = "Date",
       color = "Metric") +
  theme_minimal()
## Warning: Removed 5 rows containing missing values (`geom_line()`).

#### Explanation:
- Price: Shows raw fluctuations over time.
- YTD Average: Indicates intra-year performance.
- Six-Year MA: Highlights longer-term trends and smoothens short-term noise.

Conclusion:

The Six-Year MA line for gold prices shows a steady upward trend, indicating a long-term rise in gold prices over the period. While it has crossed the actual price line multiple times, by around 2020, the actual gold price is above the moving average, suggesting a recent surge in gold prices compared to its six-year historical average.