Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #functions used in ExactProb
  2. #Minimalist chi square, default equal probability for each bin
  3. chiStat <- function(v,p=rep(1/length(v),length(v))){sum(((v - sum(v)*p)^2)/(sum(v)*p))}
  4. #multinomial prob based on set of probabilities, defaults to equal probabilities
  5. exactMult <- function(v,p=rep(1/length(v),length(v))){
  6. n <- factorial(sum(v))
  7. d <- prod(factorial(v))
  8. p <- prod(p^v)
  9. return( (n/d)*p )
  10. }
  11.  
  12. #This generates all the permutations given n number of balls in m bins and
  13. #then calculates the exact probability according to the multinomial
  14. #distribution and the CDF of the chi-square statistic
  15. exactProb <- function(n,m,p=rep(1/m,m)){
  16. library(partitions)
  17. AllDat <- t(compositions(n,m))
  18. ExactProb <- apply(AllDat,1,exactMult,p=p)
  19. chiStat <- function(v,p){sum(((v - sum(v)*p)^2)/(sum(v)*p))}
  20. Chi <- apply(AllDat,1,chiStat,p=p)
  21. #order according to chi-stat
  22. MyData <- data.frame(as.matrix(AllDat),ExactProb, Chi)[order(Chi),]
  23. MyData$cumprob <- cumsum(MyData$ExactProb)
  24. return(MyData)
  25. }
  26.  
  27. #My wrapping all up in a global function to return items in list
  28. #given the initial data
  29. SmallSampChi <- function(d,p=rep(1/length(d),length(d))){
  30. n <- sum(d)
  31. m <- length(d)
  32. cdf <- exactProb(n=n,m=m,p=p) #generate exact probability
  33. chiSamp <- chiStat(d,p) #Chi stat for sample
  34. #p-value to the right of the test statistic
  35. pvalue <- sum(cdf[cdf[,'Chi'] >= chiSamp,'ExactProb'])
  36. #return object
  37. t <- list(cdf,p,d,chiSamp,pvalue)
  38. names(t) <- c("CDF","probabilities","data","Chi-Square Statistic","p-value")
  39. return(t)
  40. }
  41.  
  42. #now with an example dataset, three events all on the third day
  43. d <- c(0,0,3,0,0) #format N observations in M bins, 3 in third bin
  44. p <- c(0.03,0.12,0.6,0.2,0.05) #arbitrary PMF in comments
  45. t <- SmallSampChi(d=d,p=p)
  46.  
  47. plot(t$CDF$Chi,t$CDF$cumprob,type='s',xlab='Chi-Square value',ylab='Exact CDF')
  48. abline(v=t$'Chi-Square Statistic',col='#FF000099')
  49.  
  50. a <- .05
  51. t$CDF[t$CDF[,'cumprob'] > (1-a),1:5]
  52. # X1 X2 X3 X4 X5
  53. #6 1 1 1 0 0
  54. #20 0 0 0 3 0
  55. #12 1 1 0 1 0
  56. #17 1 0 0 2 0
  57. #23 0 2 0 0 1
  58. #24 1 0 1 0 1
  59. #27 1 0 0 1 1
  60. #22 1 1 0 0 1
  61. #3 1 2 0 0 0
  62. #4 0 3 0 0 0
  63. #33 0 0 1 0 2
  64. #34 0 0 0 1 2
  65. #32 0 1 0 0 2
  66. #31 1 0 0 0 2
  67. #5 2 0 1 0 0
  68. #11 2 0 0 1 0
  69. #2 2 1 0 0 0
  70. #21 2 0 0 0 1
  71. #35 0 0 0 0 3
  72. #1 3 0 0 0 0
  73.  
  74. p_alt <- rep(1/5,5)
  75. t$CDF$AltProb <- apply(as.matrix(t$CDF[,1:length(p_alt)]),1,exactMult,p=p_alt)
  76. sum(t$CDF[t$CDF[,'cumprob'] > (1-a),'AltProb']) #power of alt
  77. #[1] 0.536
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement