如何将包函数的结果放入r中的数据帧中

时间:2017-03-10 13:19:56

标签: r

我正处于r的学习阶段。 我在r中使用library(usdm)我正在使用vifcor(vardata,th=0.4,maxobservations =50000)来查找非多线性变量。我需要将vifcor(vardata,th=0.4,maxobservations =50000)的结果放入结构化数据框中以供进一步分析。

我正在使用的数据阅读流程:

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

csv文件的内容:

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

我的尝试:

  • r<-vifcor(vardata,th=0.2,maxobservations =50000)返回
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)返回
[1] "VIF"
attr(,"package")
[1] "usdm"
  • mode(r)返回"S4"

我需要Roads_regiona_dst_CHT GrowthCenter_dst_CHT进入数据框,VIFs of the remained variables进入另一个数据框!

但没有任何效果!

2 个答案:

答案 0 :(得分:1)

您应该可以使用以下命令获取广告位中的信息&#39;结果&#39;进入数据框架。然后,您可以使用传统方法将信息拆分为单独的数据框

df <- r@results

请注意,r @ results [1:2,2]会为前两行提供VIF。

答案 1 :(得分:1)

基本上,重新生成的结果是S4类,您可以通过@运算符提取广告位:

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

因此,您现在可以通过以下方式提取resultsexcluded广告位:

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