2017-05-14 189 views
0

我试着运行下面的代码。seq.default错误(a,length = max(0,b - a - 1)):长度必须是非负数

set.seed(307) 

y<- rnorm(200) 

h2=0.3773427 

t=seq(-3.317670, 2.963407, length.out=500) 

fit=density(y, bw=h2, n=1024, kernel="epanechnikov") 

integrate.xy(fit$x, fit$y, min(fit$x), t[407]) 

但是,我recived以下消息:

"Error in seq.default(a, length = max(0, b - a - 1)) : 
    length must be non-negative number" 

我不知道什么是错的。

当我使用t[406]t[408]如下我没有遇到任何问题:

integrate.xy(fit$x, fit$y, min(fit$x), t[406]) 

integrate.xy(fit$x, fit$y, min(fit$x), t[408]) 

有谁知道是什么问题,如何解决?请欣赏你的帮助。谢谢!

+0

“integrate.xy'包含什么包? – Gopala

+0

包是sfsmisc – user340803

+0

对于所有的R海报:如果使用上面的基R,总是总是在代码中包含'library()'行。永远不要假设我们有这样的包。 – Parfait

回答

0

我经历了integrate.xy函数的源代码,并且似乎有一个与使用xtol参数有关的错误。

作为参考,这里是integrate.xy函数的源代码:

function (x, fx, a, b, use.spline = TRUE, xtol = 2e-08) 
{ 
    dig <- round(-log10(xtol)) 
    f.match <- function(x, table) match(signif(x, dig), signif(table, 
                  dig)) 
    if (is.list(x)) { 
    fx <- x$y 
    x <- x$x 
    if (length(x) == 0) 
     stop("list 'x' has no valid $x component") 
    } 
    if ((n <- length(x)) != length(fx)) 
    stop("'fx' must have same length as 'x'") 
    if (is.unsorted(x)) { 
    i <- sort.list(x) 
    x <- x[i] 
    fx <- fx[i] 
    } 
    if (any(i <- duplicated(x))) { 
    n <- length(x <- x[!i]) 
    fx <- fx[!i] 
    } 
    if (any(diff(x) == 0)) 
    stop("bug in 'duplicated()' killed me: have still multiple x[]!") 
    if (missing(a)) 
    a <- x[1] 
    else if (any(a < x[1])) 
    stop("'a' must NOT be smaller than min(x)") 
    if (missing(b)) 
    b <- x[n] 
    else if (any(b > x[n])) 
    stop("'b' must NOT be larger than max(x)") 
    if (length(a) != 1 && length(b) != 1 && length(a) != length(b)) 
    stop("'a' and 'b' must have length 1 or same length !") 
    else { 
    k <- max(length(a), length(b)) 
    if (any(b < a)) 
     stop("'b' must be elementwise >= 'a'") 
    } 
    if (use.spline) { 
    xy <- spline(x, fx, n = max(1024, 3 * n)) 
    if (xy$x[length(xy$x)] < x[n]) { 
     if (TRUE) 
     cat("working around spline(.) BUG --- hmm, really?\n\n") 
     xy$x <- c(xy$x, x[n]) 
     xy$y <- c(xy$y, fx[n]) 
    } 
    x <- xy$x 
    fx <- xy$y 
    n <- length(x) 
    } 
    ab <- unique(c(a, b)) 
    xtol <- xtol * max(b - a) 
    BB <- abs(outer(x, ab, "-")) < xtol 
    if (any(j <- 0 == apply(BB, 2, sum))) { 
    y <- approx(x, fx, xout = ab[j])$y 
    x <- c(ab[j], x) 
    i <- sort.list(x) 
    x <- x[i] 
    fx <- c(y, fx)[i] 
    n <- length(x) 
    } 
    ai <- rep(f.match(a, x), length = k) 
    bi <- rep(f.match(b, x), length = k) 
    dfx <- fx[-c(1, n)] * diff(x, lag = 2) 
    r <- numeric(k) 
    for (i in 1:k) { 
    a <- ai[i] 
    b <- bi[i] 
    r[i] <- (x[a + 1] - x[a]) * fx[a] + (x[b] - x[b - 1]) * 
     fx[b] + sum(dfx[seq(a, length = max(0, b - a - 1))]) 
    } 
    r/2 
} 

给予xtol参数的值,被覆盖在所述线xtol <- xtol * max(b - a)。但变量的值是根据函数输入中给出的原始值xtol计算得出的。由于这种不匹配,函数在行bi <- rep(f.match(b, x), length = k)中返回x和b之间的任何匹配(即,NA)。这会导致您遇到的错误。

一个简单的修复,至少对于有问题的情况,将删除xtol <- xtol * max(b - a)行。但是,您应该向该软件包的维护人员提交一份错误报告,以进行更严格的修复。

相关问题