我今天在R代码中意识到了一种奇怪的行为.我尝试了一个包{boot.StepAIC},其中包含一个用于AIC逐步回归结果的bootstrap函数.但是我认为统计背景不是问题(我希望如此).
我可以使用R顶层的函数.这是我的示例代码.
require(MASS)
require(boot.StepAIC)
n<-100
x<-rnorm(n); y<-rnorm(n,sd=2); z<-rnorm(n,sd=3); res<-x+y+z+rnorm(n,sd=0.1)
dat.test<-as.data.frame(cbind(x,y,z,res))
form.1<-as.formula(res~x+y+z)
boot.stepAIC(lm(form.1, dat.test),dat.test) # should be OK - works at me
Run Code Online (Sandbox Code Playgroud)
但是,我想把它包装在一个自己的函数中.我将数据和公式传递给该函数.但我在boot.stepAIC()中遇到错误:
100个引导样本中的模型拟合失败strsplit中的错误(nam.vars,":"):非字符参数
# custom function
fun.boot.lm.stepAIC<-function(dat,form) {
if(!inherits(form, "formula")) stop("No formula given")
fit.lm<-lm(formula=form,data=dat)
return(boot.stepAIC(object=fit.lm,data=dat))
}
fun.boot.lm.stepAIC(dat=dat.test,form=form.1)
# results in an error
Run Code Online (Sandbox Code Playgroud)
那错误在哪里?我想它必须与当地和全球环境有关,不是吗?