Search code examples
roptimizationmodelnlscoefficients

Parameters bootstrap problem with optim() function for nls model


I've like to use optim() in my nls model, but doen't work. In my example:

Fisrt I create a data set

library(nls2)
#Data set
x <- c(1 ,10,  20,  30,  40,  50,  60,  70,  80,  90, 100) 
y <- c(0.033823,  0.014779,  0.004698,  0.001584, -0.002017, -0.003436, 
-0.000006, -0.004626, -0.004626, -0.004626, -0.004626) 
dat<-cbind(y,x)

Second, I make a simple nls model

#Create a nls model
fo3<- y ~ a4*exp(-x/a5)
fm3 <- nls2(fo3, alg = "brute-force",
     start = data.frame(a4 = c(-10, 10), a5 = c(-10, 10)),
     control = nls.control(maxiter = 1000))
summary(fm3)

Now a try to create a bootstrap for the y ~ a4*exp(-x/a5) model for study the model coefficients:

# bootstrap parametric
# nls model with par
#y = a4 * exp(-x/a5) 

fstar<- function(dat,a) {
              y= a[1]*exp(-x/a[2]) 
}

## Simulation 999 times
Nsim=999
RES1=NULL
for(i in 1:Nsim) 
{                                    
oo2=optim(c(0.97, 0.32),fstar, method="Nelder-Mead",control=list(maxit=10000))
RES1<-rbind(oo2$par)
write.table(RES1, file ="boot.out.mod", row.names=F, col.names=F,append=T)
}
#

And I have a bad output:

Error in fn(par, ...) : argument "a" is missing, with no default

Any member could help me please?

Thanks!


Solution

  • If a put sum() inside the function and change (dat,a) by (a,x,y), it's works!

    # bootstrap parametric
    # nls model with par
    #y = a4 * exp(-x/a5) 
    
    fstar<- function(a,x,y) {
                  sum (y= a[1]*exp(-x/a[2])) 
    }
    
    ## Simulation 999 times
    Nsim=999
    RES1=NULL
    for(i in 1:Nsim) 
    {                                    
    oo2=optim(c(0.97, 0.32),fstar, x=x, y=y, method="Nelder-Mead",control=list(maxit=10000))
    RES1<-rbind(oo2$par)
    write.table(RES1, file ="boot.out.mod", row.names=F, col.names=F,append=T)
    }
    #