5

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 のビルドが開始されるまで、すべてが正常に動作すると判断しました。どういうわけか、一時オブジェクト「データセット」は最初のステップワイズ回帰では問題なく機能しますが、2 番目の回帰では認識されません。以下に示すように、関数の一部をコメントアウトすることでこれを見つけました。このコードは正常に実行され、オブジェクト「データセット」が最初に認識されていたことを証明します。

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() 関数がそこにありました。これは、完全な関数 (エラーに集中できるように編集したもの) には、段階的なトレースを出力できるファイルを定義する別の部分があるためです。私はちょうどそれらを取り除きました

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 - 関数を使用せずに、関数と同じ操作をすべて実行します。これは、アルゴリズムが収束しない場合でも、毎回正常に実行されます。

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)
4

1 に答える 1

5

do.call呼び出し環境でデータセットを参照するために使用するとうまくいきます。元の提案については、https://stackoverflow.com/a/7668846/210673を参照してください。動作するバージョンを次に示します (sinkコードが削除されています)。

stepModel2 <- function(modeling.formula, dataset) {
  model.initial <- do.call("glm", list(modeling.formula,
                       family = "binomial",
                       data = as.name(dataset)))
  model.stepwise1 <- step(model.initial, direction = "backward")
  model.stepwise2 <- step(model.stepwise1, scope = ~.^2)
  list(modInitial = model.initial, modStep1 = model.stepwise1, modStep2 = model.stepwise2)
}

blah <- stepModel2(a~., dataset = "test.df")

set.seed(6)元のコードと一貫して失敗します。失敗する理由は、dataset変数がstep関数内に存在しないためです。 を作成する際には必要ありませんが、線形項を保持する場合にmodel.stepwise1必要です。これは、バージョンが失敗した場合です。ここで行うように、グローバル環境からデータセットを呼び出すと、この問題が修正されます。model.stepwise2model.stepwise1

于 2013-05-22T23:37:43.093 に答える