Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

estimateSd() does not check for sufficient number of finite data point #9

Open
HenrikBengtsson opened this issue Feb 27, 2018 · 0 comments

Comments

@HenrikBengtsson
Copy link

I got:

> fit <- c3co::segmentData(dat, stat = "TCN")
Error in Y[-c(n - 2, n - 1, n), , drop = FALSE] : subscript out of bounds

which happens in estimateSd():

> traceback()
8: estimateSd(Y)
7: sweep(Y, MARGIN = 2, STATS = estimateSd(Y), FUN = "/")
6: segmentByRBS(Y, K, ...)
5: segFUN(Yseg, ...)
4: eval(expr)
3: prof(segFUN(Yseg, ...), doit = profile)
2: jointSeg(Y = dataToSeg[ww, ], method = "RBS", K = 100, modelSelectionMethod = "Birge")
1: segmentData(dat2, stat = "TCN")

Troubleshooting

This is because estimateSd() assumes there are enough number of data points;

> print(estimateSd)
function (y, method = c("Hall", "von Neumann")) 
{
    method <- match.arg(method)
    if (method == "von Neumann") {
        y <- stats::na.omit(y)
        dy <- diff(y)
        Sd <- stats::mad(dy)/sqrt(2)
    }
    else if (method == "Hall") {
        Y <- as.matrix(y)
        n <- nrow(Y)
        wei <- c(0.1942, 0.2809, 0.3832, -0.8582)
        Y1 <- Y[-c(n - 2, n - 1, n), , drop = FALSE] * wei[1]
        Y2 <- Y[-c(1, n - 1, n), , drop = FALSE] * wei[2]
        Y3 <- Y[-c(1, 2, n), , drop = FALSE] * wei[3]
        Y4 <- Y[-c(1, 2, 3), , drop = FALSE] * wei[4]
        Sd <- sqrt(colMeans((Y1 + Y2 + Y3 + Y4)^2, na.rm = TRUE))
    }
    return(Sd)
}
<environment: namespace:jointseg>

Suggestion

Return NA_real_ whenever n <= 2 (or give an informative error message).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant