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

循环遍历ARIMA模型报错:`[<-.ts`仅允许替换元素

多国家物种种群预测代码报错解决

问题重现

数据框示例

Country= c('Angola', 'Angola', 'Angola', 'Angola', 'Angola', 'Angola',  'Algeria', 'Algeria', 'Algeria', 'Algeria', 'Algeria', 'Algeria')
Year= c( 2000, 2000, 2001, 2001, 2002,2002, 2000, 2000, 2001, 2001, 2002,2002)
Species= c( 'Goats', 'Sheep ', 'Goats', 'Sheep',  'Goats', 'Sheep',  'Goats', 'Sheep',  'Goats', 'Sheep',  'Goats', 'Sheep') 
Pop= c(20, 30, 22, 34, 18, 35, 24, 26, 40, 30 , 23, 43)

data <- data.frame(Country, Year, Species, Pop)

预测代码

# Create a loop over each country
for (country in unique(data$Country)) {
  # Create a loop over each species
  for (species in unique(data$Species)) {
    # Filter the dataframe by country and species
    temp <- subset(data, Country == country & Species == species)
    # Fit the ARIMA model
    model <- auto.arima(temp$Pop)
    # Make the predictions
    pred <- predict(model, n.ahead = 10)
    # Store the predictions in a dataframe
    if (exists("newdata")) {
      newdata <- rbind(newdata, data.frame(
        Country = country, 
        Year = seq(max(temp$Year)+1, max(temp$Year)+10), 
        Species = species, 
        Predicted_Pop = pred)
      )
    } else {
      newdata <- data.frame(
        Country = country, 
        Year = seq(max(temp$Year)+1, max(temp$Year)+10),    
        Species = species, 
        Predicted_Pop = pred
      )
    }
  }
}

报错信息

Error in [<-.ts(*tmp*, ri, value = c(7990484, 7990484, 7990484, 7990484, :
only replacement of elements is allowed

问题描述

代码意图按国家和物种预测未来10年动物种群数量,单国家运行正常,但遍历多国家时报错。已知predict函数返回包含pred和se的时间序列对象,导致rbind无法合并数据框,尝试replace函数仍报错。

解决思路与修正代码

错误原因

predict.arima()返回的是列表对象,包含pred(预测值的时间序列)和se(标准误)。直接将这个列表赋值给数据框列时,第一次循环会生成包含时间序列类型的列,第二次循环尝试合并时,因数据类型不兼容触发报错。

关键修正

提取预测值的数值向量而非保留时间序列类型,同时提前初始化空数据框,避免依赖exists()检查减少环境变量干扰。

修正后完整代码

# 提前初始化空数据框并指定列类型
newdata <- data.frame(Country = character(), 
                      Year = integer(), 
                      Species = character(), 
                      Predicted_Pop = numeric(),
                      stringsAsFactors = FALSE)

# 循环遍历国家和物种
for (country in unique(data$Country)) {
  for (species in unique(data$Species)) {
    # 筛选对应国家和物种的数据
    temp <- subset(data, Country == country & Species == species)
    # 拟合ARIMA模型
    model <- auto.arima(temp$Pop)
    # 获取预测结果并提取数值型预测值
    pred_result <- predict(model, n.ahead = 10)
    pred_values <- as.numeric(pred_result$pred)
    # 生成临时预测数据框
    temp_pred <- data.frame(
      Country = country, 
      Year = seq(max(temp$Year)+1, max(temp$Year)+10), 
      Species = species, 
      Predicted_Pop = pred_values,
      stringsAsFactors = FALSE
    )
    # 合并到总数据框
    newdata <- rbind(newdata, temp_pred)
  }
}

额外说明

  • 若需要保留预测标准误,可在临时数据框中新增SE_Pop = as.numeric(pred_result$se)列;
  • 提前指定列类型能避免循环中因隐性类型转换导致的未知报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 23:45:47