使用 R 查找数据中的关系

发布日期:2026-06-25 05:33:04   来源 : 杭州电子商务研究院    浏览量 :11
杭州电子商务研究院 发布日期:2026-06-25 05:33:04  
11

介绍

构建高性能机器学习算法取决于识别变量之间的关系。这有助于特征工程以及决定机器学习算法。在本指南中,您将学习使用 R 在数据中查找关系的技术。

数据

在本指南中,我们将使用一个虚构的贷款申请人数据集,其中包含 200 个观测值和 10 个变量,如下所述:

  1. Marital_status申请人是否已婚(“是”)或未婚(“否”)

  2. Is_graduate申请人是否为毕业生(“是”)或不是(“否”)

  3. 收入申请人的年收入(美元)

  4. Loan_amount提交申请的贷款金额(美元)

  5. Credit_score申请人的信用评分是好(“好”)还是不好(“坏”)。

  6. Approval_status贷款申请是否被批准(“是”)或未被批准(“否”)。

  7. 投资申请人申报的股票和共同基金投资额(美元)

  8. 性别申请人是“女性”还是“男性”

  9. 年龄申请人的年龄(岁)

  10. Work_exp申请人的工作经验(年数)

让我们首先加载所需的库和数据。

      library(plyr)
library(readr)
library(ggplot2)
library(GGally)
library(dplyr)
library(mlbench)

dat <- read_csv("data_test.csv")
glimpse(dat)
    

输出:

      Observations: 200
Variables: 10
$ Marital_status  <chr> "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"...
$ Is_graduate     <chr> "No", "No", "No", "No", "No", "No", "No", "No", "No", ...
$ Income          <int> 72000, 64000, 80000, 76000, 72000, 56000, 48000, 72000...
$ Loan_amount     <int> 70500, 70000, 275000, 100500, 51500, 69000, 147000, 61...
$ Credit_score    <chr> "Bad", "Bad", "Bad", "Bad", "Bad", "Bad", "Bad", "Bad"...
$ approval_status <chr> "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"...
$ Investment      <int> 117340, 85340, 147100, 65440, 48000, 136640, 160000, 9...
$ gender          <chr> "Female", "Female", "Female", "Female", "Female", "Fem...
$ age             <int> 34, 34, 33, 34, 33, 34, 33, 33, 33, 33, 34, 33, 33, 33...
$ work_exp        <dbl> 8.10, 7.20, 9.00, 8.55, 8.10, 6.30, 5.40, 8.10, 8.10, ...
    

输出显示数据集有五个数字(标记为intdbl)和五个字符变量(标记为chr)。我们将使用下面的代码行将它们转换为因子变量。

      names <- c(1,2,5,6,8)
dat[,names] <- lapply(dat[,names] , factor)
glimpse(dat)
    

输出:

      Observations: 200
Variables: 10
$ Marital_status  <fct> Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes,...
$ Is_graduate     <fct> No, No, No, No, No, No, No, No, No, No, No, No, Yes, Y...
$ Income          <int> 72000, 64000, 80000, 76000, 72000, 56000, 48000, 72000...
$ Loan_amount     <int> 70500, 70000, 275000, 100500, 51500, 69000, 147000, 61...
$ Credit_score    <fct> Bad, Bad, Bad, Bad, Bad, Bad, Bad, Bad, Bad, Bad, Bad,...
$ approval_status <fct> Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes, Yes,...
$ Investment      <int> 117340, 85340, 147100, 65440, 48000, 136640, 160000, 9...
$ gender          <fct> Female, Female, Female, Female, Female, Female, Female...
$ age             <int> 34, 34, 33, 34, 33, 34, 33, 33, 33, 33, 34, 33, 33, 33...
$ work_exp        <dbl> 8.10, 7.20, 9.00, 8.55, 8.10, 6.30, 5.40, 8.10, 8.10, ...
    

数值变量之间的关系

许多机器学习算法要求连续变量之间不能相互关联,这种现象称为“多重共线性”。建立数值变量之间的关系是检测和处理多重共线性的常见步骤。

相关矩阵

创建相关矩阵是一种识别数值变量间多重共线性的技术。以下代码行创建了矩阵。

      cordata = dat[,c(3,4,7,9,10)]
corr <- round(cor(cordata), 1)
corr
    

输出:

      Income Loan_amount Investment  age work_exp
Income         1.0         0.0        0.1 -0.2      0.9
Loan_amount    0.0         1.0        0.8  0.0      0.0
Investment     0.1         0.8        1.0  0.0      0.1
age           -0.2         0.0        0.0  1.0     -0.1
work_exp       0.9         0.0        0.1 -0.1      1.0
    

上面的输出显示变量IncomeWork_exp之间以及InvestmentLoan_amount之间存在强线性相关性。

相关图

还可以使用相关性图来可视化相关性,这是使用ggcorrplot包实现的。此库使用下面的第一行代码加载。

第二行创建相关图,其中colorsoutline.colorshow.legend等参数用于控制图表的显示。

      library(ggcorrplot)

ggcorrplot(corr, hc.order = TRUE, type = "lower", lab = TRUE, lab_size = 3, method="circle", colors = c("blue", "white", "red"), outline.color = "gray", show.legend = TRUE, show.diag = FALSE, title="Correlogram of loan variables")
    

输出:

相关性检验

相关性检验是另一种确定两个定量变量之间是否存在线性关系及其程度的方法。在我们的案例中,我们想从统计上检验申请人的投资和工作经验之间是否存在相关性。

第一步是用散点图来可视化关系,这在下面的代码行中完成。

      plot(dat$Investment,dat$work_exp, main="Correlation between Investment Levels & Work Exp", xlab="Work experience in years", ylab="Investment in USD")
    

输出:

上图表明两个变量之间不存在线性关系。我们可以通过使用下面的代码行计算相关系数来量化这种推论。

      cor(dat$Investment, dat$work_exp)
    

输出:

      1] 0.07653245
    

0.07 的值表明两个变量之间存在正但弱的线性关系。让我们使用相关性测试来确认这一点,该测试在 R 中使用cor.test()函数完成。

基本语法是cor.test(var1, var2, method = “method”),默认方法是pearson。这是使用下面的代码行完成的。

      cor.test(dat$Investment, dat$work_exp)
    

输出:

      Pearson's product-moment correlation

data:  dat$Investment and dat$work_exp

t = 1.0801, df = 198, p-value = 0.2814

alternative hypothesis: true correlation is not equal to 0

95 percent confidence interval:  -0.0628762,   0.2130117

sample estimates:  cor - 0.07653245
    

由于p值0.2814大于0.05,我们无法拒绝申请人的投资与其工作经验之间的关系不显著的原假设。

让我们使用下面的代码行来考虑收入工作指数之间的相关性的另一个例子。

      cor.test(dat$Income, dat$work_exp)
    

输出:

      Pearson's product-moment correlation
 
data:  dat$Income and dat$work_exp
 
t = 25.869, df = 198, p-value < 2.2e-16
 
alternative hypothesis: true correlation is not equal to 0
 
95 percent confidence interval: 0.8423810; 0.9066903
 
sample estimates: cor - 0.8784546
    

在这种情况下,p值小于0.05,因此我们拒绝申请人的收入与其工作经验之间的关系不显著的原假设。

分类变量之间的关系

在前面的部分中,我们介绍了寻找数值变量之间关系的技术。理解和估计分类变量之间的关系也同样重要。

频率表

创建频率表是查找两个分类变量之间分布的简单但有效的方法。table ()函数可用于创建两个变量之间的双向表。

在下面的第一行代码中,我们在变量marital_statusapproved_status之间创建了一个双向表。第二行打印频率表,第三行打印比例表。第四行打印行比例表,第五行打印列比例表。

      # 2 - way table
two_way = table(dat$Marital_status, dat$approval_status)
two_way

prop.table(two_way) # cell percentages
prop.table(two_way, 1) # row percentages
prop.table(two_way, 2) # column percentages
    

输出:

      #Output - two_way table

           No Yes
  Divorced 31  29
  No       66  10
  Yes      52  12


#Output - cell percentages table

             No   Yes
  Divorced 0.155 0.145
  No       0.330 0.050
  Yes      0.260 0.060

#Output - row percentages table

                No       Yes
  Divorced 0.5166667 0.4833333
  No       0.8684211 0.1315789
  Yes      0.8125000 0.1875000


#Output - column percentages table

                 No       Yes
  Divorced 0.2080537 0.5686275
  No       0.4429530 0.1960784
  Yes      0.3489933 0.2352941
    

列百分比表的输出显示,离婚申请人(56.8%)获得贷款批准的概率高于已婚申请人(19.6%)。为了检验这一见解是否具有统计意义,我们使用独立性卡方检验。

独立性卡方检验

独立性卡方检验用于确定两个或多个分类变量之间是否存在关联。在我们的案例中,我们想测试申请人的婚姻状况是否与批准状态有任何关联。

<font style="

以上内容来自杭州电子商务研究院推送
关注
关于我们
热门推荐
合作伙伴
免责声明:本站部分资讯来源于网络,如有侵权请及时联系客服,我们将尽快处理
Copyright © 2025-2027 ToB产业网址导航 公安备案 浙公网安备33010602013138号 浙ICP备16025413号-9
支持 反馈 关注 数据