logo

Introduction

The aim of this project is to explore a time series using Meta’s Prophet forecasting model. Time series models are widely used in finance to analyse trends and make predictions about future values.

That is why I have chosen to analyse gold prices and use Prophet to forecast future prices. I chose gold because it is often seen as a safe-haven asset, so its price can be affected by economic uncertainty and major world events (relevant to today’s events).

Data Collection and Preparation

I obtained historical gold price data from Yahoo Finance using the quantmod package.

I chose to use data from 2018 onwards. This period begins before the COVID-19 pandemic and also includes more recent periods of global uncertainty, both of which are relevant when studying gold prices.

library(prophet)
library(quantmod)
library(ggplot2)
getSymbols("GC=F", src = "yahoo", from = "2018-01-01")
## Warning: GC=F contains missing values. Some functions will not work if objects
## contain missing values in the middle of the series. Consider using na.omit(),
## na.approx(), na.fill(), etc to remove or replace them.
## [1] "GC=F"
gold_prices <- data.frame(
  ds = index(`GC=F`),
  y = as.numeric(Cl(`GC=F`))
)

gold_prices <- na.omit(gold_prices)

head(gold_prices)
##           ds      y
## 1 2018-01-02 1313.7
## 2 2018-01-03 1316.2
## 3 2018-01-04 1319.4
## 4 2018-01-05 1320.3
## 5 2018-01-08 1318.6
## 6 2018-01-09 1311.7

The data was converted into a dataframe with two columns, ds and y, as required by Prophet. The ds column contains the dates and the y column contains the corresponding gold prices.

Analysis (exploring further…)

To understand the series better, I first plotted the gold prices over time.

ggplot(gold_prices, aes(x = ds, y = y)) +
  geom_line() +
  labs(
    title = "Gold Prices Over Time",
    x = "Date",
    y = "Closing Price"
  )

The plot shows that gold prices do not stay around a fixed level. There is an overall upward trend, especially after 2020, but there are also noticeable fluctuations over shorter periods. This is typical of financial data. We can also observe a noticeable upward trend after 2025, which has been a hot topic in recent news.

The variability of the series also appears to change over time. In practice, as suggested, a logarithmic transformation could be used to help stabilise the variance, although this was not explored further in this project.

Analysis of COVID-19 Period

To better understand how gold prices behaved during a period of economic uncertainty, I focus on the time-frame surrounding the COVID-19 pandemic.

covid_period <- subset(gold_prices, ds >= "2019-01-01" & ds <= "2022-12-31")

ggplot(covid_period, aes(x = ds, y = y)) +
  geom_line() +
  geom_vline(xintercept = as.Date("2020-03-01"), linetype = "dashed") +
  labs(
    title = "Gold Prices During the COVID-19 Period",
    x = "Date",
    y = "Closing Price"
  )

The plot shows a noticeable increase in gold prices during 2020, which coincides with the onset of the COVID-19 pandemic. This period was characterised by high economic uncertainty, leading investors to shift towards safe-haven assets such as gold. We see this happening again in 2025/2026…

After this increase, prices remain relatively elevated compared to pre-2020 levels, suggesting a longer-term change in market behaviour rather than a short-term fluctuation.

Prophet Model

The Prophet model was fitted to the historical gold price data.

gold_model <- prophet(gold_prices)
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.

Prophet is designed to capture the main trend in a time series and any seasonal patterns that may be present.

Forecasting

Future dates were generated so that gold prices could be forecast for the next 365 days.

future_gold_dates <- make_future_dataframe(gold_model, periods = 365)
tail(future_gold_dates)
##              ds
## 2425 2027-03-14
## 2426 2027-03-15
## 2427 2027-03-16
## 2428 2027-03-17
## 2429 2027-03-18
## 2430 2027-03-19

Predictions were then made using the fitted model.

gold_forecast <- predict(gold_model, future_gold_dates)

tail(gold_forecast[, c("ds", "yhat", "yhat_lower", "yhat_upper")])
##              ds     yhat yhat_lower yhat_upper
## 2425 2027-03-14 6149.759   5914.163   6434.830
## 2426 2027-03-15 6140.571   5915.406   6395.695
## 2427 2027-03-16 6141.369   5901.259   6426.304
## 2428 2027-03-17 6143.359   5872.103   6424.455
## 2429 2027-03-18 6141.628   5890.305   6420.863
## 2430 2027-03-19 6141.705   5899.671   6440.920

The forecast output includes the predicted value yhat, together with lower and upper uncertainty bounds. These bounds are useful because they show that the future path of gold prices is uncertain rather than fixed.

Forecast Visualisation

The historical data and forecast are shown below.

plot(gold_model, gold_forecast)

This plot shows the fitted values for the observed data together with the forecast for the following 365 days. The uncertainty band becomes wider further into the future, which is reasonable because long-range forecasts are less certain.

Forecast Components

The Prophet model can also be broken down into its main components.

prophet_plot_components(gold_model, gold_forecast)
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## ℹ The deprecated feature was likely used in the prophet package.
##   Please report the issue at <https://github.com/facebook/prophet/issues>.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

The components plot shows the estimated trend and any seasonal effects identified by the model. For a financial series such as gold, the trend is the main feature to focus on.

Conclusion

In this project, I used Meta’s Prophet model to analyse and forecast gold prices. The series showed clear changes in trend over time, together with short-run volatility. Prophet provided a straightforward way to fit the historical data and generate a one-year forecast.

This project also shows that financial time series can be difficult to predict, since prices are influenced by uncertainty and external events. One limitation is that the model assumes future patterns will follow past trends, which may not hold in financial markets where sudden events can occur. Even so, Prophet is a useful tool for exploring how a time series behaves and for producing a simple forecast and this was a particularly exciting project as being able to visually see the graphs fluctuating and increasing over time is very fascinating.