In this project I will be analysing Tesla’s (TLSA) stock prices using
Meta’s Prophet forecasting system in R. All packages were installed in
the console prior to knitting this document. The packages used are
prophet, quantmod, zoo, and
dplyr.
I will be downloading Tesla’ stock data using the quantmod function in RStudio which is associated with Yahoo finance. This is the dataset I will be analysing during the project. I will use the quantmod function to abstract data from the 2023-2024 trading year and create a table with column heading “Date” and “Closing Prices”. This will allow me to fit the data into the prophet function later on.
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.3.3
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 4.3.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## [1] "TSLA"
I will check head, tails and summary of the data to analyse the specifics of the data
## Date ClosingPrice
## 1 2023-01-03 108.10
## 2 2023-01-04 113.64
## 3 2023-01-05 110.34
## 4 2023-01-06 113.06
## 5 2023-01-09 119.77
## 6 2023-01-10 118.85
## Date ClosingPrice
## 245 2023-12-21 254.50
## 246 2023-12-22 252.54
## 247 2023-12-26 256.61
## 248 2023-12-27 261.44
## 249 2023-12-28 253.18
## 250 2023-12-29 248.48
## Date ClosingPrice
## Min. :2023-01-03 Min. :108.1
## 1st Qu.:2023-04-03 1st Qu.:186.1
## Median :2023-07-04 Median :222.1
## Mean :2023-07-02 Mean :217.5
## 3rd Qu.:2023-10-01 3rd Qu.:253.0
## Max. :2023-12-29 Max. :293.3
We can see that trading opened on 3rd Jan 2023 with the stock around $108.10 and ends the trading year on 29th December 2023 at around $248.48, showing that Tesla more than doubled in stock value in a year. There was also a mean closing price of $217.50 and a median of $222.10, suggesting a roughly symmetric distribution and no extreme outliers skewing the average.
Researching the prophet function has shown me that the dataframe must have columns labelled “ds” and “y” for a model to be fitted. Therefore, I will look to rename my columns before fitting the model. The model will show the long-run stock trend for the Tesla stock, so ignoring weekly and daily seasonality would make the trend clearer.
## Loading required package: Rcpp
## Warning: package 'Rcpp' was built under R version 4.3.3
## Loading required package: rlang
In this section I will begin to forecast the data for the next 365 days beyond the original dataset using the make_future_dataframe and predict commands
tslaFutureDates <- make_future_dataframe(tslaModel, periods = 365)
tslaForecast <- predict(tslaModel, tslaFutureDates)
plot(tslaModel, tslaForecast)When forecasting stock data, it is important to extract individual trend and seasonality components to check how much of an effect this has on the data.
## 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 trend shows a consistent linear trajectory rising from $150 to a projected $420 by January 2025. The yearly seasonality also showed a strong mid-year rally around July, and a dip around October (approximately -$60). Prophet detected no structural breaks in the forecasting model, suggesting consistent growth in the stock in the next year.
To quantify the trend more precisely, I will fit a simple linear regression of closing price against a numeric time index.
tsla_readable$TimeIndex <- as.numeric(tsla_readable$Date)
tslaLinearModel <- lm(ClosingPrice ~ TimeIndex, data = tsla_readable)
summary(tslaLinearModel)##
## Call:
## lm(formula = ClosingPrice ~ TimeIndex, data = tsla_readable)
##
## Residuals:
## Min 1Q Median 3Q Max
## -58.172 -23.240 -0.602 20.657 71.433
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -5.329e+03 3.506e+02 -15.20 <2e-16 ***
## TimeIndex 2.838e-01 1.794e-02 15.82 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 29.63 on 248 degrees of freedom
## Multiple R-squared: 0.5023, Adjusted R-squared: 0.5003
## F-statistic: 250.3 on 1 and 248 DF, p-value: < 2.2e-16
The slope coefficient of 0.2838 indicates that TSLA’s closing price increased by approximately $0.28 per trading day on average across 2023, equivalent to around $71.52 of linear growth across the full year.
Forecasts may be misleading, especially with stock data that is sensitive to many factors such as geopolitics, supply contraints and developing consumer preferences. The trend growth forecasted shows a consistent linear pattern, however this should be treated with caution as Prophet extrapolates existing patterns forward and cannot account for unexpected events. Overall, this analysis demonstrates the capability of Meta’s Prophet as an accessible and powerful tool for time series forecasting, while also highlighting the inherent limitations of applying trend-based models to financial data.