When I fit a polynomial regression model in R, if I use the function poly()
and then try to get the variance inflation factors using vif()
I get the following error:
y = c(0.22200,0.39500,0.42200,0.43700,0.42800,0.46700,0.44400,0.37800,0.49400,
0.45600,0.45200,0.11200,0.43200,0.10100,0.23200,0.30600,0.09230,0.11600,
0.07640,0.43900,0.09440,0.11700,0.07260,0.04120,0.25100,0.00002)
x1 = c(7.3,8.7,8.8,8.1,9.0,8.7,9.3,7.6,10.0,8.4,9.3,7.7,9.8,7.3,8.5,9.5,7.4,7.8,
7.7,10.3,7.8,7.1,7.7,7.4,7.3,7.6)
x2 = c(0.0,0.0,0.7,4.0,0.5,1.5,2.1,5.1,0.0,3.7,3.6,2.8,4.2,2.5,2.0,2.5,2.8,2.8,
3.0,1.7,3.3,3.9,4.3,6.0,2.0,7.8)
x3 = c(0.0,0.3,1.0,0.2,1.0,2.8,1.0,3.4,0.3,4.1,2.0,7.1,2.0,6.8,6.6,5.0,7.8,7.7,
8.0,4.2,8.5,6.6,9.5,10.9,5.2,20.7)
m = lm(y~poly(x1, x2, x3, degree=2, raw=TRUE))
summary(m)
Now call vif()
> vif(m)
Error in vif.default(m) : model contains fewer than 2 terms
The model has 9
terms and an intercept.
> m$rank
[1] 10
It seems to me that the vif()
function does not function with poly()
. Is this correct? Is there a way around this or do I need to use first principles?
I can calculate the variance inflation factors like so:
X = poly(x1, x2, x3, degree=2, raw=TRUE)
C = solve(t(X)%*%X)
vifs = 1/diag(C)