sampling and nls formula

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

sampling and nls formula

Susan  Imholt

Hello,

I am trying to bootstrap a function that extracts the log-likelihood value and the nls coefficients from an nls object.  I want to sample my dataset (pdd) with replacement and for each sampled dataset, I want to run nls and output the nls coefficients and the log-likelihood value.

Code:
x<-c(1,2,3,4,5,6,7,8,9,10)
y<-c(10,11,12,15,19,23,26,28,28,30)
pdd<-data.frame(x,y)

i<-sample(10, replace=TRUE)

pdd.lik.coef<-function(data,i){
     d<-data[i,]
     pdd.nls<-nls(d$y~(a*(d$x)^2)/(b+(d$x)^2), data=pdd, start = list(a = 30, b = 36), trace=FALSE)
     pdd.logLik<-logLik(pdd.nls)
     coeff <- coef(pdd.nls)
     lik.coef <- c(pdd.logLik, coeff)
}

pdd.boot<-boot(data=pdd, statistic=pdd.lik.coef, R=1000)
pdd.boot$t

My problem lies in the pdd.lik.coef function. It seems that nls recognizes all letters in the formula d$y~(a*(d$x)^2)/(b+(d$x)^2) as parameters, so "d$y" and "d$x" in the formula aren't being recognized properly to be sampled, rather the "d" is interpreted as a parameter.  When I try to bootstrap this function, I get an error message: "Error in eval(expr, envir, enclos) : object "d" not found" (because I didn't specify an estimate for d as a parameter, since I didn't intend for it to be a parameter).  

Any suggestions?

Thanks,
Susie Imholt
Master's Candidate
Huxley College of the Environment
Western Washington University
Bellingham, WA  USA




_______________________________________________
Join Excite! - http://www.excite.com
The most personalized portal on the Web!

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: sampling and nls formula

Marco Geraci
Hi

--- Susan  Imholt <[hidden email]> wrote:

>
> Hello,
>
> I am trying to bootstrap a function that extracts
> the log-likelihood value and the nls coefficients
> from an nls object.  I want to sample my dataset
> (pdd) with replacement and for each sampled dataset,
> I want to run nls and output the nls coefficients
> and the log-likelihood value.
>
> Code:
> x<-c(1,2,3,4,5,6,7,8,9,10)
> y<-c(10,11,12,15,19,23,26,28,28,30)
> pdd<-data.frame(x,y)
>
> i<-sample(10, replace=TRUE)

i don't think you need a starting sample of the
'index'. You can omit
> i<-sample(10, replace=TRUE)

 
> pdd.lik.coef<-function(data,i){
>      d<-data[i,]
>      pdd.nls<-nls(d$y~(a*(d$x)^2)/(b+(d$x)^2),
> data=pdd, start = list(a = 30, b = 36), trace=FALSE)

there's a programming error here
The argument 'data' of 'nls' must match the one that
you pass through 'pdd.lik.coef' or the one that you
redefine ('d'). Also, when a function like 'nls' has
the argument 'data' you don't need to use '$' in the
formula (providing that 'data' contains the variables
named in the formula).

>     pdd.logLik<-logLik(pdd.nls)
>     coeff <- coef(pdd.nls)
>     lik.coef <- c(pdd.logLik, coeff)
>}

to gain some speed you might want to avoid assigning
the result to 'lik.coef', 'coeff', and 'lik.coef'.

Summarizing, the following code should work. 'should'
means that might not work. I tried with few boot
samples, and it did work. When I tried with R=1000,
the program stopped because 'nls' didn't like a
particular sample of the data and it reached maxit=50
(default). I added a 'control=list(maxiter=100)' to
'nls' to your code. I suggest you to
work on that.

code:

x<-c(1,2,3,4,5,6,7,8,9,10)
y<-c(10,11,12,15,19,23,26,28,28,30)
pdd<-data.frame(x,y)

pdd.lik.coef<-function(data,i){
     d<-data[i,]
     pdd.nls<-nls(y~(a*x^2)/(b+x^2), data=d, start =
list(a = 30, b = 36), trace=FALSE,
control=list(maxiter=100))
     c(logLik(pdd.nls), coef(pdd.nls))
}

pdd.boot<-boot(data=pdd, statistic=pdd.lik.coef,
R=1000)
pdd.boot$t

hope this helps

Marco

> _______________________________________________
> Join Excite! - http://www.excite.com
> The most personalized portal on the Web!
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html