Trying to use tidy for a power analysis and using clmm2

自闭症网瘾萝莉.ら 提交于 2021-01-29 12:35:49

问题


I'm trying to do a power analysis on a clmm2 analysis that I'm doing. This is the code for the particular statistical model:

test <- clmm2(risk_sensitivity ~ treat + sex + dispersal + 
sex*dispersal + treat*dispersal + treat*sex,random = id, data = datasocial, Hess=TRUE)

Now, I have the following function:

sim_experiment_power <- function(rep) {
  s <- sim_experiment(n_sample = 1000,
                      prop_disp = 0.10,
                      prop_fem = 0.35,
                      disp_probability = 0.75,
                      nondisp_probability = 0.90,
                      fem_probability = 0.75,
                      mal_probability = 0.90)
  broom.mixed::tidy(s) %>%
    mutate(rep = rep)
}
my_power <- map_df(1:10, sim_experiment_power)

The details of the function sim_experiment are not relevant because they are working as expected. The important thing to know is that it spits up a statistical clmm2 result. My objective with the function above is to do a power analysis. However, I get the following error:

Error: No tidy method for objects of class clmm2

I'm a bit new to R, but I guess it means that tidy doesn't work with clmm2. Does anyone know a work-around for this issue?

EDIT: This is what follows the code that I posted above, which is ultimately what I'm trying to get.

You can then plot the distribution of estimates across your simulations.

ggplot(my_power, aes(estimate, color = term)) +
  geom_density() +
  facet_wrap(~term, scales = "free")

You can also just calculate power as the proportion of p-values less than your alpha.

my_power %>%
  group_by(term) %>%
  summarise(power <- mean(p.value < 0.05))

回答1:


For what you need, you can write a function to return the coefficients with the same column name:

library(ordinal)
library(dplyr)
library(purrr)

tidy_output_clmm = function(fit){
  results = as.data.frame(coefficients(summary(fit)))
  colnames(results) = c("estimate","std.error","statistic","p.value")
  results %>% tibble::rownames_to_column("term")
}

Then we apply it using an example where I sample the wine dataset in ordinal:

sim_experiment_power <- function(rep) {
  idx = sample(nrow(wine),replace=TRUE)
  s <- clmm2(rating ~ temp, random=judge, data=wine[idx,], nAGQ=10,Hess=TRUE)
  tidy_output_clmm(s) %>% mutate(rep=rep)
}

my_power <- map_df(1:10, sim_experiment_power)

Plotting works:

ggplot(my_power, aes(estimate, color = term)) +
  geom_density() +
  facet_wrap(~term, scales = "free")

And so does power:

my_power %>% group_by(term) %>% summarise(power = mean(p.value < 0.05))
    # A tibble: 5 x 2
  term     power
  <chr>    <dbl>
1 1|2        0.9
2 2|3        0.1
3 3|4        1  
4 4|5        1  
5 tempwarm   1  


来源:https://stackoverflow.com/questions/60206510/trying-to-use-tidy-for-a-power-analysis-and-using-clmm2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!