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

旧时模样 提交于 2020-04-16 04:21:29

问题


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

回答1:


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.




回答2:


You could do something like this:

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

getPoly <- function(df, degree=2) {
    p <- poly(df$var2, degree = degree)
    colnames(p) <- paste0("poly", colnames(p))
    new_df <- cbind(df, p)
    formula_str <- paste0("var1~",paste0(paste0(letters[1:degree], "*poly", 1:degree), collapse="+"))
    return(list(df=new_df, formula_str=formula_str))
}

poly_data <- getPoly(df, 3)
start_list <- list(a=1,b=2, c=3)

nls(as.formula(poly_data$formula_str), data = poly_data$df, start = start_list) 


来源:https://stackoverflow.com/questions/43510226/r-non-linear-regression-nls-and-polynomial-interactions-poly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!