R 插入符序列与给定度数的聚
我在近似多项式的次数上滚动一个循环,以使用插入符进行训练,
ds = 1:20
for(i in 1:length(ds)){
print(i)
d=ds[i]
fit = train(y~poly(x,degree=d),data=training,method="lm",trControl=fitCtrl)
# other operations
}
运行代码给出的
Error in `[.data.frame`(data, 0, cols, drop = FALSE) :
undefined columns selected
使用 d=4 不起作用,但修复调用中的次数(即 Degree=4)是有效的。
你猜猜这里发生了什么吗?
谢谢!
编辑:
library(caret)
set.seed(1)
omega = 0.5*pi
xi = 0.5
phi = 0.5*pi
f = function(t)1-exp(-xi*omega*t)*sin(sqrt(1-xi^2)*omega*t+phi)/sin(phi)
sigma = 0.03
train.n = 100
x = seq(0,2*pi,by=2*pi/(train.n-1))
y = f(x)+rnorm(train.n,mean=0,sd=sigma)
training = data.frame(x=x,y=y)
fitCtrl <- trainControl(method = "LOOCV",verboseIter = FALSE)
ds = 1:20
for(i in 1:length(ds)){
print(i)
d=4
fit=train(y~poly(x,degree=4),data=training,method="lm",trControl=fitCtrl)
}
I'm rolling a loop over the degree of the approximating polynomial for training with caret
ds = 1:20
for(i in 1:length(ds)){
print(i)
d=ds[i]
fit = train(y~poly(x,degree=d),data=training,method="lm",trControl=fitCtrl)
# other operations
}
running the code gives
Error in `[.data.frame`(data, 0, cols, drop = FALSE) :
undefined columns selected
using d=4 doesn't work, but fixing the degree in the call, i.e. degree=4, works.
Any guess of what's going on here?
Thanks!
EDIT:
library(caret)
set.seed(1)
omega = 0.5*pi
xi = 0.5
phi = 0.5*pi
f = function(t)1-exp(-xi*omega*t)*sin(sqrt(1-xi^2)*omega*t+phi)/sin(phi)
sigma = 0.03
train.n = 100
x = seq(0,2*pi,by=2*pi/(train.n-1))
y = f(x)+rnorm(train.n,mean=0,sd=sigma)
training = data.frame(x=x,y=y)
fitCtrl <- trainControl(method = "LOOCV",verboseIter = FALSE)
ds = 1:20
for(i in 1:length(ds)){
print(i)
d=4
fit=train(y~poly(x,degree=4),data=training,method="lm",trControl=fitCtrl)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的问题实际上是
caret
在底层公式上使用all.vars()
来创建建模所需的数据帧。正如您所看到的,d
被认为是这些变量之一。通常,可以通过在公式中使用
I()
或围绕公式使用force()
来解决这些问题,但不能使用all.vars()< /代码>。
解决此问题的唯一方法是不在公式中发送
d
,而是预先将其设置为数字。在循环中使用
as.formula(paste0("y ~ poly(x, Degree=", d, ")")
将实现这一点(@akrun 也建议)。这是一个工作基于您的代码的示例:
由 reprex 包于 2022 年 3 月 20 日创建 (v2.0.1)
The problem here is actually that
caret
is usingall.vars()
on your formula under the hood to create the dataframe needed for modeling. As you can see,d
is thought to be one of these variables.Typically, one could solve these issues with the use of
I()
in the formula orforce()
around it, but not withall.vars()
.The only way to fix this is to not send in
d
in your formula, but have it be a number beforehand.Using
as.formula(paste0("y ~ poly(x, degree=", d, ")")
in your loop will achieve this (as also suggested by @akrun).Here is a working example based on your code:
Created on 2022-03-20 by the reprex package (v2.0.1)
我们可以使用
paste
在这里创建公式-output
通过循环,我们可能需要将输出存储在
list
中-output
We may use
paste
to create the formula here-output
With the loop, we may need to store the output in a
list
-output