我可以运行以下代码:
fit1 <- ses(books[,1], alpha=0.99, initial="simple", h=4) #provide alpha manually
sse = sum((books[,1] - fitted(fit1))) # the one-step-ahead within-sample forecast errors
在上面代码的背面,我想创建100个对象并尝试使用for循环(在代码下面)。
for(i in seq(0,1,by=0.01))
{
paste("fit", i, sep = "_") <- ses(books[,1], alpha=i, initial="simple", h=4) #provide alpha manually
paste("sse", i, sep = "_") = sum((books[,1] - fitted(paste("fit", i, sep = "_")))) # the one-step-ahead within-sample forecast errors
}
我收到以下错误,
**Error in paste("fit", i, sep = "_") <- ses(books[, 1], alpha = i, initial = "simple", :
target of assignment expands to non-language object**
上面的代码是创建多个对象fit_0.01,fit_0.02 ....
这个循环可以工作吗?
答案 0 :(得分:0)
您可以通过assign
运算符的方式执行循环:
for(i in seq(0, 1, by=0.1)) {
assign(paste("fit", i, sep = "_"), ses(books[,1], alpha=i, initial="simple", h=4))
}