使用astsa包拟合季节性ARIMA绘图时遇x/y长度不一致错误求解决
First, let's break down why this error might be happening with your seasonal ARIMA model plot. The error xy.coords(x, y, xlabel, ylabel, log) : 'x' and 'y' lengths differ typically means one of the plotting functions in sarima is trying to pair two vectors of unequal length—likely a mismatch between the residual values and their corresponding time stamps, often caused by edge cases in how the astsa package handles time series that don't start at the first period of their frequency (your data starts at week 18 of 2012, not week 1).
Here are actionable solutions to get your plots working:
1. Update the astsa Package to the Latest Version
Older versions of astsa had bugs related to plotting time series with non-starting periods. Updating often resolves this issue:
install.packages("astsa") # Reinstalls the latest version library(astsa) # Load the updated package
After updating, re-run your original sarima command and check if the plots render correctly.
2. Manually Generate the Required Plots (If Updating Doesn't Work)
If the error persists, you can bypass the sarima function's built-in plotting by extracting the model results and creating the plots yourself. This gives you full control and avoids the package's internal plotting quirks:
Step 1: Fit the Model Without Plotting
First, get your model output while skipping the plots:
fit <- sarima(turkey.ts, 0, 1, 1, 0, 1, 1, 52, details = FALSE)
Step 2: Extract Residuals
Pull the residuals from the fitted model—these should be a time series object matching your original data's time stamps:
resids <- fit$fit$residuals
Step 3: Recreate the Four Standard sarima Plots
Plot 1: Residuals vs. Time
plot(resids, type = "l", main = "Residuals Over Time", xlab = "Week", ylab = "Residual") abline(h = 0, col = "red", lty = 2)
Plot 2: ACF of Residuals
acf(resids, main = "ACF of Residuals", lag.max = 52*2) # Include 2 years of lags for seasonality
Plot 3: PACF of Residuals
pacf(resids, main = "PACF of Residuals", lag.max = 52*2)
Plot 4: Ljung-Box Test p-Values
This plot checks if residuals are white noise. We'll compute p-values for multiple lags and plot them:
# Choose lags (adjust based on your data frequency) lags <- seq(10, 60, by = 5) # Calculate p-values for Ljung-Box tests p_vals <- sapply(lags, function(lag) { Box.test(resids, type = "Ljung-Box", lag = lag)$p.value }) # Plot the results plot(lags, p_vals, ylim = c(0, 1), xlab = "Lag", ylab = "p-Value", main = "Ljung-Box Test p-Values") abline(h = 0.05, col = "red", lty = 2) # Significance threshold
3. Verify Your Time Series Structure
Double-check that your time series is correctly formatted to rule out any underlying data issues:
# Check basic structure str(turkey.ts) # Confirm number of observations (should be 139: 35 weeks in 2012 + 52 in 2013 +52 in2014) length(turkey.ts) # Verify time stamps match the data points head(time(turkey.ts)) tail(time(turkey.ts))
If the length or time stamps look off, re-import or redefine your time series to ensure consistency.
内容的提问来源于stack exchange,提问作者Rosanne Baars

