You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言时间序列分解代码报错‘Error in ts[, x]: object of type 'closure' not indexable’问题咨询

Hey there! Let's work through this step by step to fix your error and get those residual plots looking great.

First, let's squash that error

The Error in ts[, x]: object of type 'closure' not indexable is happening because you're using ts[, x] in your code—but ts is a built-in R function (used to create time series objects), not your data frame df33! R gets confused when you try to index a function like it's a dataset.

Fix the time series decomposition code

Let's rewrite the decomposition part to correctly reference your df33 data frame, and make sure each column is properly converted to a time series object (adjust the frequency value to match your data—e.g., 12 for monthly, 4 for quarterly, 1 for annual):

# Decompose each time series in df33
ts_dec <- lapply(df33, function(series_col) {
  # Convert the column to a ts object with appropriate frequency
  ts_object <- ts(series_col, frequency = 12)
  # Run additive decomposition
  decompose(ts_object, type = "additive")
})

This uses lapply directly on df33, so each iteration works with one column of your data frame—no need to mess with column names for indexing here.

Extract and organize residuals for ggplot

Next, we'll pull out the remainder (residual) from each decomposition, and format everything into a long-format data frame (which ggplot loves):

# Extract residuals along with time stamps and series names
residuals_list <- lapply(names(ts_dec), function(series_name) {
  data.frame(
    Time = time(ts_dec[[series_name]]$x),  # Get the time axis from the original ts object
    Series = series_name,
    Residual = ts_dec[[series_name]]$remainder
  )
})

# Combine all residuals into a single data frame
residuals_df <- do.call(rbind, residuals_list)

Plot all residuals with ggplot

Now we can visualize all residuals. If you want to overlay all series in one plot:

library(ggplot2)

ggplot(residuals_df, aes(x = Time, y = Residual, color = Series)) +
  geom_line(alpha = 0.7) +  # Alpha makes overlapping lines easier to distinguish
  labs(
    title = "Residuals from Additive Time Series Decomposition",
    x = "Time",
    y = "Residual Value"
  ) +
  theme_minimal() +
  theme(legend.position = "bottom")  # Move legend to bottom for 50+ series

If you'd prefer each series in its own subplot (way cleaner for 50+ series!), use facet_wrap:

ggplot(residuals_df, aes(x = Time, y = Residual)) +
  geom_line() +
  facet_wrap(~Series, scales = "free_y") +  # Let y-axes scale independently per series
  labs(
    title = "Residuals for Each Time Series",
    x = "Time",
    y = "Residual Value"
  ) +
  theme_minimal()

Quick notes to keep in mind

  • Double-check the frequency argument in ts()—it needs to match your data's periodicity (monthly=12, quarterly=4, etc.).
  • If your data has missing values, decompose() might throw warnings. You can handle NAs first with something like na.interp() from the forecast package if needed.

内容的提问来源于stack exchange,提问作者isma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 01:32:31