Error: $ operator is invalid for atomic vectors when using vif(x)

I am trying to find the VIFs for the Faraway package's divusa data using the following model. Any assistance in this matter is greatly appreciated.

Here is my code:

lmod <- lm(divorce ~ unemployed + femlab + marriage + birth + military, divusa)
summary(lmod)

x <- model.matrix(lmod)
head(x)

x <- model.matrix(lmod)[,-1]
head(x)

e <- eigen(t(x) %*% x)
e$val

sqrt(e$val[1]/e$val)

vif(x)

Here is the error I get every time I run my code.

Error: $ operator is invalid for atomic vectors

Your code seems to produce exactly the desired result. I cannot reproduce an error with your code.

vif() will also take model objects; vif(lmod) works identically to your last line.

so does: vif(model.matrix(lmod)[,-1])

Output shown below:

> lmod <- lm(divorce ~ unemployed + femlab + marriage + birth + military, divusa)
> summary(lmod)

Call:
lm(formula = divorce ~ unemployed + femlab + marriage + birth + 
    military, data = divusa)

Residuals:
    Min      1Q  Median      3Q     Max 
-3.8611 -0.8916 -0.0496  0.8650  3.8300 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  2.48784    3.39378   0.733   0.4659    
unemployed  -0.11125    0.05592  -1.989   0.0505 .  
femlab       0.38365    0.03059  12.543  < 2e-16 ***
marriage     0.11867    0.02441   4.861 6.77e-06 ***
birth       -0.12996    0.01560  -8.333 4.03e-12 ***
military    -0.02673    0.01425  -1.876   0.0647 .  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.65 on 71 degrees of freedom
Multiple R-squared:  0.9208,	Adjusted R-squared:  0.9152 
F-statistic: 165.1 on 5 and 71 DF,  p-value: < 2.2e-16

> 
> x <- model.matrix(lmod)
> head(x)
  (Intercept) unemployed femlab marriage birth military
1           1        5.2  22.70     92.0 117.9   3.2247
2           1       11.7  22.79     83.0 119.8   3.5614
3           1        6.7  22.88     79.7 111.2   2.4553
4           1        2.4  22.97     85.2 110.5   2.2065
5           1        5.0  23.06     80.3 110.9   2.2889
6           1        3.2  23.15     79.2 106.6   2.1735
> 
> x <- model.matrix(lmod)[,-1]
> head(x)
  unemployed femlab marriage birth military
1        5.2  22.70     92.0 117.9   3.2247
2       11.7  22.79     83.0 119.8   3.5614
3        6.7  22.88     79.7 111.2   2.4553
4        2.4  22.97     85.2 110.5   2.2065
5        5.0  23.06     80.3 110.9   2.2889
6        3.2  23.15     79.2 106.6   2.1735
> 
> e <- eigen(t(x) %*% x)
> e$val
[1] 1174600.548   21261.741   16133.842    6206.181    1856.894
> 
> sqrt(e$val[1]/e$val)
[1]  1.000000  7.432684  8.532498 13.757290 25.150782
> 
> vif(x)
unemployed     femlab   marriage      birth   military 
  2.252888   3.613276   2.864864   2.585485   1.249596 
> vif(lmod)
unemployed     femlab   marriage      birth   military 
  2.252888   3.613276   2.864864   2.585485   1.249596

Thank you so much for your assistance!

You're quite welcome.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.