无论如何,有没有自动(手动)R代码来计算P值

时间:2019-05-17 07:10:41

标签: r

我正在尝试自动化下面的R代码,其中我正在计算p值。数据为csv格式。 我有每个部分及其版本的点击次数和公开号码。 如果有人可以帮助您应用任何循环或其他内容。

我具有.csv格式的数据:

    `    Total Clicks                
    Section   Version A  Version B  Version C   Version D  
    Section1    1,999   2,116              2,307    2,568
    Section2    3,450   1,781              3,416    1,399
    Section3    1,773     915              1,744      644
    Section4        0   2,255                  0    1,432
    Section5      588     573                721      235
    Main email  7,222   7,067              7,467    6,043
    Total email 7,810   7,640              8,188    6,278
    `

    `Version # Opens
    A    9,073
    B    9,150
    C    9,215
    D    9,153
   `

目前,我正在以以下格式手动分配数据:

   ` S1_Click_A=1,999 ####(section 1, email A)
    S1_Click_B=2,116 ## (section 1, email B)
    S1_Click_C=2,307
    S1_Click_D=2,568
    S2_Click_A=3,450 
    S2_Click_B=1,781 
    .
    .
    .
    S5_Click_C=721 
    S5_Click_D=235
    MainBody_Click_A=7,222
    MainBody_Click_B=7,067
    .
    .
    TotalEmail_Click_C=8,188
    TotalEmail_Click_D=6,278
`

   `# to test % total click is the comparable across versions`
   `# section 1 test 
   S1ab <- prop.test(x = c(S1_Click_A,S1_Click_B), n = c(Open_A,Open_B))
   S1ac <- prop.test(x = c(S1_Click_A,S1_Click_C), n = c(Open_A,Open_C))
   S1ad <- prop.test(x = c(S1_Click_A,S1_Click_D), n = c(Open_A,Open_D))
   S1bc <- prop.test(x = c(S1_Click_B,S1_Click_C), n = c(Open_B,Open_C))
   S1bd <- prop.test(x = c(S1_Click_B,S1_Click_D), n = c(Open_B,Open_D))
   S1cd <- prop.test(x = c(S1_Click_C,S1_Click_D), n = c(Open_C,Open_D))
        `
   `#section 2 test
   S2ab <- prop.test(x = c(S2_Click_A,S2_Click_B), n = c(Open_A,Open_B))
   S2ac <- prop.test(x = c(S2_Click_A,S2_Click_C), n = c(Open_A,Open_C))
    ```
   S2cd <- prop.test(x = c(S2_Click_C,S2_Click_D), n = c(Open_C,Open_D))
   `

   `#section 3 test
    S3ab <- prop.test(x = c(S3_Click_A,S3_Click_B), n = c(Open_A,Open_B))
    S3ac <- prop.test(x = c(S3_Click_A,S3_Click_C), n = c(Open_A,Open_C))
    ```
    S3cd <- prop.test(x = c(S3_Click_C,S3_Click_D), n = c(Open_C,Open_D))`

    `#section 4 test
     S4ab <- prop.test(x = c(S4_Click_A,S4_Click_B), n = c(Open_A,Open_B))
     S4ac <- prop.test(x = c(S4_Click_A,S4_Click_C), n = c(Open_A,Open_C))


     `#section 5 test
      S5ab <- prop.test(x = c(S5_Click_A,S5_Click_B), n = c(Open_A,Open_B))
      S5ac <- prop.test(x = c(S5_Click_A,S5_Click_C), n = c(Open_A,Open_C))

     `#Main body test
     MainBodyab <- prop.test(x = c(MainBody_Click_A,MainBody_Click_B), n = c(Open_A,Open_B))
     MainBodyac <- prop.test(x = c(MainBody_Click_A,MainBody_Click_C), n = c(Open_A,Open_C))

     `

    ```
    `

        `#FINAL P VALUE`

        `S1ab$p.value
        S1ac$p.value
        S1ad$p.value

        `

    I expect:
    1. I want to read the data in the above format. I mean reading the 
       section 1 version A data i.e 1,999 and assigning the same to 
       S1_Click_A=1,999 similarly for others.
    2. a matrix with their clicks and p values in a single row.

dput()
    structure(list(Section = structure(c(2L, 3L, 4L, 5L, 6L, 1L, 7L), .Label = 
    c("Main email body", "Section 1", "Section 2", "Section 3", "Section 4", 
    "Section 5", "Total email"), class = "factor"), Version.A = c(2967L, 4840L, 
    2508L, 2093L, 1117L, 12408L, 13525L), Version.B = c(3353L, 4522L, 2250L, 
    1333L, 925L, 11458L, 12383L), Version.C = c(495L, 285L, 228L, 209L, 186L, 
    282L, 271L), Version.D = c(559L, 266L, 205L, 133L, 154L, 260L, 248L)), class 
    = "data.frame", row.names = c(NA, -7L ))

最终格式的输入

structure(list(Section = structure(c(2L, 3L, 4L, 5L, 6L, 1L, 
7L), .Label = c("Main email body", "Section 1", "Section 2", 
"Section 3", "Section 4", "Section 5", "Total email"), class = "factor"), 
    Version.A = structure(c(3L, 4L, 2L, 1L, 5L, 6L, 7L), .Label = c("0", 
    "1,773", "1,999", "3,450", "588", "7,222", "7,810"), class = "factor"), 
    Version.B = structure(c(2L, 1L, 7L, 3L, 4L, 5L, 6L), .Label = c("1,781", 
    "2,116", "2,255", "573", "7,067", "7,640", "915"), class = "factor"), 
    Version.C = structure(c(3L, 4L, 2L, 1L, 6L, 5L, 7L), .Label = c("0", 
    "1,744", "2,307", "3,416", "7,467", "721", "8,188"), class = "factor"), 
    Version.D = structure(c(3L, 1L, 7L, 2L, 4L, 5L, 6L), .Label = c("1,399", 
    "1,432", "2,568", "235", "6,043", "6,278", "644"), class = "factor"), 
    A.vs..B = c(NA, NA, NA, NA, NA, NA, NA), A.vs..C = c(NA, 
    NA, NA, NA, NA, NA, NA), A.vs..D = c(NA, NA, NA, NA, NA, 
    NA, NA), B.vs..C = c(NA, NA, NA, NA, NA, NA, NA), B.vs..D = c(NA, 
    NA, NA, NA, NA, NA, NA), C.vs..D = c(NA, NA, NA, NA, NA, 
    NA, NA)), class = "data.frame", row.names = c(NA, -7L)) 

2 个答案:

答案 0 :(得分:1)

这里是第一部分的解决方案,其他部分都是相同的原理。

首先生成组合,然后对其进行测试。

df <- structure(list(Section = structure(c(2L, 3L, 4L, 5L, 6L, 1L, 7L), .Label = 
                                           c("Main email body", "Section 1", "Section 2", "Section 3", "Section 4", 
                                             "Section 5", "Total email"), class = "factor"), Version.A = c(2967L, 4840L, 
                                                                                                           2508L, 2093L, 1117L, 12408L, 13525L), Version.B = c(3353L, 4522L, 2250L, 
                                                                                                                                                               1333L, 925L, 11458L, 12383L), Version.C = c(495L, 285L, 228L, 209L, 186L, 
                                                                                                                                                                                                           282L, 271L), Version.D = c(559L, 266L, 205L, 133L, 154L, 260L, 248L)), class 
                = "data.frame", row.names = c(NA, -7L ))

opens <-  data.frame(A = 9073, B = 9150, C = 9215, D = 9153)

s1_comb <- combn(colnames(df)[-1], 2)
open_comb <- combn(colnames(opens), 2)




res <- cbind(df,  
             matrix(NA, 
                    nrow = nrow(df), 
                    ncol = ncol(open_comb), 
                    dimnames = list(1:nrow(df), paste(open_comb[1,], "vs", open_comb[2,]))
             )
       )
for(k in 1:ncol(s1_comb)){
  for(o in 1:(nrow(df)-2)){
    res[o, paste(open_comb[1, k], "vs", open_comb[2, k])] <- prop.test(x = unlist(df[o, s1_comb[,k]]), 
                                                                       n = unlist(opens[open_comb[,k]]))$p.value
  }
}
res

最终输出,根据要求:

> res
          Section Version.A Version.B Version.C Version.D       A vs B        A vs C        A vs D        B vs C        B vs D
1       Section 1      2967      3353       495       559 2.452892e-08  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
2       Section 2      4840      4522       285       266 1.259231e-07  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
3       Section 3      2508      2250       228       205 2.961113e-06  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
4       Section 4      2093      1333       209       133 1.081110e-48  0.000000e+00  0.000000e+00 4.567813e-198 6.505394e-234
5       Section 5      1117       925       186       154 2.756287e-06 7.420214e-161 3.232226e-174 1.051130e-116 4.618885e-129
6 Main email body     12408     11458       282       260           NA            NA            NA            NA            NA
7     Total email     13525     12383       271       248           NA            NA            NA            NA            NA
        C vs D
1 3.472031e-02
2 4.850847e-01
3 3.178608e-01
4 5.557843e-05
5 1.022220e-01
6           NA
7           NA

答案 1 :(得分:0)

您应该查看RMarkdown,它可用于创建完全可重复的报告。

基本上,您编写了一个脚本,该脚本加载您的数据并执行分析并创建输出文档(PDF,HTML)。

RStudio是一款很棒的免费IDE,可用于编写RMarkdown文档。