提问人:Ivan 提问时间:6/13/2023 最后编辑:Ivan 更新时间:6/14/2023 访问量:35
如何在 R 中跳过错误并计算错误时间
How to skip error and calculate error times in R
问:
我尝试使用 Muggeo(2003) 方法测试分段回归的统计功效。
以下是我计算 structbreak 时间的代码。该方法是牛顿方法,其目的是收敛到全局最大值,但有时它会失败并返回错误消息。当 structbreak 的 times 等于零时,也会返回错误。
library(segmented)
breakout <- FALSE ; iteration <- 1 ; max_iter <- 1001
a <- matrix(0 , max_iter , 1)
b <- matrix(0 , max_iter , 1)
c <- matrix(0 , max_iter , 1)
repeat{
TS <- cumsum(rnorm(100,0,1))
idt <- 1:length(TS)
o1 <- lm(TS ~ idt)
os1 <- selgmented(o1, Kmax = 2, type = "bic")
a[iteration] <- ifelse(length(os1$psi[,1]) == 1 , 1 , 0)
b[iteration] <- ifelse(length(os1$psi[,1]) == 2 , 1 , 0)
c[iteration] <- ifelse(length(os1$psi[,1]) == 0 , 1 , 0)
iteration <- iteration + 1
if(iteration == max_iter) break }
当没有结构中断时,函数 selgmented 总是返回错误,循环将被中断。
答: 暂无答案
评论
tryCatch
segmented
selgmented()
repeat