数据框示例:
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模型正在创建两个列表(pred
和se
)时间序列对象,而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()
1条答案
按热度按时间hmmo2u0o1#
问题很可能是由于按 Country 和 Species 的唯一值对 Dataframe 进行切片时
temp
为空。考虑在单个函数中泛化流程,然后使用by
或split
将 Dataframe 拆分为这些组,而不是嵌套for
循环。然后通过你定义的方法处理每个分割。这将让你处理 Dataframe 的列表,然后你可以在最后rbind
* 一次 *。