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. I use the pmap
function for this. 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?
CodePudding user response:
The problem is that in your lambda function, you are not using the argument names for 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
. Just name the arguments explicitly as you did in your first example, and your code will work.
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)