【问题标题】:Why are there redundant rows and columns in my contingency table? [duplicate]为什么我的列联表中有多余的行和列? [复制]
【发布时间】:2021-11-24 13:40:43
【问题描述】:

我是 R 新手,目前正在学习列联表。我想使用来自“loans_full_schema”(来自 openintro)的数据以及“application_type”和“homeownership”数据创建一个列联表。下面是我的代码。

library(oibiostat)

data("loans_full_schema")

tab <- table(loans_full_schema$application_type, loans_full_schema$homeownership)
tab

我的结果是my outcome

然而,我希望能够得到如下结果wanted outcome
所以我的问题是为什么我的结果中有一个“任何”列和一个空白行?

【问题讨论】:

  • 你能给出levels(loans_full_schema$application_type)的结果吗?
  • > 级别(loans_full_schema$application_type) [1] "" "个人" "联合"

标签: r contingency


【解决方案1】:

那是因为数据中有空层。

levels(loans_full_schema$homeownership)
#[1] ""         "ANY"      "MORTGAGE" "OWN"      "RENT"    

levels(loans_full_schema$application_type)
#[1] ""           "individual" "joint"     

您可以使用droplevels 删除它们。

loans_full_schema <- droplevels(loans_full_schema) 

table(loans_full_schema$application_type, loans_full_schema$homeownership)
            
#             MORTGAGE  OWN RENT
#  individual     3839 1170 3496
#  joint           950  183  362

您可以使用addmargins 来添加总数。

addmargins(table(loans_full_schema$application_type, loans_full_schema$homeownership))

#             MORTGAGE   OWN  RENT   Sum
#  individual     3839  1170  3496  8505
#  joint           950   183   362  1495
#  Sum            4789  1353  3858 10000

【讨论】:

    猜你喜欢
    • 2022-11-03
    • 2021-03-07
    • 2020-02-07
    • 2013-11-05
    • 2020-09-13
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2017-02-20
    相关资源
    最近更新 更多