ARIMA(1,1,1)模型残差均值非零问题求助
Hey Lucas, let's work through why your ARIMA(1,1,1) model is producing residuals with a non-zero mean (0.3065) and how to fix this issue.
A non-zero mean in residuals tells us your model isn't fully capturing the underlying pattern in the data—specifically, there's a consistent offset the model isn't accounting for. For an ARIMA(1,1,1) model, this usually ties back to whether we're accounting for the mean of the differenced series.
1. First: Check the Differenced Data's Mean
Your ARIMA(1,1,1) uses first-order differencing (d=1) to make the data stationary. Let's check if the differenced GDP series has a non-zero mean—this is the most common culprit here:
# Calculate first difference of your GDP time series pib_diff <- diff(dadospib$pib) # Check the mean of the differenced data mean(pib_diff)
If this value is significantly different from zero, your default ARIMA(1,1,1) model (which doesn't include a constant term) won't account for this offset, leading to residuals with a non-zero mean.
2. Add a Constant Term to Your ARIMA Model
In R's arima() function, when d >= 1, the default is include.mean = FALSE. We can override this to let the model fit the mean of the differenced series:
# Fit ARIMA(1,1,1) with a constant term model_with_mean <- arima(dadospib$pib, order = c(1,1,1), include.mean = TRUE) # Check the new residual mean residuals(model_with_mean) %>% mean()
This should bring the residual mean close to zero, as the model now explicitly accounts for the baseline offset in the differenced data.
3. Validate the Model with Diagnostics
After adjusting the model, don't skip checking if the residuals behave like white noise:
- Plot the residual time series to ensure no obvious trends/seasonality:
plot(residuals(model_with_mean)) - Test for autocorrelation with the Box-Pierce test:
Box.test(residuals(model_with_mean), type = "Box-Pierce")(a non-significant p-value means no remaining autocorrelation) - Check normality with a Q-Q plot:
qqnorm(residuals(model_with_mean)); qqline(residuals(model_with_mean))
4. Let Auto-ARIMA Find the Optimal Specification
If adding a constant doesn't fix the issue, or you want to confirm you're using the best ARIMA parameters, use the auto.arima() function from the forecast package—it automatically selects the best order and decides whether a constant is needed:
library(forecast) # Since your data is seasonally adjusted, set seasonal = FALSE auto_model <- auto.arima(dadospib$pib, seasonal = FALSE) # View model details summary(auto_model) # Check residual mean residuals(auto_model) %>% mean()
This function uses AIC/AICc to pick the model that minimizes information loss, which often includes the right combination of AR/I/Q terms and a constant if needed.
5. Double-Check Data Preprocessing
Finally, confirm your seasonal adjustment was done correctly. Even adjusted data can have lingering seasonality or outliers that throw off the model. Use tsoutliers() to detect and handle any extreme values:
outliers <- tsoutliers(dadospib$pib) print(outliers)
If outliers are present, you can adjust the data before re-fitting your model.
内容的提问来源于stack exchange,提问作者Lucas Leal

