5日とサイモンさんのコメントから分かるように、まだ無回答が
- ユーザー定義関数内)(オブジェクトエラーがありません、これは再現性があり、非常に奇妙な問題です。この問題は、非常に高い予測力を持つ階段状の回帰が関数にラップされている場合にのみ発生するようです。
私はしばらくこのことに苦労しており、どんな助けでも大歓迎です。私はいくつかの段階的回帰を実行し、それらのすべてをリストに出力する関数を作成しようとしています。しかし、Rは関数の引数で指定したデータセットの読み込みに問題があります。私はさまざまなボード(here、here、およびhere)でいくつかの同様のエラーが見つかりましたが、それらのどれも解決されていないようです。これは、ユーザー定義関数で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が構築されますし始めるまで、すべてがアップ正常に動作することを決定しました。どうやら、一時的なオブジェクト 'dataset'は最初の階段回帰ではうまく動作しますが、2番目の回帰では認識されません。私は以下のように関数の一部をコメントアウトすることでこれを見つけました。このコードは、オブジェクトのデータセットは、「元々認識されていたことを証明し、罰金を実行します:EDIT
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 2処分した - セッション情報
のSessionInfo() 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 - これは単に関数を使用することなく、機能として全て同じ動作を行います。これは、アルゴリズムが収束しない場合でも、細かいたびに実行されます:呼び出し元の環境に設定されているデータを参照するためにdo.call
を使用し
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)
このエラーを再現しようとしましたが、できませんでした。私はWindows 7でR 2.15.3 64ビットを実行していますが、関数は 'sink():'への呼び出しに達するまでうまくいっていました。あなたはあなたが使っているRのバージョンを教えてください。 – Simon
@imim - 私は関数にoutfile引数を戻します。これは私のためにエラーを再現させました。私は今質問にシステムとセッション情報を追加しています。 – zap2008
これは非常に奇妙です...エラーは一見無作為にポップアップしています。私のコードを10回実行すると、おそらく2回ポップアップします... – zap2008