Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #Some libraries that will help
  2. library(nlme)
  3. library(dplyr)
  4. library(tidyr)
  5. library(mvtnorm)
  6.  
  7. ######This controls it all!
  8. n_sims <- 5
  9.  
  10. #A function to make a sigma matrix
  11. #but, assumes a standard normal - sd of 1,
  12. #and thus correlation = covriance. You can monkey here
  13. make_sigma <- function(cor_effect, len){
  14. ret <- matrix(rep(cor_effect, len^2), ncol=len)
  15. diag(ret) <- 1
  16.  
  17. return(ret)
  18. }
  19.  
  20. #OK, start by making a data frame with effect sizes and correlation in tanks
  21. #then use that to add data
  22. test_df <- expand.grid(effect_size = rep(1:5, n_sims),
  23. cor_tank = seq(0,0.3, length.out=4)) %>%
  24. mutate(sim = 1:n()) %>%
  25. #add tanks
  26. crossing(tank = 1:4) %>%
  27. #add subsample
  28. crossing(subsample = 1:10) %>%
  29. #check that we're ok
  30. # group_by(sim, tank, effect_size, cor_tank) %>%
  31. # summarize(len = n())
  32.  
  33. #add data
  34. group_by(sim, tank) %>%
  35. mutate(sim_obs = rmvnorm(1, mean = effect_size*tank,
  36. sigma=make_sigma(cor_tank[1], 10))) %>%
  37.  
  38. ungroup()
  39.  
  40. #Let's just run one analysis
  41. one_test_df <- test_df %>% filter(sim==8) #schoose a number with a nonzero cor_tank
  42.  
  43.  
  44. one_lme <- lme(sim_obs ~ tank, random = ~1|factor(tank), data=one_test_df)
  45. summary(one_lme)
  46.  
  47. broom::tidy(one_lme)
  48. broom::tidy(one_lme, "fixed")
  49.  
  50.  
  51. #let's run a lot of models
  52. fit_df <- test_df %>%
  53. group_by(sim, effect_size, cor_tank) %>%
  54. nest() %>%
  55. mutate(fit_mod = purrr::map(data, ~lme(sim_obs ~ tank, random = ~1|factor(tank), data=.))) %>%
  56. mutate(coef = purrr:::map(fit_mod, ~broom::tidy(., "fixed"))) %>%
  57. unnest(coef) %>%
  58. filter(term != "(Intercept)")
  59.  
  60. #let's see what this looks like
  61.  
  62. library(ggplot2)
  63. ggplot(data=fit_df, mapping = aes(x=cor_tank, y=p.value, color=factor(effect_size))) +
  64. geom_point(position = position_dodge(width=0.1), size=2, alpha=0.5)
  65.  
  66.  
  67. ggplot(data=fit_df, mapping = aes(x=cor_tank, y=p.value, color=factor(effect_size))) +
  68. geom_jitter( size=2, alpha=0.5) +
  69. facet_wrap(~effect_size) +
  70. scale_y_log10() +
  71. geom_hline(yintercept=0.05, lty=2) #show an alpha
  72.  
  73.  
  74. #### OK - power time for different alphas!
  75. alphas = seq(0.01, 0.1, length.out=10)
  76.  
  77. pow_df <- fit_df %>%
  78. crossing(alpha = alphas) %>%
  79. group_by(alpha, effect_size, cor_tank) %>%
  80. summarize(type_2_error_rate = sum(p.value>alpha)/n()) %>%
  81. ungroup() %>%
  82. mutate(power = 1-type_2_error_rate)
  83.  
  84.  
  85. #visualize power
  86. ggplot(data = pow_df, mapping=aes(x=cor_tank, y=power, color=factor(alpha))) +
  87. geom_line() + geom_point() +
  88. facet_wrap(~effect_size)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement