r - 如何让polr优雅地失败

时间:2016-08-26 17:14:10

标签: r error-handling r-markdown logistic-regression

我的RMarkdown脚本存在问题,并且序数逻辑回归拟合的迭代应用已得到充分讨论(其原因是)herehere。我也尝试将maxint增加到100,如建议here一样无效。

我知道整体代码很好,因为到目前为止,似乎只有几百种模型失败了。

脚本中断错误

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)的内容然后继续输入列表中的下一个模型?

我怀疑这将涉及在条件测试中包装调用,可能是一个看起来像:

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

1 个答案:

答案 0 :(得分:0)

事实证明,R中有一些错误处理,虽然不成熟(见here)。遵循这个逻辑,并记住该示例实际上是循环的一部分:

List
相关问题