5天了还是没有回复
- 从西蒙的评论中可以看出,这是一个可重现且非常奇怪的问题。似乎只有当具有非常高预测能力的逐步回归被包装在函数中时才会出现这个问题。
我已经为此苦苦挣扎了一段时间,任何帮助将不胜感激。我正在尝试编写一个运行多个逐步回归并将其全部输出到列表的函数。但是,R 在读取我在函数参数中指定的数据集时遇到问题。我在各种板上发现了几个类似的错误(here http://www.mail-archive.com/r-help@r-project.org/msg75827.html, here https://stat.ethz.ch/pipermail/r-help/2009-February/188468.html, and here https://stackoverflow.com/questions/9161273/use-stepaic-on-a-list-of-models),但是这些问题似乎都没有得到解决。这一切都归结为在用户定义函数中调用 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)
EDIT- 在有人问之前,所有的summary()函数都在那里,因为完整的函数(我编辑了它,以便您可以专注于错误)有另一个部分,它定义了一个可以向其输出逐步跟踪的文件。我刚刚摆脱了他们
EDIT 2- 会话信息
会话信息()
R版本2.15.1 (2012-06-22)
平台:x86_64-pc-mingw32/x64(64位)
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
EDIT 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)