What I'm doing wrong here when trying to conve

2019-07-27 05:37发布

问题:

I am trying to convert an "nlmrt object to an "nls" object using nls2. However, I can only manage to do it if I write explicitly the names of the parameters in the call. Can't I define the parameter names programmatically? See the reproducible example:

library(nlmrt)

scale_vector <- function(vector, ranges_in, ranges_out){
    t <- (vector - ranges_in[1, ])/(ranges_in[2, ]-ranges_in[1, ])
    vector <- (1-t) * ranges_out[1, ] + t * ranges_out[2, ] 
}

shobbs.res  <-  function(x) {
    # UNSCALED Hobbs weeds problen -- coefficients are rescaled internally using
    # scale_vector

    ranges_in  <- rbind(c(0, 0, 0), c(100, 10, 0.1))
    ranges_out <- rbind(c(0, 0, 0), c(1, 1, 1)) 
    x <- scale_vector(x, ranges_in, ranges_out)

    tt <- 1:12
    res <- 100*x[1]/(1+10*x[2]*exp(-0.1*x[3]*tt)) - y }


y  <-  c(5.308, 7.24, 9.638, 12.866, 17.069, 23.192, 31.443, 
         38.558, 50.156, 62.948, 75.995, 91.972)
st  <-  c(b1=100, b2=10, b3=0.1)

ans1n <- nlfb(st, shobbs.res)
print(coef(ans1n))

This works:

library(nls2)
ans_nls2 <- nls2(y ~ shobbs.res(c(b1, b2, b3)) + y, start = coef(ans1n), alg = "brute")

However, this forces me to hard-code the parameters names in the call to nls2. For reasons related to my actual code, I would like to be able to do something like

ans_nls2 <- nls2(y ~ shobbs.res(names(st)) + y, start = coef(ans1n), alg = "brute")

But this returns an error:

    Error in vector - ranges_in[1, ] : 
  non-numeric argument to binary operator 

Is it possible to fix this, without having to hard-code explicitly the names of parameters in the call to nls2?

回答1:

nls2 will accept a string as a formula:

co <- coef(ans1n)
fo_str <- sprintf("y ~ shobbs.res(c(%s)) + y", toString(names(co)))

nls2(fo_str, start = co, alg = "brute")

giving:

Nonlinear regression model
  model: y ~ shobbs.res(c(b1, b2, b3)) + y
   data: NULL
      b1       b2       b3 
196.1863  49.0916   0.3136 
 residual sum-of-squares: 2.587

Number of iterations to convergence: 3 
Achieved convergence tolerance: NA