NLS Function By Group

☆樱花仙子☆ 提交于 2019-12-12 23:30:18

问题


I have a dataset where I want to apply non linear least squares by group. This is a continuation to my previous question: NLS Function - Number of Iterations Exceeds max

The dataset looks like this:

df
x        y    GRP
0        0      1
426   9.28      1
853   18.5      1
1279  27.8      1
1705  37.0      1
2131  46.2      1
0        0      2
450   7.28      2
800   16.5      2
1300  30.0      2
2000  40.0      2
2200  48.0      2  

If I were to do this with one group it would be like this:

df1<-filter(df, GRP==1)

a.start <- max(df1$y)
b.start <- 1e-06
control1 <- nls.control(maxiter= 10000,tol=1e-02, warnOnly=TRUE)
nl.reg <- nls(y ~ a * (1-exp(-b * x)),data=df1,start= 
list(a=a.start,b=b.start),
           control= control1)
coef(nl.reg)[1]
coef(nl.reg)[2]

> coef(nl.reg)[1]
       a 
5599.075 
> coef(nl.reg)[2]
       b 
3.891744e-06 

I would then do the same thing for GRP2. I want my final output to look like this:

x        y    GRP                       a                       b
0        0      1                5599.075            3.891744e-06
426   9.28      1                5599.075            3.891744e-06
853   18.5      1                5599.075            3.891744e-06
1279  27.8      1                5599.075            3.891744e-06
1705  37.0      1                5599.075            3.891744e-06
2131  46.2      1                5599.075            3.891744e-06
0        0      2    New Value for a GRP2    New Value for b GRP2     
450   7.28      2    New Value for a GRP2    New Value for b GRP2
800   16.5      2    New Value for a GRP2    New Value for b GRP2
1300  30.0      2    New Value for a GRP2    New Value for b GRP2
2000  40.0      2    New Value for a GRP2    New Value for b GRP2
2200  48.0      2    New Value for a GRP2    New Value for b GRP2

Ideally I think dplyr would be the best way but I can't figure out how to do it. This is what I think it will probably look like:

control1 <- nls.control(maxiter= 10000,tol=1e-02, warnOnly=TRUE)
b.start <- 1e-06

df %>%
  group_by(GRP) %>%
  do(nlsfit = nls( form = y ~ a * (1-exp(-b * x)), data=., 
start= list( a=max(.$y), b=b.start),
      control= control1) ) %>%
  list(a = coef(nlsfit)[1], b = coef(nlsfit)[2])

Error:

 in nlsModel(formula, mf, start, wts) : 
  singular gradient matrix at initial parameter estimates

Not really sure how to do this though and any help would be great. Thanks!


回答1:


I initially got the same error message (re: not finding object 'y' in nls) as I did with a tidyverse stab when initially attempting to use the lapply-split-function paradigm and went searching for: "[r] using nls inside function". I've changed my original use of attach to list2env:

sapply(  split( df , df$GRP), function(d){ dat <- list2env(d)
    nlsfit <- nls( form = y ~ a * (1-exp(-b * x)), data=dat, start= list( a=max(y), b=b.start),
          control= control1) 

list(a = coef(nlsfit)[1], b = coef(nlsfit)[2])} )
#---

  1            2            
a 14.51827     441.5489     
b 2.139378e-06 -6.775562e-06

You also get warnings that you were expecting. These could be suppressed with suppressWarnings( ... )

One of the suggestions was to use attach. Which I then did with extreme reluctance, since I have often warned newbies not to ever use attach. But here it seemed to force a local environment to be constructed. I'm more comforatable with list2env as a mechaism to satisfy nls. The top of the code for nls was what led me to that choice:

if (!is.list(data) && !is.environment(data)) 
    stop("'data' must be a list or an environment")


来源:https://stackoverflow.com/questions/52710549/nls-function-by-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!