Can we make prediction with nlxb from nlmrt packag

2020-05-08 15:04发布

问题:

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:(

I use dplyr to group data and use do to fit each group using nlxb from nlmrt package.

Here is my attempt

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)First, how to get fitting coefficients of each group in continuation to dffitpipeline.

2) Doing prediction of based on new x values.

for instance range <- data.frame(x=seq(1e-5,20000,length.out=10000))

predict(fit,data.frame(x=range)

Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "nlmrt"

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.

回答1:

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.

Also note that nls.lm.control is from the nlsLM package and should not be used here -- use list instead.