如何为R数据帧的每一行运行二项式测试?

时间:2017-06-22 06:12:54

标签: r

我正在计算每行的p-value dbinom()R Dataframe

data =

small    Sum
2         7
3         6
5         11

我可以做的每一行:

> binom.test(2, 7, 0.5, alternative=c("two.sided"), conf.level = 0.95)

Exact binomial test

data:  2 and 7
number of successes = 2, number of trials = 7, p-value = 0.4531
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.03669257 0.70957914
   sample estimates:
   probability of success 
         0.2857143

但是,我没有成功将它应用于所有行。

类似的东西:

counts$pVal <- 2*sum(dbinom(0:counts$small, counts$Sum, 0.5))

#or,
counts_2ms04h$pVal <- binom.test(0:counts$small, counts$Sum, 0.5, alternative=c("two.sided"), conf.level = 0.99)


## I also used tapply
test <- function(x, n, p){binom.test(x, n, p, alternative="two-sided")}
mapply(test, counts$small, counts$Sum, 0.5)

Error in binom.test(x, n, p, alternative = "two-sided") : 
  'n' must be a positive integer >= 'x' 

谢谢,

2 个答案:

答案 0 :(得分:1)

怎么样:

bt <- function(a, b, p = 0.5) {binom.test(a, b, 0.5, alternative=
                            c("two.sided"), conf.level = 0.95)$p.value}
counts$pVal <- mapply(bt, counts$small, counts$Sum)

  small Sum     pVal
1     2   7 0.453125
2     3   6 1.000000
3     5  11 1.000000

答案 1 :(得分:0)

直接运行dbinom有什么问题。

resource "mongodbatlas_cluster" "test-cluster" {
  project_id   = mongodbatlas_project.test_db.id
  name         = "test-cluster"
  cluster_type = "REPLICASET"
  replication_specs {
    num_shards = 1
    regions_config {
      region_name     = var.atlas_region
      electable_nodes = 1
      priority        = 7
      read_only_nodes = 0
    }
  }
  provider_backup_enabled      = true
  auto_scaling_disk_gb_enabled = true
  mongo_db_major_version       = "4.2"

  //Provider Settings "block"
  provider_name               = "GCP"
  disk_size_gb                = 10
  provider_instance_size_name = "M10"
}