Do there exist methods to identify quadratic components in a linear model with R?
Suppose we have an additive model of the form y=x1+x2+... with a lot of variables. Is there a routine in R to identify variables that should be considered as exhibiting a quadratic effect? I know that Box-Cox transformation allows to iden开发者_Python百科tify links for y, but what about x. If there are just a few variables, it's easy to test them, but what about holding a whole bunch?
Regards from Germany
You probably don't care to know whether you need quadratic terms, but rather whether any of the effects are non-linear. While a quadratic term can pick up some of those, there are some decidedly non-quadratic effects that are not linear. There are many ways of doing that, but I like using restricted cubic splines as implemented in the Hmisc
and Design
packages.
For example:
library(Design)
x1 <- runif(200)
x2 <- runif(200)
x3 <- runif(200)
x4 <- runif(200)
y <- x1 + x2 + rnorm(200)
f1 <- ols(y ~ rcs(x1,4) + rcs(x2,4) + rcs(x3,4) + rcs(x4,4))
> anova(f1)
Analysis of Variance Response: y
Factor d.f. Partial SS MS F P
x1 3 19.2033740 6.40112466 7.96 0.0001
Nonlinear 2 5.6426655 2.82133277 3.51 0.0319
x2 3 10.6042751 3.53475836 4.40 0.0051
Nonlinear 2 0.5047319 0.25236593 0.31 0.7309
x3 3 3.0844406 1.02814688 1.28 0.2829
Nonlinear 2 0.1474818 0.07374091 0.09 0.9124
x4 3 4.1770965 1.39236549 1.73 0.1619
Nonlinear 2 4.1770665 2.08853325 2.60 0.0771
TOTAL NONLINEAR 8 9.5322762 1.19153452 1.48 0.1660
REGRESSION 12 37.1220435 3.09350362 3.85 <.0001
ERROR 187 150.3064834 0.80377799
ols
is essentially the equivalent of lm
. Note the ANOVA table in the output: it has test for non-linearity of the effects including a global test.
If you want to create all the two-way interactions you can do this:
lm(y ~ (x1 + x2 + x3)^2, data=dat)
See:
?formula
精彩评论