Polynomial regression in R with multiple independent variables

后端 未结 1 1608
不思量自难忘°
不思量自难忘° 2021-02-05 17:03

I want to do a polynomial regression in R with one dependent variable y and two independent variables x1 and x2. In my mind the model shou

1条回答
  •  盖世英雄少女心
    2021-02-05 17:32

    you can use polym

    y ~ polym(x1, x2, degree=2, raw=TRUE) # is equivalent to
    y ~ x1 + x2 + I(x1^2) + I(x2^2) + x1:x2
    

    But be careful with the order of the coefficients they are not the same as the second formula.

    If you use degree=3 then it will add interactions of higher order like this I(x1^2):x2 +I(x2^2):x1, thus you have to adapt your formula.

    NB : polym is a wrapper for poly, so you can use this latter with the same call.

    0 讨论(0)
提交回复
热议问题