简体   繁体   中英

How to get result of package function into a dataframe in r

I am at the learning stage of r. I am using library(usdm) in r where I am using vifcor(vardata,th=0.4,maxobservations =50000) to find the not multicollinear variables. I need to get the result of vifcor(vardata,th=0.4,maxobservations =50000) into a structured dataframe for further analysis.

Data reading process I am using:

performdata <- read.csv('F:/DGDNDRV_FINAL/OutputTextFiles/data_blk.csv')
vardata <-performdata[,c(names(performdata[5:length(names(performdata))-2])]

Content of the csv file:

pointid grid_code   Blocks_line_dst_CHT GrowthCenter_dst_CHT    Roads_nationa_dst_CHT   Roads_regiona_dst_CHT   Settlements_CHT_line_dst_CHT    Small_Hat_Bazar_dst_CHT Upazilla_lin_dst_CHT    resp
1   6   150 4549.428711 15361.31836 3521.391846 318.9043884 3927.594727 480 1
2   6   127.2792206 4519.557617 15388.68457 3500.24292  342.0526123 3902.883545 480 1
3   2   161.5549469 4484.473145 15391.6377  3436.539063 335.4101868 3844.216553 540 1

My tries:

  • r<-vifcor(vardata,th=0.2,maxobservations =50000) returns
 2 variables from the 6 input variables have collinearity problem: Roads_regiona_dst_CHT GrowthCenter_dst_CHT After excluding the collinear variables, the linear correlation coefficients ranges between: min correlation ( Small_Hat_Bazar_dst_CHT ~ Roads_nationa_dst_CHT ): -0.04119076963 max correlation ( Small_Hat_Bazar_dst_CHT ~ Settlements_CHT_line_dst_CHT ): 0.1384278434 ---------- VIFs of the remained variables -------- Variables VIF 1 Blocks_line_dst_CHT 1.026743892 2 Roads_nationa_dst_CHT 1.010556752 3 Settlements_CHT_line_dst_CHT 1.038307666 4 Small_Hat_Bazar_dst_CHT 1.026943711 
  • class(r) returns
 [1] "VIF" attr(,"package") [1] "usdm" 
  • mode(r) returns "S4"

I need Roads_regiona_dst_CHT GrowthCenter_dst_CHT into a dataframe and VIFs of the remained variables into another dataframe!

But nothing worked!

You should be able to use the below command to get the information in the slot 'results' into a data frame. You can then split the information out into separate data frames using traditional methods

df <- r@results

Note that r@results[1:2,2] would give you the VIF for the first two rows.

Basically the resturned result is a S4 class and you can extract slots via the @ operator:

library(usdm)
example(vifcor) # creates 'v2'
str(v2)
# Formal class 'VIF' [package "usdm"] with 4 slots
#   ..@ variables: chr [1:10] "Bio1" "Bio2" "Bio3" "Bio4" ...
#   ..@ excluded : chr [1:5] "Bio5" "Bio10" "Bio7" "Bio6" ...
#   ..@ corMatrix: num [1:5, 1:5] 1 0.0384 -0.3011 0.0746 0.7102 ...
#   .. ..- attr(*, "dimnames")=List of 2
#   .. .. ..$ : chr [1:5] "Bio1" "Bio2" "Bio3" "Bio8" ...
#   .. .. ..$ : chr [1:5] "Bio1" "Bio2" "Bio3" "Bio8" ...
#   ..@ results  :'data.frame':   5 obs. of  2 variables:
#   .. ..$ Variables: Factor w/ 5 levels "Bio1","Bio2",..: 1 2 3 4 5
#   .. ..$ VIF      : num [1:5] 2.09 1.37 1.25 1.27 2.31

So you can extract the results and the excluded slot now via:

v2@excluded
# [1] "Bio5"  "Bio10" "Bio7"  "Bio6"  "Bio4"
v2@results
#   variables      VIF
# 1      Bio1 2.086186
# 2      Bio2 1.370264
# 3      Bio3 1.253408
# 4      Bio8 1.267217
# 5      Bio9 2.309479

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM