0

I am using the nlstools package in R. I fit a model to my data and plot it well. I am not sure what i am doing wrong but i can't seem to call methods from nlstools as i get an error for "object 'd' not found". Here is the code, with the error lines commented:

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) 

Any ideas of what is going on? thanks

1

4 Answers 4

3

Ok, here is what i found: the problem is when the call to nlsBoot is inside a function. The code bellow should replicate the problem (nlsBoot not finding variable 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()

I also noticed that if i make variable d global:

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

Or define it outside the function before the call:

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

All works well. If someone knows why, please post it. Thanks!

Here is a fix (the commented out lines are from the original and are replaced by the line following them):

# 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

I can't reproduce the error (posted as answer rather than comment for decent code formatting).

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

The next line will fail because you haven't created a plot yet:

## 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) does appear to work; so does plot(res[order(X),])

1
  • (sorry, forgot to copy the main plot instruction). I can also run the examples found in the help pages, so I am left to think that my problem is in the data. Either variable D, X or Y. I wonder if I can't define X and Y outside the model (as you haven't). I'll try that tomorrow morning and get back here. Thanks
    – lourencoj
    Dec 11, 2012 at 22:07
1

I am posting this here because I found the same problem and the answer does not seem to be clear. nlstools like nlsJack and nlsBoot need to access the original data set because what is passed to the function is only the nls object, which does not contain a link to the original data, just its label. So, when you make your data set available as global object, the nlsBoot (or nlsJack) will find it and run the analysis.

0

I encountered this same problem when using data containing NA's. Once I removed the NA's, the code worked fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.