How to get data.frame with cases from contingency table in r?

I would like to reproduce some calculations from the book (logit regression). The book gives a table of contingencies and results.

Here is the table:

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")

This gives me the following:

            >= 55 <55
    Present    21  22
    Absent      6  51

But to use the glm () function, the data must be as follows:

(two columns, one with "Age" and one with "Presence", filled 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

Is there an easy way to get this structure from a table / matrix?

+4
source share
5 answers
reshape2::melt(example)

It will give you

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

which you can easily use for glm

+2
source

countsToCases .

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
# ...

, .

+2

:

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, :

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")]
+1

, group_by() do() . - 0 1. , .

library(tidyr)
library(dplyr)

, " > " ​​

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

, , , .

+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

Adding

You can also get an answer here from @therimalaya. But this is only the first step

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

(only there you will be there partially)

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

but actually there is a column of successes and failures, you need to do something else. You can use tidyrto get there

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

similar to mine example_dfabove

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

All Articles