ARIMA模型上的R循环错误在'[〈-.ts'('*tmp*',ri,只允许替换元素

clj7thdc  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(96)

数据框示例:

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)

预测每个国家和每个物种未来10年动物种群的环函数:

# 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
      )
    }
  }
}

这段代码可以在一个国家运行,但在循环时不起作用,我得到了以下错误:
[<-.ts中的错误(*tmp*,ri,值= c(7990484,7990484,7990484,7990484,:只允许更换元件
我知道arima模型正在创建两个列表(predse)时间序列对象,而rbind是不能将其合并为 Dataframe 的那个吗?任何指针都将受到赞赏。我尝试了replace()函数,但仍然得到同样的错误。谢谢!
我希望这个循环能产生一个未来10年每个国家和每个物种的动物种群列表。
由于@Parfait的建议,我在我的初始代码中使用了他的方法:

library(forecast) 
        results<- data.frame()
        
        # Create a loop over each country
        for (country in unique(data$Country)) {
           for (species in unique(data$Species)) {
        
        # Filter the dataframe by country and species
        temp <- subset(FAO.DB, Country == country & Species == species)
        
        # Check if the dataframe is empty
        if(nrow(temp) > 1){
        # Fit the ARIMA model
        model <- auto.arima(temp$Pop)
        
        # Make the predictions
        pred <- forecast(model, n.ahead = 10)
    
        # Store the predictions in a dataframe
        newdata <- data.frame( Country = country, 
                             Year = seq(max(temp$Year)+1, 
                                    max(temp$Year)+10), 
                             Species = species, 
                             Predicted_Pop = pred)
    
        # Store the predictions in a dataframe
        
        results <- rbind(results, newdata)
    
               }
             }
         }

我还将函数predict()更改为函数forecast()

hmmo2u0o

hmmo2u0o1#

问题很可能是由于按 CountrySpecies 的唯一值对 Dataframe 进行切片时temp为空。考虑在单个函数中泛化流程,然后使用bysplit将 Dataframe 拆分为这些组,而不是嵌套for循环。然后通过你定义的方法处理每个分割。这将让你处理 Dataframe 的列表,然后你可以在最后rbind * 一次 *。

# User-defined method
predict_population <- function(temp) {
  if (NROW(temp) > 1) {
    # Fit the ARIMA model
    model <- auto.arima(temp$Pop)
    # Make the predictions
    pred <- predict(model, n.ahead = 10)
  
    # Store the predictions in a dataframe
    newdata <- data.frame(
      Country = temp$Country[1], 
      Year = seq(max(temp$Year)+1, max(temp$Year)+10), 
      Species = temp$Species[1], 
      Predicted_Pop = pred
    )
  }
}

# APPROACH 1:
# Filter the dataframe by country and species with `by`  
df_list <- by(data, data[c("Country", "Species")], predict_population)

# APPROACH 2:
# Filter the dataframe by country and species with `split`
# Run method with `lapply`
df_list <- split(data, data[c("Country", "Species")]) |> lapply(predict_population)

# RBIND ALL DFs TO MASTER DATA FRAME
new_data <- do.call(rbind, unname(df_list))

相关问题