1

I was playing around with the nlsLM function, from the minpack.lm library, and encountered some behaviour that I don't understand.

Given that the following function produces output when I supply a numeric vector 'b' as input I wanted to use this function to fit a nonlinear model to my data.

volEquation <- function(DBH, PHt, b){
       b[1] * DBH^b[2] * PHt^b[3]
}

However I have become stuck when it comes to correctly specifying the initial parameter values. R code follows:

library(minpack.lm)

n <- 20
x <- seq(12, 60, length.out = n)
y <- seq(22, 45, length.out = n)
z <- x^2 * y ^ 3 + rnorm(n, 0, 0.1)

Data <- data.frame(DBH = x, PHt = y, TVT = z)

nlsFormula   <- "TVT ~ volEquation(DBH, PHt, b)"
nlsInitial   <- list(b = c(0.5, 2.25, 3.25))
nlsLMOutput  <- nlsLM(formula = nlsFormula, data = Data, start = nlsInitial)
nlsOutput    <- nls(formula = nlsFormula, data = Data, start = nlsInitial

nls was successful at fitting the data while nlsLM gave me this error message,

Error in rownames<-(*tmp*, value = "b") : length of 'dimnames' [1] not equal to array extent

Can anyone provide insight as to why this problem occurs in the nlsLM function? I've tried sifting through the nlsLM code but I still don't understand what's going on.

frank2165
  • 114
  • 2
  • 7
  • It would be nice if you made a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data so we can test possible solutions. – MrFlick Apr 22 '15 at 02:02
  • The way the code is set up is does not appear to like parameters that are vectors themselves the way `nls()` does. (Being from a user-submitted package it doesn't have to follow the same rules as the base function even it is named similarly). The test data works with my proposed solution if I increase the number of max iterations. – MrFlick Apr 22 '15 at 03:55
  • @MrFlick I was afraid of that, I finally figured out where in the nlsLM code the problem is (more specifically it's in the nls.lm function) but It's probably beyond my ability to fix. Separated parameters will have to do. – frank2165 Apr 22 '15 at 10:17

1 Answers1

2

Try separating your parameters

volEquation <- function(DBH, PHt, x,y,z){
       x * DBH^y * PHt^z
}

nlsFormula <- "TVT ~ volEquation(DBH, PHt, x, y, z)"
nlsInitial <- c(x=5e-3, y=2, z=1)
nlsOutput  <- nlsLM(formula = nlsFormula, data = Data, start = nlsInitial, control=nls.lm.control(maxiter=100))
MrFlick
  • 195,160
  • 17
  • 277
  • 295