zoukankan      html  css  js  c++  java
  • 【应用】R--判断类别型属性之间是否有相关性(相互之间是否独立)

    检验某学区所有在售房源中,小区与楼栋类别(低层;多层;小高层;高层)是否相关

    导入数据:

    > house<- read.table("house_data.txt", header = TRUE, sep='|',fileEncoding ="UTF-8",
                      stringsAsFactors = FALSE, 
                      colClasses = c("character","character","numeric",
                                     "character","numeric","numeric","character",
                                     "numeric","numeric","character"))
    > library(sqldf)

    > houseXQ<- sqldf("select * from house where  community_name!='东郊小镇' ",row.names=TRUE)

     > head(houseXQ)

      结果如下:

                       id community_name house_area house_structure house_total house_avg    agency_name house_floor_curr house_floor_total
    1 6328500962692431872       尚东花园         77          3室2厅         285     37013       利众置业                5                 5
    2 6328500979813580800     赛世香樟园         93          2室2厅         265     28495 苏商房产仙林店                9                11
    3 6328501016647958528       雁鸣山庄         84          2室2厅         246     29286       个人房源                4                 5
    4 6328501019412004864       仙林新村         58          2室2厅         155     26724     苏恒不动产                5                 6
    5 6328501019420393472       仙林新村         61          2室2厅         160     26230     苏恒不动产                4                 6
    6 6328501019428782080       仙林新村         59          2室2厅         161     27288     苏恒不动产                4                 6
      house_floor_type
    1             多层
    2           小高层
    3             多层
    4             多层
    5             多层
    6             多层

    生成列联表(频数表):

    > houseTable<- xtabs(~community_name + house_floor_type, data=houseXQ)
    

      

    看一下新表格:

    > houseTable
    

    结果如下:

    house_floor_type
    community_name None 低层 多层 高层 小高层
        东方天郡      0    1    2   46      2
        康桥圣菲      0    1    8    0      0
        南师大茶苑    1    0    0    0      0
        赛世香樟园    0    0    0    2      1
        三味公寓      0    0    2    0      0
        尚东花园      0    0    1    0      0
        仙鹤山庄      0    0    1    0      0
        仙林新村      0    0    7    0      0
        亚东城        0    0    1   46     62
        雁鸣山庄      0    0    4    0      0
        咏梅山庄      0    0    1    0      0
    从数据可以直观看出来,康桥圣菲可能都是多层的,亚东城是小高层(11层)和高层(18层)
    使用  CrossTable()函数生成频数表:
    > CrossTable(houseXQ$community_name, houseXQ$house_floor_type)
    

      结果如下:

     Cell Contents
    |-------------------------|
    |                       N |
    | Chi-square contribution |
    |           N / Row Total |
    |           N / Col Total |
    |         N / Table Total |
    |-------------------------|
     
    Total Observations in Table:  189 
     
                           | houseXQ$house_floor_type 
    houseXQ$community_name |      None |      低层 |      多层 |      高层 |    小高层 | Row Total | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
                  东方天郡 |         0 |         1 |         2 |        46 |         2 |        51 | 
                           |     0.270 |     0.393 |     3.835 |    16.787 |    13.768 |           | 
                           |     0.000 |     0.020 |     0.039 |     0.902 |     0.039 |     0.270 | 
                           |     0.000 |     0.500 |     0.074 |     0.489 |     0.031 |           | 
                           |     0.000 |     0.005 |     0.011 |     0.243 |     0.011 |           | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
                  康桥圣菲 |         0 |         1 |         8 |         0 |         0 |         9 | 
                           |     0.048 |     8.595 |    35.063 |     4.476 |     3.095 |           | 
                           |     0.000 |     0.111 |     0.889 |     0.000 |     0.000 |     0.048 | 
                           |     0.000 |     0.500 |     0.296 |     0.000 |     0.000 |           | 
                           |     0.000 |     0.005 |     0.042 |     0.000 |     0.000 |           | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
                南师大茶苑 |         1 |         0 |         0 |         0 |         0 |         1 | 
                           |   187.005 |     0.011 |     0.143 |     0.497 |     0.344 |           | 
                           |     1.000 |     0.000 |     0.000 |     0.000 |     0.000 |     0.005 | 
                           |     1.000 |     0.000 |     0.000 |     0.000 |     0.000 |           | 
                           |     0.005 |     0.000 |     0.000 |     0.000 |     0.000 |           | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
                赛世香樟园 |         0 |         0 |         0 |         2 |         1 |         3 | 
                           |     0.016 |     0.032 |     0.429 |     0.173 |     0.001 |           | 
                           |     0.000 |     0.000 |     0.000 |     0.667 |     0.333 |     0.016 | 
                           |     0.000 |     0.000 |     0.000 |     0.021 |     0.015 |           | 
                           |     0.000 |     0.000 |     0.000 |     0.011 |     0.005 |           | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
                  三味公寓 |         0 |         0 |         2 |         0 |         0 |         2 | 
                           |     0.011 |     0.021 |    10.286 |     0.995 |     0.688 |           | 
                           |     0.000 |     0.000 |     1.000 |     0.000 |     0.000 |     0.011 | 
                           |     0.000 |     0.000 |     0.074 |     0.000 |     0.000 |           | 
                           |     0.000 |     0.000 |     0.011 |     0.000 |     0.000 |           | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
                  尚东花园 |         0 |         0 |         1 |         0 |         0 |         1 | 
                           |     0.005 |     0.011 |     5.143 |     0.497 |     0.344 |           | 
                           |     0.000 |     0.000 |     1.000 |     0.000 |     0.000 |     0.005 | 
                           |     0.000 |     0.000 |     0.037 |     0.000 |     0.000 |           | 
                           |     0.000 |     0.000 |     0.005 |     0.000 |     0.000 |           | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
                  仙鹤山庄 |         0 |         0 |         1 |         0 |         0 |         1 | 
                           |     0.005 |     0.011 |     5.143 |     0.497 |     0.344 |           | 
                           |     0.000 |     0.000 |     1.000 |     0.000 |     0.000 |     0.005 | 
                           |     0.000 |     0.000 |     0.037 |     0.000 |     0.000 |           | 
                           |     0.000 |     0.000 |     0.005 |     0.000 |     0.000 |           | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
                  仙林新村 |         0 |         0 |         7 |         0 |         0 |         7 | 
                           |     0.037 |     0.074 |    36.000 |     3.481 |     2.407 |           | 
                           |     0.000 |     0.000 |     1.000 |     0.000 |     0.000 |     0.037 | 
                           |     0.000 |     0.000 |     0.259 |     0.000 |     0.000 |           | 
                           |     0.000 |     0.000 |     0.037 |     0.000 |     0.000 |           | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
                    亚东城 |         0 |         0 |         1 |        46 |        62 |       109 | 
                           |     0.577 |     1.153 |    13.636 |     1.244 |    16.030 |           | 
                           |     0.000 |     0.000 |     0.009 |     0.422 |     0.569 |     0.577 | 
                           |     0.000 |     0.000 |     0.037 |     0.489 |     0.954 |           | 
                           |     0.000 |     0.000 |     0.005 |     0.243 |     0.328 |           | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
                  雁鸣山庄 |         0 |         0 |         4 |         0 |         0 |         4 | 
                           |     0.021 |     0.042 |    20.571 |     1.989 |     1.376 |           | 
                           |     0.000 |     0.000 |     1.000 |     0.000 |     0.000 |     0.021 | 
                           |     0.000 |     0.000 |     0.148 |     0.000 |     0.000 |           | 
                           |     0.000 |     0.000 |     0.021 |     0.000 |     0.000 |           | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
                  咏梅山庄 |         0 |         0 |         1 |         0 |         0 |         1 | 
                           |     0.005 |     0.011 |     5.143 |     0.497 |     0.344 |           | 
                           |     0.000 |     0.000 |     1.000 |     0.000 |     0.000 |     0.005 | 
                           |     0.000 |     0.000 |     0.037 |     0.000 |     0.000 |           | 
                           |     0.000 |     0.000 |     0.005 |     0.000 |     0.000 |           | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
              Column Total |         1 |         2 |        27 |        94 |        65 |       189 | 
                           |     0.005 |     0.011 |     0.143 |     0.497 |     0.344 |           | 
    -----------------------|-----------|-----------|-----------|-----------|-----------|-----------|
    

    独立性检验

    1、卡方检验:对二维表的行变量和列变量进行卡方独立性检验。

    卡方分布—— n个独立的标准正态分布变量的平方和服从自由度为 n 的卡方分布(分类资料相关性检验)

    http://www.cnblogs.com/quietwalk/p/8276097.html

    假设:楼盘与类型相互独立,没有相关性。

    对上面生成的列联表 houseTable 进行卡方检验:

    > chisq.test(houseTable)
    

      结果:

    Pearson's Chi-squared test
    
    data:  houseTable
    X-squared = 403.62, df = 40, p-value < 2.2e-16
    
    Warning message:
    In chisq.test(houseTable) : Chi-squared近似算法有可能不准

    P值 <0.05进行说明假设【楼盘与类型相互独立,没有相关性】不成立。
    也就是说,哪个小区里面有什么样的楼(是低层,多层,小高层,高层)是确定的(因为早在建造前规划的时候就定了呀~~~)

    不可能说要建个小区,然后开发商掷个东南西北的色子:
    如果结果是东,就建低层;如果结果是西,就建多层;如果结果是南,就建小高层;如果结果是北,就建高层。
    如果是这样的话,那么小区与楼的类型就没有相关性。


    2、Fisher精确检验:原假设为——边界固定的列联表中行和列是相互独立的。
    >fisher.test(houseTable,simulate.p.value=TRUE,B=2000)
    

    结果为:

    Fisher's Exact Test for Count Data with simulated p-value (based on 2000 replicates)
    
    data:  houseTable
    p-value = 0.0004998
    alternative hypothesis: two.sided

    说明:可以看出,p值<0.05,原假设不成立,即相互独立不成立,
    houseXQ$community_name 与 houseXQ$house_floor_type具有相关性。
    注意:
    如果直接用fisher.test(houseTable)会报以下错误:
    FEXACT error 6.
    LDKEY is too small for this problem.
    Try increasing the size of the workspace.
    但是如果simulate.p.value=TRUE,B=2000,设置了蒙特卡洛模拟和模拟次数,用fisher检验就不会出错。


    3、 Cochran-Mantel-Haenszel 检验
    原假设为:两个类别变量在第三个变量的每一层中都是条件独立的。
    >houseTable<- xtabs(~community_name + house_floor_type + house_structure, data=houseXQ)
    >houseTable
    > mantelhaen.test(houseTable)
    

      

    Error in mantelhaen.test(houseTable) : 每个阶层里的样本大小都必需大于一


     




  • 相关阅读:
    Javascript中最常用的55个经典技巧(转)
    Yahoo!网站性能最佳体验的34条黄金守则(转载)
    你误解了Windows的文件后缀名吗?
    你不知道的Spring配置文件
    Thrift 个人实战--Thrift 的序列化机制
    Thrift 个人实战--Thrift 网络服务模型
    Thrift 个人实战--初次体验Thrift
    HBase 实战(2)--时间序列检索和面检索的应用场景实战
    Kafka实战系列--Kafka API使用体验
    Kafka实战系列--Kafka的安装/配置
  • 原文地址:https://www.cnblogs.com/quietwalk/p/8258547.html
Copyright © 2011-2022 走看看