如何从r中的列联表中获取带有案例的data.frame?

时间:2016-04-08 12:15:41

标签: r glm contingency

我想从书中重现一些计算(logit回归)。这本书给出了一个列联表和结果。

这是表:

enter image description here

    .


    example <- matrix(c(21,22,6,51), nrow = 2, byrow = TRUE)
    #Labels:
    rownames(example) <- c("Present","Absent")
    colnames(example) <- c(">= 55", "<55")

它给了我这个:

            >= 55 <55
    Present    21  22
    Absent      6  51

但是要使用glm() - 函数,数据必须采用以下方式:

(两个列,一个带有“Age”,一个带有“Present”,填充0/1)

    age <- c(rep(c(0),27), rep(c(1),73))
    present <- c(rep(c(0),21), rep(c(1),6), rep(c(0),22), rep(c(1),51))

    data <- data.frame(present, age)

    > data
        present age
    1         0   0
    2         0   0
    3         0   0
    .         .   .
    .         .   .
    .         .   .
    100       1   1

是否有一种简单的方法可以从表格/矩阵中获取此结构?

5 个答案:

答案 0 :(得分:2)

您也许可以将countsToCases函数用作defined here

countsToCases(as.data.frame(as.table(example))) 
#        Var1  Var2
#1    Present >= 55
#1.1  Present >= 55
#1.2  Present >= 55
#1.3  Present >= 55
#1.4  Present >= 55
#1.5  Present >= 55
# ...

如果您愿意,您可以随后将变量重新编码为数字。

答案 1 :(得分:2)

reshape2::melt(example)

这会给你,

     Var1  Var2 value
1 Present >= 55    21
2  Absent >= 55     6
3 Present   <55    22
4  Absent   <55    51

您可以轻松使用glm

答案 2 :(得分:1)

我会选择:

library(data.table)
tab <- data.table(AGED = c(1, 1, 0, 0),
                  CHD = c(1, 0, 1, 0),
                  Count = c(21, 6, 22, 51))

tabExp <- tab[rep(1:.N, Count), .(AGED, CHD)]

编辑快速解释,因为我花了一些时间才弄明白:

data.table个对象.N中存储组的行数(如果与by分组)或仅存储整个data.table的行数,所以< strong>在此示例中:

tab[rep(1:.N, Count)]

tab[rep(1:4, Count)]

最后

tab[rep(1:4, c(21, 6, 22, 51)]

是等价的。

与基数R相同:

tab2 <- data.frame(AGED = c(1, 1, 0, 0),
                   CHD = c(1, 0, 1, 0),
                   Count = c(21, 6, 22, 51))

tabExp2 <- tab2[rep(1:nrow(tab2), tab2$Count), c("AGED", "CHD")]

答案 3 :(得分:1)

下面的代码可能看起来很长,但只有group_by()do()指令处理扩展数据。剩下的就是以长格式更改数据并将字符变量编码为0和1.我试着从您在问题中给出的确切矩阵开始。

加载数据操作包

library(tidyr)
library(dplyr)

创建数据框

按照示例创建矩阵,但要避免&#34;&gt;&#34;列名称中的标志

example <- matrix(c(21,22,6,51), nrow = 2, byrow = TRUE)
rownames(example) <- c("Present","Absent")
colnames(example) <- c("above55", "below55")

将矩阵转换为数据框

example <- data.frame(example) %>%
    add_rownames("chd")    

或者直接创建数据框

data.frame(chd = c("Present", "Absent"),
           above55 = c(21,6),
           below55 = c(22,51))

重塑数据

data2 <- example %>% 
    gather(age, nrow, -chd) %>%
    # Encode chd and age as 0 or 1
    mutate(chd = ifelse(chd=="Present",1,0),
           age = ifelse(age=="above55",1,0)) %>%
    group_by(chd, age) %>%
    # Expand each variable by nrow
    do(data.frame(chd = rep(.$chd,.$nrow),
                  age = rep(.$age,.$nrow)))

head(data2)
# Source: local data frame [6 x 2]
# Groups: chd, age [1]
# 
#     chd   age
#   (dbl) (dbl)
# 1     0     0
# 2     0     0
# 3     0     0
# 4     0     0
# 5     0     0
# 6     0     0
tail(data2)
# Source: local data frame [6 x 2]
# Groups: chd, age [1]
# 
#     chd   age
#   (dbl) (dbl)
# 1     1     1
# 2     1     1
# 3     1     1
# 4     1     1
# 5     1     1
# 6     1     1

table(data2)
#        age
# chd  0  1
#   0 51  6
#   1 22 21

与您的示例相同,但年龄编码除外 我上面评论中提到的问题。

答案 4 :(得分:1)

所以,glm并不是那么不灵活。部分?glm读取

 For ‘binomial’ and ‘quasibinomial’ families the response can also
 be specified as a ‘factor’ (when the first level denotes failure
 and all others success) or as a two-column matrix with the columns
 giving the numbers of successes and failures.

我假设你想测试年龄对Present/Absent的影响。 关键是指定响应(如psueudo-code)c(success, failure)

所以你需要像data.frame(Age= ..., Present = ..., Absent)这样的数据。从example执行此操作的最简单方法是转置,然后强制转换为data.frame,并添加一列:

example_t <- as.data.frame(t(example))
example_df <- data.frame(example_t, Age=factor(row.names(example_t)))

给你

      Present Absent   Age
>= 55      21      6 >= 55
<55        22     51   <55

然后,你可以运行glm:

glm(cbind(Present, Absent) ~ Age, example_df, family = 'binomial')

获取

Call:  glm(formula = cbind(Present, Absent) ~ Age, family = "binomial",
    data = example_for_glm)

Coefficients:
(Intercept)       Age<55
      1.253       -2.094

Degrees of Freedom: 1 Total (i.e. Null);  0 Residual
Null Deviance:      18.7
Residual Deviance: -1.332e-15   AIC: 11.99

附录

您也可以通过@therimalaya的答案到达此处。但这只是第一步

as.data.frame(as.table(example))

(只能让你分开)

    Var1  Var2 Freq
1 Present >= 55   21
2  Absent >= 55    6
3 Present   <55   22
4  Absent   <55   51

但实际上有一列成功和失败,你需要做更多的事情。您可以使用tidyr到达那里

as.data.frame(as.table(example)) %>% tidyr::spread(Var1, Freq)

类似于上面的example_df

  Var2 Present Absent
1 >= 55      21      6
2   <55      22     51