Original link:tecdat.cn/?p=7327
Original source:Tuo End number according to the tribe public number
You’ll learn how to use Prophet (in R) to solve a common problem: forecasting your company’s daily orders for the next year.
Data preparation and exploration
Prophet works best with daily data as well as at least a year of historical data. We will use SQL to process the data to be predicted on a daily basis:
`select
`` date,
value
from modeanalytics.daily_orders
order by date`
Copy the code
We can pipe SQL query result sets into R data box objects. First, rename your SQL query to Daily Orders. Then, in R, we can pass the query result set to data frame DF using the following statement:
df <- datasets[["Daily Orders"]]
Copy the code
To quickly understand how many observations your data box contains, run the following statement:
#
dim(df)
Copy the code
There are two columns in the Prophet input DataFrame: a date and a value, respectively.
str(df)
Copy the code
In this example, you will need to do some manual date format conversions:
df <- mutate (
df,
date = ymd_hms(date) #
)
Copy the code
Now that you have the data ready to use with Prophet, plot it and examine the data before entering it into Prophet.
- Around May 2017, the trend trajectory changed markedly. By default, Prophet automatically detects such “trend points” and allows the trend to adjust appropriately.
- There is a distinct seasonality on a weekly and yearly basis. If the time series is longer than two cycles, Prophet will automatically adapt to both weekly and annual seasonality.
- The mean and variance of our observations increase over time.
The Box – Cox transformation
Often in forecasting, you explicitly choose a particular type of power transformation to apply to the data to eliminate noise, and then feed the data into the prediction model (for example, logarithmic transformation or square root transformation, etc.). However, it can sometimes be difficult to determine which transformation is right for your data.
Box-cox transformation is a data transformation used to evaluate a set of Lambda coefficients (λ) and select values that achieve the best approximation of normality.
If we plot the newly converted data with the unconverted data, we can see that the Box-Cox transformation eliminates the observed increase in variance over time:
To predict
After using Prophet to fit the model through the box-Cox transformed data set, you can now start making predictions about future dates. Now we can use the predict() function to predict each row in a future data frame.
forecast <- predict(m, future)
Copy the code
At this point, Prophet creates a new data box for the prediction variable containing the predicted value yhat for the future date under the column named.
plot(m, forecast)
Copy the code
In our example, our prediction looks like this:
If you want to visualize the individual predicted components, you can use plot_Components:
Predictions and component visualizations show that Prophet is able to accurately model underlying trends in the data, as well as accurately model weekly and yearly seasonality (for example, low order volumes on weekends and holidays).
Inverse Box – Cox transformation
Because Prophet is used for box-Cox converted data, you need to convert the predicted values back to their original units. To convert the new predicted value back to its original units, you will need to perform a Box-Cox reversal. ` `
Now that you have converted the predicted value back to its original units, you can visualize the predicted value with the historical value:
Most welcome insight
1. Use LSTM and PyTorch for time series prediction in Python
2. Long and short-term memory model LSTM is used in Python for time series prediction analysis
3. Time series (ARIMA, exponential smoothing) analysis using R language
4. R language multivariate Copula – Garch – model time series prediction
5. R language Copulas and financial time series cases
6. Use R language random wave model SV to process random fluctuations in time series
7. Tar threshold autoregressive model for R language time series
8. R language K-Shape time series clustering method for stock price time series clustering
Python3 uses ARIMA model for time series prediction