I want to corelate 19 variables from Worldclim V2.0 using the vif test. There's no ready code like for V1 so I wrote a code below but I'm facing one error. I'm new to R.

library (raster)

bioFinal<- c("wc2.0_bio_30s_01.tif", "wc2.0_bio_30s_02.tif", 
             "wc2.0_bio_30s_03.tif", "wc2.0_bio_30s_04.tif", 
             "wc2.0_bio_30s_07.tif", "wc2.0_bio_30s_11.tif", 
             "wc2.0_bio_30s_12.tif", "wc2.0_bio_30s_16.tif", 
             "wc2.0_bio_30s_17.tif", "wc2.0_bio_30s_08.tif", 
             "wc2.0_bio_30s_09.tif", "wc2.0_bio_30s_10.tif", 
             "wc2.0_bio_30s_13.tif", "wc2.0_bio_30s_14.tif", 
             "wc2.0_bio_30s_15.tif", "wc2.0_bio_30s_18.tif", 

#check collinearity structure - try two methods vifstep and vifcor 
# 1 no cor, 2-3 low, 4-5 moderate, >5 high 
v1 = vifstep(bioFinal, th=10 ) 
v2 = vifcor(bioFinal, th=0.9)

when I run it I have this error for both vifstep and vifcor :

  Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘vifstep’ for signature ‘"character"’

How can I fix the error, and can use a zip file in the code instead of listing all these layers?

  • Well, first your data are only paths and names of rasters. Second, it is doubtful that these vif functions will accept raster data. It would be helpful if you showed us where the vifcor and vifstep functions came from. Sep 30 '20 at 2:03
  • I used the usdm library for the the vif functions if that was your question. Regarding if the functions might not read raster data, I tried to find what format the function accepts so I can convert my data to but found nothing useful. If you know certain format that would certainly works please let me know. And yeah I actually didn't know that these were only names.
    – Areej
    Oct 6 '20 at 13:23
  • I would encourage you to read a bit on VIF methods and reconsider your approach. Calculating VIF on entire raster populations is not a well-supported idea. At the very least, it should be based on a sample and not all of the raster values. Oct 6 '20 at 20:37

Your object bioFinal is only the names of the files, there's no step where they are read in in the code you post; as a list they are a character vector, thus the error. First you have to read them in with stack: Instead of copying file names one by one, you may list them with list.files.

file_list <- list.files("C:\\Users\\Areej\\Documents\\", pattern = ".tif", full.names = T)
# just be sure there are no more .tif files in that directory
bioFinal <- stack(file_list)
v1 = vifstep(bioFinal, th=10 ) 
v2 = vifcor(bioFinal, th=0.9) 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.