NLS按组的功能 [英] NLS Function By Group
问题描述
我有一个数据集,我想按组应用非线性最小二乘法。这是我上一个问题的延续:
NLS函数-数量迭代次数超过最大值
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
数据集如下:
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
然后,我会对GRP2做同样的事情。我希望最终输出看起来像这样:
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
理想情况下,我认为dplyr是最好的方法,但我不知道该怎么做。这是我认为的样子:
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])
错误:
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!
推荐答案
我最初得到的是相同的错误消息(re:在中找不到对象'y' nls
),就像我最初尝试使用 lapply-split-function $ c时使用
tidyverse
刺刀所做的那样$ c>范例,然后搜索: [r]在函数内使用nls。我已将 attach c的原始用法更改为
list2env
:
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
您还会收到警告,提示您期望。这些可以通过 suppressWarnings(...)
You also get warnings that you were expecting. These could be suppressed with suppressWarnings( ... )
来抑制。建议之一是使用附加
。然后我极度不情愿地这样做了,因为我经常警告新手不要使用 attach
。但是,这似乎在迫使当地环境的建设。我更喜欢list2env作为满足nls的机制。 nls
的代码顶部是导致我选择的原因:
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")
这篇关于NLS按组的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!