Is there a way to compute a p-value for multiple steps in a df without writing a prop.test code multiple times for each step?

I have a table where column A is trials, and the remaining columns are successes on different steps. Row C is a control, and row T is a test.

      A    B    C    D    E    F    G
C 19144 2537 2395 2366 2310 2256 2234
T 19133 2269 2058 2032 2007 1960 1942

I want to perform a prop.test comparing control versus test for each step (С vs T for B; С vs T for C, С vs T for В and so on). 6 steps in total. I want to save p-value for each step in a third row.

Is there a way to compute a p-value for each step without writing a prop.test code 6 times for each step?

If you can write the code to do any one of the 6, that could be straightforwardly adapted to repeat for the others.
If you provide the code, I will show you how. We can also verify that the solution that does all 6, contains the value for the one that you found.

Thank you!
Руку is my code:

control <- c(19144, 2537, 2395, 2366, 2310, 2256, 2234)
test <- c(19133, 2269, 2058, 2032, 2007, 1960, 1942)
df <- data.frame(control,test)
df <- t(df)

df <- as.data.frame(df)

trials <- df[,1]
trials_1 <- cbind(trials, replicate(4,trials))
trials_1





stat1 <- prop.test(x=c(df[,2]), n=c(df[,1]), p = NULL,
                  alternative = c("two.sided"),
                  conf.level = 0.95, correct = FALSE)
stat1$p.value

stat2 <- prop.test(x=c(df[,3]), n=c(df[,1]), p = NULL,
                   alternative = c("two.sided"),
                   conf.level = 0.95, correct = FALSE)
stat2$p.value




stat <- prop.test(x=df[,2:6], n=trials_1, p = NULL,
                  alternative = c("two.sided"),
                  conf.level = 0.95, correct = FALSE)

stat$p.value





so this is the core of what you would do, repeatedly, changing 2 to 3 to 4 to 5 to 6 where it appears in the first df[] clause.
So the approach would be to make that into a function where a variable stands in for that number; and use an iterator, (apply*) family of functions, or (purrr if tidyverse is favoured).

For example

(my_pvalues <- sapply(2:6, function(x_){
  prop.test(x=c(df[,x_]), n=c(df[,1]), p = NULL,
            alternative = c("two.sided"),
            conf.level = 0.95, correct = FALSE)$p.value
}))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.