Prophet is a procedure for forecasting time series data where trends are able to be fit with yearly, weekly and daily seasonality.
The prophet procedure is an additive regression model with four main components:
Includes a piece wise growth trend, which is useful to detect changes in the trend not due to seasonality. This allows it to automatically detect these by selecting change points in the data.
These holidays are user in putted in order to account for changes in the data that would usually be irregular.
Prophet allows you to add a customized seasonality outside of the standard daily, weekly, quarterly and yearly.
## No documentation for 'add_seasonality' in specified packages and libraries:
## you could try '??add_seasonality'
Where m is the prophet object, name is the string name of the seasonality component, period is number of days in one period, and fourier.order is the number of fourier components to use.
This is useful when trying to forecast with more complex data as it allows you to specify the seasonality in order to achieve more accurate forecasting.
JohnsonJohnson is a data set of the quarterly earnings (dollars) per share of Johnson and Johnson stock between 1960 and 1980.
## Qtr1 Qtr2 Qtr3 Qtr4
## 1960 0.71 0.63 0.85 0.44
## 1961 0.61 0.69 0.92 0.55
## 1962 0.72 0.77 0.92 0.60
## 1963 0.83 0.80 1.00 0.77
## 1964 0.92 1.00 1.24 1.00
## 1965 1.16 1.30 1.45 1.25
## 1966 1.26 1.38 1.86 1.56
## 1967 1.53 1.59 1.83 1.86
## 1968 1.53 2.07 2.34 2.25
## 1969 2.16 2.43 2.70 2.25
## 1970 2.79 3.42 3.69 3.60
## 1971 3.60 4.32 4.32 4.05
## 1972 4.86 5.04 5.04 4.41
## 1973 5.58 5.85 6.57 5.31
## 1974 6.03 6.39 6.93 5.85
## 1975 6.93 7.74 7.83 6.12
## 1976 7.74 8.91 8.28 6.84
## 1977 9.54 10.26 9.54 8.73
## 1978 11.88 12.06 12.15 8.91
## 1979 14.04 12.96 14.85 9.99
## 1980 16.20 14.67 16.02 11.61
The purpose of my project is to predict whether past earnings are indicative of future earnings. I am going to use historical data in order to forecast future earnings per share predictions.
First when using prophet in R you need to download the relevant prophet libraries.
## Loading required package: Rcpp
## Loading required package: rlang
This loads the prophet package into R, or you can download the latest version using:
## Skipping install of 'prophet' from a github remote, the SHA1 (2a57e9d3) has not changed since last install.
## Use `force = TRUE` to force installation
Now to setup a data frame you need to take the original data and set the time as the x value and the data in the y column.
The function
time()returns the time series of dates for the data set.The function
as.yearmon()converts this time series into a vector of dates.
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
This puts the data frame in the prophet forecaster.
Creates a data frame with dates in the future for 100 quarterly periods. For this data set a quarterly frequency was used as it matches the historical data that was given and it allows the model to show the seasonality of fluctuations in EPS predictions which may not be able to be seen if this was predicted annually.
Function used to predict the future EPS values based on the future dates.
JohnsonJohnson.df2 <- data.frame(time = as.vector(time(JohnsonJohnson)), x = as.vector(JohnsonJohnson))
model <- lm(x ~ time, data = JohnsonJohnson.df2)
summary(model)##
## Call:
## lm(formula = x ~ time, data = JohnsonJohnson.df2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.1853 -1.1968 -0.5239 0.8742 5.1229
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.280e+03 5.926e+01 -21.61 <2e-16 ***
## time 6.522e-01 3.007e-02 21.69 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.671 on 82 degrees of freedom
## Multiple R-squared: 0.8515, Adjusted R-squared: 0.8497
## F-statistic: 470.3 on 1 and 82 DF, p-value: < 2.2e-16
The equation for the linear regression is,
\[Y[i] = 0.652X[i] - 1280 \]
, in simple English this would mean that for each year that passed the expected EPS increase is 0.652 dollars per share.
plot(JohnsonJohnson.df2$time, JohnsonJohnson.df2$x, type = 'l', main = "Johnson & Johnson EPS Time Series vs Linear Refression", xlab = "Year", ylab = "EPS")
model <- lm(x ~ time, data = JohnsonJohnson.df2)
lines(JohnsonJohnson.df2$time, fitted(model), type = 'l', col = 'red')Quarterly predictions:
Annual Predictions:
future25yearsyear<- make_future_dataframe(m, periods = 25, freq = "year")
forecast25yearsyear <- predict(m, future25yearsyear)
plot(m, forecast25yearsyear)Monthly Predictions:
future25yearsmonth<- make_future_dataframe(m, periods = 300, freq = "month")
forecast25yearsmonth <- predict(m, future25yearsmonth)
plot(m, forecast25yearsmonth)The only forecast that accurately predicts the models seasonality and shows this in the plot is the quarterly predictions. The annual predictions do not show any seasonality to the model which would be expected due to the cyclical nature of a companies earnings and the monthly predictions model looks over fitted as it is capturing too much of the seasonality and is therefore hard to read and utilize in order to see future EPS predictions.
JJfordecompose <- ts(JohnsonJohnson.df2$x,start=c(1960),frequency=4)
plot ( decompose ( JJfordecompose ) )The overall trend of the EPS is increasing, this shows that in the future it is likely to keep increasing and that the company is growing.
The seasonal component is very regular, indicating that the seasonal component repeats over this period which could be due to company cycles or economic fluctuations that may effect sales.
At the end of the period there is high randomness which could have been due to unexpected events that aren’t explained by trend or seasonal components.
I was unable to find accurate EPS values for the times after the data ends, current values were much smaller than the predictions due to the fact that the model only increases and doesn’t factor in world events. Therefore I would only use this model to predict short term EPS values for a company in order to make short term investment decisions.
References