简体   繁体   English

R.将多项式检验应用于数据帧列表

[英]R. lapply multinomial test to list of dataframes

I have a data frame A , which I split into a list of 100 data frames, each having 3 rows (In my real data each data frame has 500 rows). 我有一个数据帧A ,我将其分成100个数据帧的列表,每个数据帧有3行(在我的真实数据中,每个数据帧有500行)。 Here I show A with 2 elements of the list (row1-row3; row4-row6): 在这里,我显示了A,其中包含列表的2个元素(row1-row3; row4-row6):

A <- data.frame(n = c(0, 1, 2, 0, 1, 2),
                prob = c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1),
                count = c(24878, 33605, 12100 , 25899, 34777, 13765))

# This is the list:
nest <- split(A, rep(1:2, each = 3))

I want to apply the multinomial test to each of these data frames and extract the p-value of each test. 我想对这些数据帧中的每一个应用多项式检验,并提取每个检验的p值。 So far I have done this: 到目前为止,我已经做到了:

library(EMT)

fun <- function(x){
  multinomial.test(x$count,
                   prob=x$prob,
                   useChisq = FALSE, MonteCarlo = TRUE,
                   ntrial = 100, # n of withdrawals accomplished
                   atOnce=100)
}

lapply(nest, fun)

However, I get: 但是,我得到:

 "Error in multinomial.test(x$counts_set, prob = x$norm_genome, useChisq = F,  : 
   Observations have to be stored in a vector, e.g.  'observed <- c(5,2,1)'"

Does anyone have a smarter way of doing this? 有人有更聪明的方式吗?

The results of split are created with names 1 , 2 and so on. 结果split与名称创建12等。 That's why x$count in fun cannot access it. 这就是为什么无法获得x$countfun To make it simpler, you can combine your splitted elements using the list function and then use lapply : 为了更简单,您可以使用list函数组合lapply元素,然后使用lapply

n <- c(0,1,2,0,1,2)
prob <- c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1)
count <- c(24878, 33605, 12100 , 25899, 34777, 13765)
A <- cbind.data.frame(n, prob, count)

nest = split(A,rep(1:2,each=3))

fun <- function(x){
  multinomial.test(x$count,
                   prob=x$prob,
                   useChisq = F, MonteCarlo = TRUE,
                   ntrial = 100, # n of withdrawals accomplished
                   atOnce=100)
}

# Create a list of splitted elements
new_list <- list(nest$`1`, nest$`2`)

lapply(new_list, fun)

A solution with dplyr. 使用dplyr的解决方案。

A = data.frame(n = c(0,1,2,0,1,2),
               prob = c(0.4, 0.5, 0.1, 0.4, 0.5, 0.1),
               count = c(43, 42, 9, 74, 82, 9))

library(dplyr)
nest <- A %>%
  mutate(pattern = rep(1:2,each=3)) %>%
  group_by(pattern) %>%
  dplyr::summarize(mn_pvals = multinomial.test(count, prob)$p.value)
nest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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