【问题标题】:nls boot error must have positive lengthnls 引导错误必须具有正长度
【发布时间】:2018-08-20 21:27:16
【问题描述】:

我在使用 nlsBoot() 时收到以下错误,知道有什么问题吗?

Error in apply(tabboot, 1, quantile, c(0.5, 0.025, 0.975)) : 
  dim(X) must have a positive length

set.seed(1)
x = 1:100
y = x^2+rnorm(100,50,500)
plot(x,y)
d = data.frame(x =x, y=y)
mymodel = nls(y~x^b,start= list(b=1),data = d)
mymodel
library(nlstools)
nlsBoot(mymodel, niter = 999)

【问题讨论】:

    标签: r nls


    【解决方案1】:

    尝试在应用 nls 函数之前定义公式,如下所示:

    formula <- as.formula(y ~ x^b)
    mymodel <-  nls(formula,start= list(b=1),data = d)
    

    添加

    好吧,我已经修改了代码,现在它可以处理一个参数拟合。

    # My suggestion is to erase all the environment first:
    rm(list = ls())
    # Then we start again:
    set.seed(1)
    x = 1:100
    y = x^2+rnorm(100,50,500)
    plot(x,y)
    d = data.frame(x =x, y=y)
    mymodel = nls(y~x^b,start= list(b=1),data = d)
    

    这是您必须使用的功能:

    nlsboot_onepar <- function (nls, niter = 999) 
    {
    if (!inherits(nls, "nls")) 
    stop("Use only with 'nls' objects")
    data2 <- eval(nls$data, sys.frame(0))
    fitted1 <- fitted(nls)
    resid1 <- resid(nls)
    var1 <- all.vars(formula(nls)[[2]])
    l1 <- lapply(1:niter, function(i) {
    data2[, var1] <- fitted1 + sample(scale(resid1, scale = FALSE), 
                                      replace = TRUE)
    nls2 <- try(update(nls, start = as.list(coef(nls)), 
                       data = data2), silent = TRUE)
    if (inherits(nls2, "nls")) 
      return(list(coef = coef(nls2), rse = summary(nls2)$sigma))
    })
    if (sum(sapply(l1, is.null)) > niter/2) 
    stop(paste("Procedure aborted: the fit only converged in", 
               round(sum(sapply(l1, is.null))/niter), "% during bootstrapping"))
    tabboot <- sapply(l1[!sapply(l1, is.null)], function(z) z$coef,simplify = 
    FALSE)
    tabboot <- as.matrix(t(as.numeric(tabboot)))
    rownames(tabboot) <- "b"
    rseboot <- sapply(l1[!sapply(l1, is.null)], function(z) z$rse)
    recapboot <- t(apply(tabboot, 1, quantile, c(0.5, 0.025, 
                                               0.975)))
    colnames(recapboot) <- c("Median", "2.5%", "97.5%")
    estiboot <- t(apply(tabboot, 1, function(z) c(mean(z), sd(z))))
    colnames(estiboot) <- c("Estimate", "Std. error")
    serr <- sum(sapply(l1, is.null))
    if (serr > 0) 
    warning(paste("The fit did not converge", serr, "times during 
    bootstrapping"))
    listboot <- list(coefboot = t(tabboot), rse = rseboot, bootCI = recapboot, 
                   estiboot = estiboot)
    class(listboot) <- "nlsBoot"
    return(listboot)
    }
    

    然后我们使用它:

    result <- nlsboot_onepar(mymodel, niter = 999)
    

    如果要绘制参数分布,可以这样做:

    graphics.off()
    plot(density(as.vector(result$coefboot)))
    # or
    hist(as.vector(result$coefboot))
    

    希望对你有帮助。

    【讨论】:

    • 这个函数不处理只有一个参数的公式(我已经检查过了)。您可以尝试在原始代码中添加另一个参数(无需先定义公式然后调整模型的步骤),例如 y~x^b + mu 或 y~ a*x^b
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    相关资源
    最近更新 更多