nlstools方法不接受nls对象

时间:2012-12-11 17:45:43

标签: r

我正在使用R中的nlstools包。我将模型拟合到我的数据并绘制好。我不知道我做错了什么,但我似乎无法从nlstools调用方法,因为我收到“对象'未找到”的错误。这是代码,错误行注释:

Y=y_coll
X=x_ntips
d=data.frame(X,Y)

thisfit=nls(Y ~ a*X^b,data=d, start = list(a = .1, b = .1)) 

### Error in eval(expr, envir, enclos) : object 'd' not found
#a=nlsBoot (thisfit, niter = 999)
#cr= nlsConfRegions(thisfit, exp = 2, length = 200) 

#plotting   
res= data.frame(X, pred = predict(thisfit)) 
points(res[order(X),], type='l', col="grey40", lty=1) 

关于发生了什么的任何想法? 感谢

4 个答案:

答案 0 :(得分:2)

好的,这就是我发现的:问题是当nlsBoot调用在函数内部时。下面的代码应该复制问题(nlsBoot找不到变量d):

library(nlstools)

call_thisFunction <- function(){

        X=c(69, 36, 135, 66, 10, 6, 15, 18)
        Y=c(0.10008780, 0.20840336, 0.08147234, 0.12500000, 0.19444444, 0.60000000, 0.21978022, 0.29411765)
        d<- data.frame(X=X,Y=Y)
        print(d)
        thisfit<- nls(Y ~ a*X^b,data=d, start = list(a = .1, b = .1)) 
        print("done NLS")
        a= nlsBoot(thisfit, niter = 50) #FAIL - Error in eval(expr, envir, enclos) : object 'd' not found

}

call_thisFunction()

我也注意到如果我将变量d设为全局:

d<<- data.frame(X=X,Y=Y) 

或者在调用之前在函数外部定义它:

(...)
d=c()
call_thisFunction()

一切运作良好。如果有人知道原因,请发布。 谢谢!

这是一个修复(注释掉的行来自原始行,并被后面的行替换):

# nlsBoot <- function(nls, niter = 999)
nlsBoot <- function (nls, niter = 999, envir = parent.frame())
{
    if (!inherits(nls, "nls")) 
        stop("Use only with 'nls' objects")

    # data2 <- eval(nls$data, sys.frame(0))
    data2 <- eval(nls$data, envir)
    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)
    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)
}

答案 1 :(得分:1)

我无法重现错误(发布为答案,而不是评论合适的代码格式)。

set.seed(101)
d <- data.frame(X=rlnorm(100),Y=runif(100))
thisfit=nls(Y ~ a*X^b,data=d, start = list(a = .1, b = .1)) 
library(nlstools)
a=nlsBoot (thisfit, niter = 999)
cr= nlsConfRegions(thisfit, exp = 2, length = 200) 
## 100%
##  Confidence regions array returned 
res= data.frame(X, pred = predict(thisfit)) 

下一行将失败,因为您尚未创建绘图:

## points(res[order(X),], type='l', col="grey40", lty=1) 
## Error in plot.xy(xy.coords(x, y), type = type, ...) : 
##    plot.new has not been called yet

plot(cr)确实有效; plot(res[order(X),])

也是如此

答案 2 :(得分:0)

我在这里发帖,因为我发现了同样的问题,答案似乎并不清楚。像nlsJack和nlsBoot这样的nlstools需要访问原始数据集,因为传递给函数的内容只是nls对象,它不包含原始数据的链接,只包含其标签。因此,当您将数据集作为全局对象使用时,nlsBoot(或nlsJack)将找到它并运行分析。

答案 3 :(得分:0)

使用包含NA的数据时遇到了同样的问题。一旦删除了NA,代码就可以正常工作。