Advertisement
Guest User

Untitled

a guest
May 27th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. library(dplyr)
  2. library(reshape2)
  3. library(ggplot2)
  4.  
  5. n <- 500
  6. null <- .75
  7. obs_n <- seq(1,n-1)
  8. binom_probs_low <- pbinom(obs_n, n, null)
  9. binom_probs_upp <- pbinom(obs_n, n, null,lower.tail = FALSE)
  10. binom_probs <- pmin(binom_probs_low, binom_probs_upp)
  11.  
  12. t_probs <- rep(0,n-2)
  13. for (i in 1:(n-1)) {
  14. x = c(rep(1,i),rep(0,n-i))
  15. t_probs[i] <- t.test(x,mu=.75)$p.value
  16. }
  17.  
  18. df <- data.frame(Sample = obs_n, reject = NA, Binomial = binom_probs, t = t_probs)
  19. df <- melt(df,id.vars = c("Sample","reject"),var = "Distribution")
  20. df$reject[df$Distribution =='Binomial'] <- ifelse(df$value[df$Distribution =='Binomial'] < .025 |df$value[df$Distribution =='Binomial'] > .975 , 1,0)
  21. df$reject[df$Distribution =='t'] <- ifelse(df$value[df$Distribution =='t'] < .025, 1,0)
  22. df <- df %>% group_by(Sample) %>%
  23. mutate(decision = if (all(reject==1)) { 1} else if (all(reject==0) ) { 0 } else { 2 })
  24. df$decision <- factor(df$decision)
  25. ggplot(data = df, aes(x=Sample, y = value, color = decision,shape=Distribution)) +
  26. geom_point() +
  27. coord_cartesian(xlim = c(.6*n, .9*n)) +
  28. ggtitle("Binomial vs. t NHST Results")
  29.  
  30. # decision = if (all(reject==1)) { 1} else if (all(reject==0) ) { 0} else { 2}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement