[R-sig-ME] Data Handling within nlme library

Phillip Alday me @end|ng |rom ph||||p@|d@y@com
Tue Aug 3 00:56:41 CEST 2021


This is a bug in the way gnls is using environments to call nls() to get
starting values for glns(). You can circumvent this by doing the call to
nls() for starting values directly:

make_model <- function() {
 
  my_soybean <- Soybean
 
  form <- weight ~ SSlogis(Time, Asym, xmid, scal)
 
  gnls(form, data=my_soybean, weights=varPower(),
       start=coef(nls(form, my_soybean)))
 
}

Phillip

On 01/08/2021 20:43, bill using denney.ws wrote:
> Hello,
>
>  
>
> I'm having an issue with data handling within the nlme library.
> Specifically, when I fit a gnls model within one function, I cannot update
> that model within another function with new data.  Are there any ways to
> give data within a function and have the model work outside?  An example of
> what I'm trying to do is below in the first example:
>
>  
>
> ``` r
>
> library(nlme)
>
>  
>
> make_model <- function() {
>
>   my_soybean <- Soybean
>
>   gnls(weight ~ SSlogis(Time, Asym, xmid, scal), data=my_soybean, weights =
> varPower())
>
> }
>
>  
>
> my_model <- make_model()
>
> #> Error in stats::nls(formula = weight ~ SSlogis(Time, Asym, xmid, scal), :
> object 'my_soybean' not found
>
> # Issue: my_soybean is in the function environment but not found during
> estimation
>
>  
>
> make_model <- function() {
>
>   time <- Soybean$Time
>
>   weight <- Soybean$weight
>
>   gnls(
>
>     weight ~ Asym/(1+exp((xmid-time)/scal)),
>
>     start=c(Asym=30, xmid=40, scal=10),
>
>     weights = varPower()
>
>   )
>
> }
>
>  
>
> my_model <- make_model()
>
> #> Error in terms.formula(formula, data = data): '.' in formula and no
> 'data' argument
>
> # Issue: No idea where the error is coming from (making a guess that it is
> the weights argument)
>
>  
>
> make_model <- function() {
>
>   time <- Soybean$Time
>
>   weight <- Soybean$weight
>
>   gnls(
>
>     weight ~ Asym/(1+exp((xmid-time)/scal)),
>
>     start=c(Asym=30, xmid=40, scal=10),
>
>   )
>
> }
>
>  
>
> my_model <- make_model()
>
> # Issue: when data are given in the local environment but not with a `data`
>
> # argument, it works.
>
> ```
>
>  
>
> <sup>Created on 2021-08-01 by the [reprex
> package](https://reprex.tidyverse.org) (v2.0.0)</sup>
>
>  
>
> Thanks,
>
>  
>
> Bill
>
>  
>
> P.S.  I'm pretty sure that this is also related to an issue found in the
> nlraa library described at https://github.com/femiguez/nlraa/issues/4
>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-mixed-models using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models



More information about the R-sig-mixed-models mailing list