gpt4 book ai didi

r - r-如何让polr正常失败

转载 作者:行者123 更新时间:2023-12-03 07:58:57 24 4
gpt4 key购买 nike

我的RMarkdown脚本和有序Logistic回归拟合的迭代应用程序存在问题,对此进行了充分讨论(其原因是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

最佳答案

事实证明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
}

关于r - r-如何让polr正常失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39171527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com