问题描述
我提出这个问题,因为我不明白为什么 nlxb
拟合函数不能与predict()函数一起使用。
I'm asking this question because I couldn't figure it out why nlxb
fitting function does not work with the predict() function.
我一直在寻找解决方案,但迄今为止没有运气:(
I have been looking around to solve this but so far no luck:(
我使用 dplyr
分组数据,并使用 do
来适应每个组,使用 nlxb
从 nlmrt
包。
I use dplyr
to group data and use do
to fit each group using nlxb
from nlmrt
package.
这是我的尝试
set.seed(12345)
set =rep(rep(c("1","2","3","4"),each=21),times=1)
time=rep(c(10,seq(100,900,100),seq(1000,10000,1000),20000),times=1)
value <- replicate(1,c(replicate(4,sort(10^runif(21,-6,-3),decreasing=FALSE))))
data_rep <- data.frame(time, value,set)
> head(data_rep)
# time value set
#1 10 1.007882e-06 1
#2 100 1.269423e-06 1
#3 200 2.864973e-06 1
#4 300 3.155843e-06 1
#5 400 3.442633e-06 1
#6 500 9.446831e-06 1
* * * *
library(dplyr)
library(nlmrt)
d_step <- 1
f <- 1e9
d <- 32
formula = value~Ps*(1-exp(-2*f*time*exp(-d)))*1/(sqrt(2*pi*sigma))*exp(-(d-d_ave)^2/(2*sigma))*d_step
dffit = data_rep %>% group_by(set) %>%
do(fit = nlxb(formula ,
data = .,
start=c(d_ave=44,sigma=12,Ps=0.5),
control=nls.lm.control(maxiter = 100),
trace=TRUE))
-------- ------------------------------------------------
有两点我想终于得到,
--------------------------------------------------------
There are two points I would like to get finally,
1)首先,如何获得每个组继续 dffit
管道。
1)First, how to get fitting coefficients of each group in continuation to dffit
pipeline.
2)根据新的x值进行预测。
2) Doing prediction of based on new x values.
例如范围< - data.frame(x = seq(1e-5,20000,length.out = 10000) code>
for instance range <- data.frame(x=seq(1e-5,20000,length.out=10000))
预测(fit,data.frame(x = range)
predict(fit,data.frame(x=range)
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "nlmrt"
由于 nlxb
与 nls
我喜欢使用 nlxb
的解决方案,但如果您有更好的解决方案,请告诉我们。
Since nlxb
is working smoothly compared to nls
r-minpack-lmnls-lm-failed-with-good-results I would prefer solutions with nlxb
. But if you have a better solution please let us know.
推荐答案
没有 coef
或预测
nlmrt
类对象,但是nlmrt包确实提供了 wrapnls
,它将运行 nlmrt
然后 nls
,以便nls
对象结果,然后th在对象可以使用所有的nls
类方法。
There are no coef
or predict
methods for "nlmrt"
class objects but the nlmrt package does provide wrapnls
which will run nlmrt
and then nls
so that an "nls"
object results and then that object can be used with all the "nls"
class methods.
另请注意 nls.lm.control
来自nlsLM包,不应该在这里使用 - 使用列表
Also note that nls.lm.control
is from the nlsLM package and should not be used here -- use list
instead.
这篇关于我们可以使用nlmrt包中的nlxb进行预测吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!