Используйте purrr с prop.test для нескольких строк (и столбцов) - PullRequest
0 голосов
/ 13 ноября 2018

Аналогично этому вопросу, но не совсем так.

 result test1 test2 test3 test4
  a    13    12    12     8
  b     5     9    10     8
  c    39    24    30    29

Как использовать purr для строк и столбцов prop.tests, сравнивающих test1 с test2: test4 по результату?

Вручную, наверное, для каждого теста это будет выглядеть так:

результат: a, test1, test2: prop.test(c(13, 12), c(57, 45))

результат: c, test1, test4: prop.test(c(30, 29), c(57, 45))

1 Ответ

0 голосов
/ 13 ноября 2018
# example data
dt = read.table(text = "
result test1 test2 test3 test4
a    13    12    12     8
b     5     9    10     8
c    39    24    30    29
", header=T, stringsAsFactors=F)


# function to get prop.test
# based on a row and two columns
GetTest = function(r, t1, t2) {

  prop.test(unlist(dt[dt$result == r, c(t1, t2)]),
            colSums(dt[,c(t1, t2)]))

}

# apply function
GetTest("a","test1","test2")

# 2-sample test for equality of proportions with continuity correction
# 
# data:  unlist(dt[dt$result == r, c(t1, t2)]) out of colSums(dt[, c(t1, t2)])
# X-squared = 0.047595, df = 1, p-value = 0.8273
# alternative hypothesis: two.sided
# 95 percent confidence interval:
#   -0.2274729  0.1502799
# sample estimates:
# prop 1    prop 2 
# 0.2280702 0.2666667 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...