R中Arima()函数xreg参数外生变量差分处理咨询
xreg in Manual Arima() Specifications (When d/D > 0) Great question—this is a super common pitfall when switching from the automated auto.arima() to manually specifying your ARIMA model with Arima(). Let’s break this down clearly:
Core Rule
When you manually set d > 0 (non-seasonal differencing) or D > 0 (seasonal differencing) in Arima(), you must explicitly apply the same differencing to your xreg covariates before passing them to the function. Unlike auto.arima(), which handles this transformation automatically under the hood, Arima() treats your input xreg as-is, with no implicit adjustments.
Why This Matters
Your ARIMA model with differencing is effectively modeling the differenced version of your response variable (e.g., diff(y, d) or seasonal-differenced y). To maintain a valid, interpretable relationship between the response and covariates, the covariates need to exist in the same "differenced space" as the response. Skipping this step means you’re regressing a differenced y against raw-level x values—this leads to inconsistent model estimates and unreliable forecasts.
Step-by-Step Implementation
1. Match Differencing to Your Model Specs
Apply exactly the same differencing (non-seasonal + seasonal) to your covariates as you do to your response variable:
- For
d=1: Compute 1st-order differences for each column inxreg(e.g.,diff(xreg_col, differences = 1)). - For
D=1with seasonal periodm: Compute seasonal differences for each column (e.g.,diff(xreg_col, lag = m, differences = 1)). - For combined
dandD: Apply non-seasonal differencing first, then seasonal differencing (the order doesn’t affect the final result, but consistency with your response variable’s differencing order helps avoid confusion).
2. Example Code
Let’s say we’re fitting a monthly ARIMA model with order=(1,1,0) and seasonal=(0,1,1) (1st non-seasonal differencing + 1st seasonal differencing, m=12):
library(forecast) # Sample time series data set.seed(123) y <- ts(rnorm(60), frequency = 12) x <- ts(rnorm(60), frequency = 12) # Apply matching differencing to both y and x y_diff <- diff(diff(y, differences = 1), lag = 12, differences = 1) x_diff <- diff(diff(x, differences = 1), lag = 12, differences = 1) # Fit manual ARIMA model with differenced xreg model <- Arima(y, order = c(1,1,0), seasonal = c(0,1,1), m=12, xreg = x_diff) # Prepare FUTURE covariates (use RAW, undifferenced values!) future_x <- ts(rnorm(12), frequency = 12) # 12 months of future x values # Generate forecast fc <- forecast(model, newxreg = future_x)
3. Forecast Note
When generating forecasts with forecast(), pass raw, undifferenced future covariate values to newxreg. The forecast() function will automatically apply the same differencing steps used in the model to these future values before generating predictions, then reverse the differencing to return forecasts in the original response scale.
Key Difference from auto.arima()
To recap for clarity:
auto.arima(): Automatically differencesxregto match the chosend/D, and accepts raw future covariates for forecasting.Arima(): Requires you to manually differencexregto match your specifiedd/D, but still accepts raw future covariates for forecasting (sinceforecast()handles the differencing step for prediction).
内容的提问来源于stack exchange,提问作者Mark

