Help with package nlsr (giving a function as a formula)

I have been using the package nls" for a while, but it doesn't work in some situations. So I decided to try nlsr". But I am stuck. My problem is how do I give a function as a formula like in ``nls"

rm(list=ls()) # clear before starting
weed  = c(5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443,
          38.558, 50.156, 62.948, 75.995, 91.972) 
tt = 1:12
weeddf = data.frame(tt, weed)
st  = c(b1=1, b2=1, b3=1) # a default starting vector (named!)
wmodu =  weed ~ b1/(1 + b2 * exp(- b3 * tt))
# I want to give the above as a function.
wmod = function(tt,b1,b2,b3){
  return(b1/(1 + b2 * exp(- b3 * tt)))
}
anlxb1un = nlxb(wmodu, start=st, trace=FALSE, data=weeddf)
print(anlxb1un)
# the above two lines work
anlxb1un1 = nlxb(weed~wmod(tt,b1,b2,b3), start=st, trace=FALSE, data=weeddf)
# the above produces the following error message

Error in deriv.default(residexpr, names(pvec)) : 
  Function 'wmod' is not in the derivatives table

anlxb1un2 = nls(weed~wmod(tt,b1,b2,b3), start=st, trace=FALSE, data=weeddf)
# the above works 

This traces to the required first argument to nlxb needing to be an object of class formula, including both the LHS and the RHS. wmodu is a formula but wmod as defined in the reprex isn't.

library(nlsr)
weed <- c(
  5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443,
  38.558, 50.156, 62.948, 75.995, 91.972
)
tt <- 1:12
weeddf <- data.frame(tt, weed)
st <- c(b1 = 1, b2 = 1, b3 = 1) # a default starting vector (named!)
wmodu <- weed ~ b1 / (1 + b2 * exp(-b3 * tt))

wmod <- function(tt, b1, b2, b3) {
  return(formula(weed ~ b1 / (1 + b2 * exp(-b3 * tt))))
}
wmod()
#> weed ~ b1/(1 + b2 * exp(-b3 * tt))
#> <environment: 0x13cf93728>

anlxb1un1 <- nlxb(wmod(), start = st, trace = FALSE, data = weeddf)
print(anlxb1un1)
#> residual sumsquares =  2.5873  on  12 observations
#>     after  19    Jacobian and  25 function evaluations
#>   name            coeff          SE       tstat      pval      gradient    JSingval   
#> b1               196.186         11.31      17.35  3.167e-08  -4.859e-09        1011  
#> b2               49.0916         1.688      29.08  3.284e-10  -3.099e-08      0.4605  
#> b3               0.31357      0.006863      45.69  5.768e-12   2.305e-06     0.04714

Created on 2023-08-08 with reprex v2.0.2

BTW: this is considered poor practice in general and somewhat rude in a reprex. Restarting R is preferred and those answering questions may have other work in progress that they don't want to lose. Not a biggie, but a convention.

Thank you for the reply and sorry about my ignorance in using rm(list=ls()).

Since I couldn't figure out how to give a function as an input like in nls", I was playing with the example in nlrs" package.

If my function is longer than one line (where intermediate variables are calculated), how do I approach in converting that to a formula?

Do you need to parameterize the arguments? If not, what’s the motivation for using a function to generate the required formula as I have done?

return(formula(weed ~ b1 / (1 + b2 * exp(-b3 * tt))))

Here is a really basic question. Look at the following bit

weed <- c(
  5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443,
  38.558, 50.156, 62.948, 75.995, 91.972
)
 tt <- 1:12
 weeddf <- data.frame(tt, weed)
 wmodu <- weed ~ b1 / (1 + b2 * exp(-b3 * tt))

How do I get R to evaluate RHS this expression? For example say b1=200, b2=50, b3=0.5. How doI make the above formula calculates values for tt=1:20?

Thanks

PS: nm that was a silly question.

library(nlsr)
weed <- c(
  5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443,
  38.558, 50.156, 62.948, 75.995, 91.972
)
tt <- 1:12
weeddf <- data.frame(tt, weed)
st <- c(b1 = 1, b2 = 1, b3 = 1) # a default starting vector (named!)
wmodu <- weed ~ b1 / (1 + b2 * exp(-b3 * tt))

wmod <- function(tt, b1, b2, b3) {
  return(formula(weed ~ b1 / (1 + b2 * exp(-b3 * tt))))
}

anlxb1un1 <- nlxb(wmod(1:20,200,50,.5), start = st, trace = FALSE, data = weeddf)
print(anlxb1un1)
#> residual sumsquares =  2.5873  on  12 observations
#>     after  19    Jacobian and  25 function evaluations
#>   name            coeff          SE       tstat      pval      gradient    JSingval   
#> b1               196.186         11.31      17.35  3.167e-08  -4.859e-09        1011  
#> b2               49.0916         1.688      29.08  3.284e-10  -3.099e-08      0.4605  
#> b3               0.31357      0.006863      45.69  5.768e-12   2.305e-06     0.04714

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.