简体   繁体   English

如何为 R 中的功率分析生成具有不同样本大小的负二项分布?

[英]How to generate a negative binomial distribution with different sample sizes for power analyses in R?

I try to do a power simulation with an outcome variable that is zero-inflated.我尝试使用零膨胀的结果变量进行功率模拟。 So I use a negative binomial distribution.所以我使用负二项分布。 What I need is the following distribution:我需要的是以下分布:

library(tidyverse)
set.seed(123)
rt_random <- 
rnbinom(n = 2000, mu = 25, size = .9) 
qplot(rt_random)

期望分布

Now, I tried to create a function where I can use different sample sizes for the power simulation.现在,我尝试创建一个 function,我可以在其中使用不同的样本大小进行功率模拟。 I use the pmap function for this.为此,我使用pmap function。 However this does not seem to work.但是,这似乎不起作用。 The final distribution is not zero-inflated at all and the mean is not close to the defined mean:最终分布根本不是零膨胀的,均值不接近定义的均值:

generate_distribution <- function(n, mus, sizes){
  tibble(n = n, 
       t_mu = mus, 
       t_size = sizes)%>% 
  mutate(rt = pmap(list(n, t_mu, t_size),
                     ~rnbinom(..1, ..2, ..3))) %>% 
  unnest(rt) 
  
}
set.seed(123)
rt_df <- 
  generate_distribution(n = 2000,
                        mus = 25,
                        sizes = .9)
qplot(rt_df$rt)

错误分配

Is there an easy way to change my code so that I get the desired distribution?有没有一种简单的方法来更改我的代码以获得所需的分布?

The problem is that in your lambda function, you are not using the argument names for rnbinom .问题是在您的 lambda function 中,您没有使用rnbinom的参数名称。 The default order for the arguments if you don't name them is n, size, prob, mu , so you are passing 2000 to n , 25 to size and 0.9 to prob . arguments 的默认顺序是n, size, prob, mu ,因此您将 2000 传递给n ,将 25 传递给size ,将 0.9 传递给prob Just name the arguments explicitly as you did in your first example, and your code will work.只需像在第一个示例中那样明确命名 arguments,您的代码就可以运行。

generate_distribution <- function(n, mus, sizes){
  tibble(n = n, 
       t_mu = mus, 
       t_size = sizes)%>% 
  mutate(rt = pmap(list(n, t_mu, t_size),
                     ~rnbinom(n = ..1, mu = ..2, size = ..3))) %>% 
  unnest(rt) 
}

set.seed(123)
rt_df <- 
  generate_distribution(n = 2000,
                        mus = 25,
                        sizes = .9)
qplot(rt_df$rt) 

在此处输入图像描述

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

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