R语言中的伪代码

R语言中的伪代码,r,pseudocode,R,Pseudocode,我刚开始使用“wbs”包,我想如果我在使用包之前在r中为它编写伪代码,我可以更好地理解它。因此,我目前正在将野生二进制分割伪代码转化为R编程语言材料 function WildBinSeg(s, e, ζT) if e−s < 1 then STOP else Ms,e := set of those indices m for which [sm,em] ∈ FM T is such that [sm,em] ⊆ [s,e]

我刚开始使用“wbs”包,我想如果我在使用包之前在r中为它编写伪代码,我可以更好地理解它。因此,我目前正在将野生二进制分割伪代码转化为R编程语言材料

function WildBinSeg(s, e, ζT) 
    if e−s < 1 then 
        STOP 
    else 
        Ms,e := set of those indices m for which [sm,em] ∈ FM T is such that [sm,em] ⊆ [s,e] 
        (Optional: augment Ms,e := Ms,e ∪{0}, where [s0,e0] = [s,e]) 
        (m0,b0) := argmaxm∈Ms,e,b∈{sm,...,em−1}| ˜ Xb sm,em| 
        if | Xb0 sm0,em0| > ζT then 
            add b0 to the set of estimated change-points 
            WildBinSeg(s, b0, ζT) 
            WildBinSeg(b0 + 1, e, ζT) 
        else 
            STOP 
        end if 
    end if 
end function

我知道这是一个函数,伪代码是一个函数,但我不确定是否应该为这行创建另一个函数,因为它有两个命令。有人能帮我写这行吗

您首先需要编写一个函数,该函数将绘制M个区间,并在每个区间中找到CUSUM统计绝对值的最大值(有关详细信息,请参阅WBS论文)

在下面的R代码中,我假设这样一个函数的输出存储在'res'变量中,这是一个4×M的矩阵。前两列包含绘制的区间(sm,em)的左端点和右端点,第三列是每个区间(bm)的CUSUM统计的最大值,最后一列包含相应CUSUM统计的值(Xbm sm,em)


WildBinSeg如果没有
fmt
ζT
的定义,我发现你的代码很难理解。似乎少了一些字符。对我来说,类似乳胶的下标,如
s_m
e_{m-1}
或带有
e^{m-1}
的上标会有很大帮助。
    Ms,e := set of those indices m for which [sm,em] ∈ FM T is such that [sm,em] ⊆ [s,e] 
WildBinSeg <- function(s, e, threshold,res){
    if(e-s <1) return(NULL)
    else{
        #we leave only intervals contained in [s,e]
        res <- res[res[,1]>= s & res[,2]<= e,,drop=FALSE]
        #place for the optional augmentation

        #check if there are any intervals left
        if(nrow(res)==0) return(NULL)
        else{           
            #we find the maximum
            max.id <- which.max(abs(res[,4]))
            b0 <- res[max.id,3]
            Xb0 <- res[max.id,4]

            if(abs(Xb0) > threshold)
                return(c(
                                WildBinSeg(s, b0, threshold,res),
                                b0,
                                WildBinSeg(b0+1,e, threshold,res)
            ))
            else return(NULL)   
        }

    }
}
require(wbs)
set.seed(12)
#we generate a piecewise constant function plus Gaussian noise vector
x <- rnorm(1000)
x[1:500] <- x[1:500]+1

res <- wbs(x)$res[,1:4]
#we set the threshold to the recommended value
threshold <- 1.3*sqrt(2*log(1000))



WildBinSeg(1,1000,threshold,res)