1

I got a question: Someone have run the corvif function with the code HighstatLibV10.R available in the page http://www.highstat.com/index.php/mixed-effects-models-and-extensions-in-ecology-with-r? I can't get the VIF values because the output gives me this error:

Error in myvif(lm_mod) : object 'tmp_cor' not found! 

I have 6 physical variables and I'm looking for collinearity among variables. Any help more than welcome!

theforestecologist
  • 4,667
  • 5
  • 54
  • 91
  • 1
    Hi Mauro, welcome to StackOverflow. Your question is not clear. You should provide the code you're having trouble with in your question instead of or in addition to the link you provide. Also, error messages and code are much easier to ready and understand when formatted as a code block. – Joey Harwood Nov 16 '17 at 17:04
  • Thanks Joey. Sorry if I haven't explained very well. I have six environmental variables across 30 sites. I would like to test the collinearity among variables. To do I should run the corvif function and the pairplot with the Pearson correlation coefficients to get the VIF-values. Corvif function is running (here my error) with the file that you can get from this link: http://www.highstat.com/Books/Book2/HighstatLibV10.R When I run corvif(mydata) I got the error: Error in myvif(lm_mod) : object 'tmp_cor' not found. Any help is more than welcome. – Mauro Oróstica Nov 16 '17 at 18:30
  • Please update your question instead of adding information in comments. That makes it easier to read. – Stephan Vierkant Nov 17 '17 at 09:16
  • I ran into the same problem with [v1](https://besjournals.onlinelibrary.wiley.com/action/downloadSupplement?doi=10.1111%2Fj.2041-210X.2009.00001.x&attachmentId=65870515) and [v10](http://www.highstat.com/Books/Book2/HighstatLibV10.R). I checked *all* of their data sets ([here](http://www.highstat.com/Books/Book2/ZuurDataMixedModelling.zip)) and found no evidence of a variable called `tmp_cor`. (I had assumed it was a mistaken leftover from a specific data set...). So, unfortunately, I couldn't figure out a solution to this either... – theforestecologist May 22 '18 at 13:22
  • Ooops.. I mistyped my _find_ search while looking for `tmp_cor` in the code :p. so `tmp_cor` is an object created in `corvif` that is created using the `cor` function (in the base `stats` package that comes with R install). Specifically, `tmp_cor <- cor(dataz,use="complete.obs")`. However, it's been removed from the code when I look at the code for `corvif` – theforestecologist May 22 '18 at 13:39

2 Answers2

1

If working with the corvif() is not of utmost importance you can use the vif() in the R package 'car' to get VIF values for your linear models.

Leo Ohyama
  • 887
  • 1
  • 9
  • 26
0

So tmp_cor is an object that is supposed to be created in corvif

  • tmp_cor is created using the cor function (in the base stats package that comes with R install) via: tmp_cor <- cor(dataz,use="complete.obs").

However, I noticed that with both v1 and v10 of Zurr et al's HighstatLib.R code this error occurs:

Error in myvif(lm_mod) : object 'tmp_cor' not found! 

First I checked V10:

It seems that the "final" version of corvif created when sourcing HighstatLibV10.R actually neglects to create tmp_cor at all!

> print(corvif)
function(dataz) {
  dataz <- as.data.frame(dataz)

  #vif part
  form    <- formula(paste("fooy ~ ",paste(strsplit(names(dataz)," "),collapse=" + ")))
  dataz   <- data.frame(fooy=1 + rnorm(nrow(dataz)) ,dataz)
  lm_mod  <- lm(form,dataz)

  cat("\n\nVariance inflation factors\n\n")
  print(myvif(lm_mod))
}

But, I noticed that the error in the OP's post also occurred when using V1 (i.e., HighstatLib.R associated with Zuur et al 2010). Although the code file creates 2 versions of corvif, they (and especially the latter of the two which would supercede the first) include a line to create tmp_cor:

corvif <- function(dataz) {
    dataz <- as.data.frame(dataz)
    #correlation part
    cat("Correlations of the variables\n\n")
    tmp_cor <- cor(dataz,use="complete.obs")
    print(tmp_cor)

    #vif part
    form    <- formula(paste("fooy ~ ",paste(strsplit(names(dataz)," "),collapse=" + ")))
    dataz   <- data.frame(fooy=1,dataz)
    lm_mod  <- lm(form,dataz)

    cat("\n\nVariance inflation factors\n\n")
    print(myvif(lm_mod))
}

So even though the code for corvif creates tmp_cor in the V1 code file, it appears that the helper function myvif (which actually uses the tmp_cor object) is not accessing it.

This suggests that we have a scoping problem...

Sure enough, if I just quickly change the tmp_cor line to create a global object, the code works fine:

 tmp_cor <<- cor(dataz,use="complete.obs")

Specifically:

corvif <- function(dataz) {
    dataz <- as.data.frame(dataz)
    #correlation part
    cat("Correlations of the variables\n\n")
    tmp_cor <<- cor(dataz,use="complete.obs")
    print(tmp_cor)

    #vif part
    form    <- formula(paste("fooy ~ ",paste(strsplit(names(dataz)," "),collapse=" + ")))
    dataz   <- data.frame(fooy=1,dataz)
    lm_mod  <- lm(form,dataz)

    cat("\n\nVariance inflation factors\n\n")
    print(myvif(lm_mod))
}

A more complete "fix" could be done by manipulating environments.

theforestecologist
  • 4,667
  • 5
  • 54
  • 91