Search code examples
rlazy-evaluationnls

R - non linear regression (nls) and polynomial interactions (poly)


I can run a nls regression at R if I explicitly define the parameters ("a" and "b" in the example below). However, how could I code the nls with a generic number of variables/higher degress in the poly function?

df <- data.frame(var1 = rnorm(100), var2 = rnorm(100))

p <- as.data.frame(poly(df$var2, degree = 2))

names(p) <- paste0("poly", names(p))

df <- cbind(df, p)

nls(var1 ~ a*poly1 + b*poly2, data = df, start = list(a = 1, b = 2))

Trying code, as is done with the lm function, is not possible:

nls(var1 ~ poly(var2, degree = 2), data = df, start = list(a = 1, b = 2)) #=> Error

Solution

  • You need to explicitly multiply the polynomial terms and the coefficients you're estimating (a and b), as you did in the first example. You can do this with matrix multiplication.

    Note that poly returns a matrix, where the rows line up with your data and the columns are the polynomial terms:

    > dim(poly(df$var2, degree = 2))
    [1] 100   2
    

    Therefore, rather than working with a and b separately, combine them into a vector and multiply the 100 x 2 matrix with this 2 x 1 vector:

    nls(var1 ~ poly(var2, degree = 2) %*% coef, data = df,
        start = list(coef = c(a = 1, b = 2)))
    

    This gives the same answer as your working example.