簡體   English   中英

如何在 R 中循環匯總統計信息

[英]How can I summarize statistics in a loop in R

我有一個包含大約 60 個變量(A、B、C、D、...)的數據集,每個變量都有 3 個對應的信息列(A、Group_A 和 WOE_A),如下表所示:

ID  A   Group_A WOE_A   B   Group_B WOE_B   C   Group_C WOE_C   D   Group_D WOE_D   Status
213 0   1   0.87    0   1   0.65    0   1   0.80    915.7   4   -0.30   1
321 12  5   0.08    4   4   -0.43   6   5   -0.20   85.3    2   0.26    0
32  0   1   0.87    0   1   0.65    0   1   0.80    28.6    2   0.26    1
13  7   4   -0.69   2   3   -0.82   4   4   -0.80   31.8    2   0.26    0
43  1   2   -0.04   1   2   -0.49   1   2   -0.22   51.7    2   0.26    0
656 2   3   -0.28   2   3   -0.82   2   3   -0.65   8.5 1   1.14    0
435 2   3   -0.28   0   1   0.65    0   1   0.80    39.8    2   0.26    0
65  8   4   -0.69   3   4   -0.43   5   4   -0.80   243.0   3   0.00    0
565 0   1   0.87    0   1   0.65    0   1   0.80    4.0 1   1.14    0
432 0   1   0.87    0   1   0.65    0   1   0.80    81.6    2   0.26    0

我想在 R 中打印一個帶有一些統計信息的表( Min(A), Max(A), WOE_A, Count(Group_A), Count(Group_A, where Status=1), Count(Group_A, where Status=0) ), 60 個變量中的每一個都按 Group 分組,我認為我需要循環執行它。 我嘗試了“dplyr”package,但我不知道如何引用與變量 (A) 相關的所有三列(A、Group_A 和 WOE_A)以及如何匯總所有所需統計信息的信息。

我開始的代碼是:

df <- data
List <- list(df)
for (colname in colnames(df)) {
  List[[colname]]<- df %>%
    group_by(df[,colname]) %>%
    count()
}
List

這就是我想要打印結果的方式:

**Var A                       
Group   Min(A)  Max(A)  WOE_A   Count(Group_A)  Count_1(Group_A, where Status=1)  Count_0(Group_A, where Status=0)**
1                       
2                       
3                       
4                       
5   

非常感謝!

勞拉

Laura,正如其他人所提到的,使用“長”數據幀比使用寬數據幀更好。

您最初使用dplyrgroup_by()的想法讓您幾乎實現了目標。 注意:這也是一種分解數據然后將其與通用列組合的方法,如果寬-長正在突破極限。

讓我們從這個開始:

library(dplyr)

#---------- extract all "A" measurements
df %>% 
   select(A, Group_A, WOE_A, Status) %>% 
#---------- grouped summary of multiple stats
   group_by(A) %>% 
   summarise(
       Min = min(A)
    ,  Max = max(A)
    ,  WOE_A = unique(WOE_A) 
    ,   Count = n()    # n() is a helper function of dplyr
    ,  CountStatus1 = sum(Status == 1)  # use sum() to count logical conditions
    ,  CountStatus0 = sum(Status == 0)
)

這產生:

# A tibble: 6 x 7
      A   Min   Max WOE_A Count CountStatus1 CountStatus0
  <dbl> <dbl> <dbl> <dbl> <int>        <int>        <int>
1     0     0     0  0.87     4            2            2
2     1     1     1 -0.04     1            0            1
3     2     2     2 -0.28     2            0            2
4     7     7     7 -0.69     1            0            1
5     8     8     8 -0.69     1            0            1
6    12    12    12  0.08     1            0            1

好的。 在嵌套測量和變量名稱時,將寬 dataframe 變成長 go 並非易事。 最重要的是, IDStatus是每一行的 ids/key 變量。

將寬轉換為長的標准工具是tidyrpivot_longer() 閱讀此內容。 在您的特定情況下,我們希望將多個列推送到多個目標中。 為此,您需要了解.value哨兵。 pivot_longer()幫助頁面可能有助於研究這種情況。

為了減輕構建復雜正則表達式來解碼變量名稱的痛苦,我將您的group-id-label (例如 A、B)重命名為X_A 、 X_B . This ensures that all column-names are built in the form of . This ensures that all column-names are built in the form of構建!

library(tidyr)

    df %>% 
    # ----------- prepare variable names to be well-formed, you may do this upstream
      rename(X_A = A, X_B = B, X_C = C, X_D = D) %>%
     
    # ----------- call pivot longer with .value sentinel and names_pattern
    # ----------- that is an advanced use of the capabilities
      pivot_longer(
          cols = -c("ID","Status")         # apply to all cols besides ID and Status
       , names_to = c(".value", "label")   # target column names are based on origin names
                                           # and an individual label (think id, name as u like)
       , names_pattern = "(.*)(.*_[A-D]{1})$")  # regex for the origin column patterns
                                                # pattern is built of 2 parts (...)(...)
                                                # (.*) no or any symbol possibly multiple times
                                                # (.*_[A-D]{1}) as above, but ending with underscore and 1 letter 

這給你

# A tibble: 40 x 6
      ID Status label     X Group   WOE
   <dbl>  <dbl> <chr> <dbl> <dbl> <dbl>
 1   213      1 _A      0       1  0.87
 2   213      1 _B      0       1  0.65
 3   213      1 _C      0       1  0.8 
 4   213      1 _D    916.      4 -0.3 
 5   321      0 _A     12       5  0.08
 6   321      0 _B      4       4 -0.43
 7   321      0 _C      6       5 -0.2 
 8   321      0 _D     85.3     2  0.26
 9    32      1 _A      0       1  0.87
10    32      1 _B      0       1  0.65

把所有的放在一起

df %>% 
# ------------ prepare and make long
   rename(X_A = A, X_B = B, X_C = C, X_D = D) %>% 
   pivot_longer(cols = -c("ID","Status")
               , names_to = c(".value", "label")
               , names_pattern = "(.*)(.*_[A-D]{1})$") %>% 

# ------------- calculate stats on groups
  group_by(label, X) %>% 
  summarise(Min = min(X),  Max = max(X),  WOE = unique(WOE)
           ,Count = n(),  CountStatus1 = sum(Status == 1)
           , CountStatus0 = sum(Status == 0)
)

瞧:

# A tibble: 27 x 8
# Groups:   label [4]
   label     X   Min   Max   WOE Count CountStatus1 CountStatus0
   <chr> <dbl> <dbl> <dbl> <dbl> <int>        <int>        <int>
 1 _A        0     0     0  0.87     4            2            2
 2 _A        1     1     1 -0.04     1            0            1
 3 _A        2     2     2 -0.28     2            0            2
 4 _A        7     7     7 -0.69     1            0            1
 5 _A        8     8     8 -0.69     1            0            1
 6 _A       12    12    12  0.08     1            0            1
 7 _B        0     0     0  0.65     5            2            3
 8 _B        1     1     1 -0.49     1            0            1
 9 _B        2     2     2 -0.82     2            0            2
10 _B        3     3     3 -0.43     1            0            1
# ... with 17 more rows

我設法做的循環在下面可用。 除了我想列出的表格之外,我還需要制作一個圖表來顯示每個列出的表格中的一些信息,然后在不同的頁面上打印一個 PDF,其中包含每個變量以及相應的表格和圖表。

    data <- as.data.frame(data)
    
    # 5 is the column where my first information related to a variable is, so for each variable I am building the data with its' related columns
    i <- 5 
    #each variable has 3 columns (Value, Group, WOE)
    for (i in seq(5, 223, 3)){   
    ID <- data[,1]
    A <- data[,i]
    Group <- data[,i+1]
    WOE <- data[,i+2]
    Status <- data[,224]
    df <- cbind(ID, A, Group, WOE, Status) 
    df <- data.frame(df)
    
    # Perform table T with its' corresponding statistics
    T <- df %>% 
    select(A, Group, WOE, Status) %>% 
    group_by(Group) %>% 
    summarise(
      Min = min(A, na.rm=TRUE),  Max = max(A, na.rm=TRUE),  WOE = unique(WOE),   
      Count = n(), 
      CountStatus1 = sum(Status == 1), 
      CountStatus0 = sum(Status == 0),
      BadRate = round((CountStatus1/Count)*100,1))
      print(colnames(data)[i])
      print(T)

    # Then I plot some information from Table T
    p <- ggplot(T) + geom_col(aes(x=Group, y=CountStatus1), size = 1, color = "darkgreen", fill = "darkgreen")
    p <- p + geom_line(aes(x=Group, y=WOE*1000), col="firebrick", size=0.9) + 
    geom_point(aes(x=Group, y=WOE*1000), col="gray", size=3) + 
    ggtitle(label = paste("WOE and Event Count by Group", " - " , colnames(data)[i])) + 
    labs(x = "Group", y = "Event Count", size=7) +
    theme(plot.title = element_text(size=8, face="bold", margin = margin(10, 0, 10, 0)), 
          axis.text.x = element_text(angle=0, hjust = 1)) +
    scale_y_continuous(sec.axis = sec_axis(trans = ~ . /1000, name="WOE", breaks = seq(-3, 5, 0.5)))
    print(p)
}

為我需要的所有變量打印信息,如下圖所示:

變量之一的表

相同變量的圖表

但是,現在我在 pdf 中導出結果時遇到了一些問題。 我不知道如何在 PDF 的不同頁面上打印每個表格和圖表的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM