2016-08-26 7 views
1

私は自分のRMarkdownスクリプトとよく討議されている順序ロジスティック回帰近似の問題を抱えています(その理由はherehereです)。私はmaxintを100に増やしてみたところ、hereは効果がありません。r - polrをうまく失敗させる方法

これまで数百のモデルのうち1つのモデルでしか失敗していないように見えるので、全体的なコードは問題ありません。エラーを壊す

スクリプトはです:

Error in polr(a, data = rData, Hess = TRUE) : 
    attempt to find suitable starting values failed 
In addition: Warning messages: 
1: glm.fit: algorithm did not converge 
2: glm.fit: fitted probabilities numerically 0 or 1 occurred 

私の質問はです。モデルがうまくフィットせず、cat(Model XY doesn't converge, moving on)のようなものを報告し、入力リストの次のモデルを続ける方法がありますか?

私のように見える、おそらく1、これは条件テストで呼び出しをラップ伴います疑い:これはを失敗しているデータとモデルである

if(polr(...) == FAILS) { 
    cat(message) 
    return() # exit out of current iteration 
} else { 
    polr(... # run the model fit normally 
} 

## Raw Data 
T_P <- c(32,34,31,24,40,21,30,31,25,31,18,32,26,26,27,35,22,32,27,28) 
T_M <- c(16,6,12,12,13,10,14,14,11,13,5,13,9,13,11,18,11,15,12,13) 
E <- c(10,15,11,15,15,8,14,13,15,12,9,11,13,15,9,15,6,13,6,15) 
Q13.1.2 <- c(5,4,5,5,4,4,5,4,3,5,3,4,3,5,4,4,4,5,5,4) # categorical response 

## Dataframe of data 
rData <- data.frame(T_P,T_M,E,Q13.1.2) 

d <- "Q13.1.2"  # dependant variable 
c <- "T_P + T_M + E" # sting of covariates 
a <- str_c("as.factor(",d,") ~ ", c, sep="") # concat depVar & indVars into model alogrithm 

m <- polr(a, data=rData, Hess=TRUE) # build model 

答えて

0

が判明Rには何らかのエラー処理があります(here参照)。このロジックに従うと、この例を覚えているのは実際にはループの一部です:

tryCatch({ 
      # this is the test (just run the call in question) 
      polr(a, data=rData, Hess=TRUE) 
     }, warning = function(w) { 
      # this is how tryCatch handles a stop event 
      message("Model doesn't fit") # message to console 
      modelFail <<- TRUE # set a variable to test     
     }, error = function(e) { 
      # this is tryCatch handing an error event 
      message("error!") 
     }, finally = { 
      # this is what to do in the event of an.. event 
      # I'm not using this approach 
     }) 

if(exists("modelFail")){ 
    cat("DON'T PANIC! The model fit failed, moving on...") 
    rm(modelFail) # I clear the flag to reuse in case of another failure in loop 
    return() 
} else { 
    # now it is safe to run the fit as we have handled an error above     
    m <- polr(a, data=rData, Hess=TRUE) # build model 
} 
+1

初歩的なものに比べて何か? – nrussell

関連する問題