简体   繁体   中英

How to Optimal Visualize Categorical Regression Results in R

In my data all variables (depended and independed) are categorical. It can be or 1 or 2 (categorical values).

lp1=structure(list(a = c(2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 
2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L), b = c(1L, 1L, 1L, 2L, 2L, 2L, 
1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L), c = c(1L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L
), d = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, NA, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), e = c(1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 
1L, NA, 2L, 2L, 2L, 2L, 1L, 2L, 1L), f = c(2L, 2L, 2L, 2L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L), g = c(NA, 
1L, 2L, NA, 2L, 2L, 1L, 2L, 1L, NA, NA, NA, NA, NA, 1L, NA, NA, 
NA), h = c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), i = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), j = c(2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), k = c(2L, 
1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, NA, 1L, 2L, 1L, 1L, 1L, 1L, 
1L), l = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, NA, 2L, NA, 
1L, 1L, 1L, 1L, 2L), m = c(1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 2L, NA, 2L, 2L, 2L, 1L), n = c(1L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, NA, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L), xxx = c(2L, 
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
2L)), class = "data.frame", row.names = c(NA, -18L))

I try get plot between two variables(xxx is depended and a is independed)

library(ggplot2)

ggplot(lp1, aes(xxx, a)) +
  geom_point() +
  theme_minimal()

the graph is not very informative because there are only 2 values or 1 or 2. 在此处输入图像描述

Is there any way to optimally graph the relationship between two categorical variables? Maybe something normalize data?

I apologize if I'm asking this question in the wrong forum. Thank you.

You could use the table() function to get much better idea of what's going on. This is not a graphical representation, but it's simplicity is not a downfall in my opinion.

library(tidyverse)
lp1 = structure(
  list(
    a = c(2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L),
    b = c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L),
    c = c(1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L),
    d = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, NA, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
    e = c(1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, NA, 2L, 2L, 2L, 2L, 1L, 2L, 1L),
    f = c(2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L),
    g = c(NA, 1L, 2L, NA, 2L, 2L, 1L, 2L, 1L, NA, NA, NA, NA, NA, 1L, NA, NA, NA),
    h = c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
    i = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L),
    j = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
    k = c(2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, NA, 1L, 2L, 1L, 1L, 1L, 1L, 1L),
    l = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, NA, 2L, NA, 1L, 1L, 1L, 1L, 2L),
    m = c(1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, NA, 2L, 2L, 2L, 1L),
    n = c(1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, NA, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L),
    xxx = c(2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L)
  ),
  class = "data.frame", row.names = c(NA, -18L)
)

lp1 %>% 
  select(a, xxx) %>% 
  table()
#>    xxx
#> a   1 2
#>   1 4 2
#>   2 8 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM