R nls引导错误必须具有正长度

R nls引导错误必须具有正长度,r,nls,R,Nls,我用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),da

我用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)

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

formula <- as.formula(y ~ x^b)
mymodel <-  nls(formula,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)
}

我希望这能对您有所帮助。

当我这样做时,我在nlsBoot(mymodel,niter=999)中得到一个错误:过程中止:拟合在引导过程中仅收敛于1%。此函数不处理只有一个参数的公式(我已检查)。您可以尝试在原始代码中添加另一个参数(无需先定义公式,然后调整模型),如y~x^b+mu或y~a*x^b
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))