TransWikia.com

Correlating Worldclim data with vif function in R

Geographic Information Systems Asked by Areej on November 3, 2020

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)
library(usdm)

setwd("C:/Users/Areej/Documents")
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_05.tif","wc2.0_bio_30s_06.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", 
             "wc2.0_bio_30s_19.tif")
bioFinal

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

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?

One Answer

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.

library(raster)
library(usdm)
file_list <- list.files("C:UsersAreejDocuments", 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) 

Answered by Elio Diaz on November 3, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP