5天了还是没有回复
- 从西蒙的评论可以看出,这是一个可重现且非常奇怪的问题。似乎只有在将具有非常高预测能力的逐步回归包含在函数中时才会出现问题。
我已经为此苦苦挣扎了一段时间,任何帮助将不胜感激。我正在尝试编写一个运行多个逐步回归并将它们全部输出到列表的函数。但是,R 无法读取我在函数参数中指定的数据集。我在各种板上(此处、此处和此处)发现了几个类似的错误,但似乎都没有得到解决。这一切都归结为在用户定义的函数中调用 step() 的一些奇怪问题。我正在使用以下脚本来测试我的代码。多次运行整个过程,直到出现错误(相信我,它会的):
test.df <- data.frame(a = sample(0:1, 100, rep = T),
b = as.factor(sample(0:5, 100, rep = T)),
c = runif(100, 0, 100),
d = rnorm(100, 50, 50))
test.df$b[10:100] <- test.df$a[10:100] #making sure that at least one of the variables has some predictive power
stepModel <- function(modeling.formula, dataset, outfile = NULL) {
if (is.null(outfile) == FALSE){
sink(file = outfile,
append = TRUE, type = "output")
print("")
print("Models run at:")
print(Sys.time())
}
model.initial <- glm(modeling.formula,
family = binomial,
data = dataset)
model.stepwise1 <- step(model.initial, direction = "backward")
model.stepwise2 <- step(model.stepwise1, scope = ~.^2)
output <- list(modInitial = model.initial, modStep1 = model.stepwise1, modStep2 = model.stepwise2)
sink()
return(output)
}
blah <- stepModel(a~., dataset = test.df)
这将返回以下错误消息(如果错误没有立即出现,请继续重新运行 test.df 脚本以及对 stepModel() 的调用,它最终会出现):
Error in is.data.frame(data) : object 'dataset' not found
我已经确定一切运行良好,直到 model.stepwise2 开始构建。不知何故,临时对象“数据集”在第一次逐步回归中工作得很好,但无法被第二次识别。我通过注释掉部分函数发现了这一点,如下所示。此代码将运行良好,证明对象“数据集”最初被识别:
stepModel1 <- function(modeling.formula, dataset, outfile = NULL) {
if (is.null(outfile) == FALSE){
sink(file = outfile,
append = TRUE, type = "output")
print("")
print("Models run at:")
print(Sys.time())
}
model.initial <- glm(modeling.formula,
family = binomial,
data = dataset)
model.stepwise1 <- step(model.initial, direction = "backward")
# model.stepwise2 <- step(model.stepwise1, scope = ~.^2)
# sink()
# output <- list(modInitial = model.initial, modStep1 = model.stepwise1, modStep2 = model.stepwise2)
return(model.stepwise1)
}
blah1 <- stepModel1(a~., dataset = test.df)
编辑 - 在任何人问之前,所有的 summary() 函数都在那里,因为完整的函数(我编辑了它以便你可以专注于错误)有另一个部分定义了一个文件,你可以输出逐步跟踪。我刚刚摆脱了他们
编辑 2 - 会话信息
sessionInfo() R 版本 2.15.1 (2012-06-22) 平台:x86_64-pc-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] tcltk stats graphics grDevices utils datasets methods base
other attached packages:
[1] sqldf_0.4-6.4 RSQLite.extfuns_0.0.1 RSQLite_0.11.3 chron_2.3-43
[5] gsubfn_0.6-5 proto_0.3-10 DBI_0.2-6 ggplot2_0.9.3.1
[9] caret_5.15-61 reshape2_1.2.2 lattice_0.20-6 foreach_1.4.0
[13] cluster_1.14.2 plyr_1.8
loaded via a namespace (and not attached):
[1] codetools_0.2-8 colorspace_1.2-1 dichromat_2.0-0 digest_0.6.2 grid_2.15.1
[6] gtable_0.1.2 iterators_1.0.6 labeling_0.1 MASS_7.3-18 munsell_0.4
[11] RColorBrewer_1.0-5 scales_0.2.3 stringr_0.6.2 tools_2.15
编辑 3 - 这执行与函数相同的所有操作,只是不使用函数。即使算法不收敛,这每次都会运行良好:
modeling.formula <- a~.
dataset <- test.df
outfile <- NULL
if (is.null(outfile) == FALSE){
sink(file = outfile,
append = TRUE, type = "output")
print("")
print("Models run at:")
print(Sys.time())
}
model.initial <- glm(modeling.formula,
family = binomial,
data = dataset)
model.stepwise1 <- step(model.initial, direction = "backward")
model.stepwise2 <- step(model.stepwise1, scope = ~.^2)
output <- list(modInitial = model.initial, modStep1 = model.stepwise1, modStep2 = model.stepwise2)