Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. # Function support for the math final
  2.  
  3. ll <- c('dplyr','magrittr','tidyr','ggplot2','cowplot','ggrepel','GGally','broom','forcats',
  4. 'stringr','reshape2','gridExtra','grid','latex2exp','MASS')
  5. sapply(ll,function(l) require(l,character.only = T))
  6.  
  7. # Clean up
  8. rm(list=ls())
  9. dir <- 'C:/Users/erikinwest/Documents/Courses/MATH_896/final/'
  10. setwd(dir)
  11. source('C:/Users/erikinwest/Documents/R/funzies.R')
  12.  
  13. options(max.print = 75)
  14.  
  15. ################################################
  16. library(gtools)
  17.  
  18.  
  19. # Set the theta/n we care about
  20. theta <- 1
  21. n <- 10
  22.  
  23. # Make a function that will sample from our weird distribution
  24. rtheta <- function(theta,n) {
  25. # Sample from 0 to 3 theta
  26. samp <- runif(n,0,4*theta)
  27. # Find the "good" index
  28. good.idx <- (samp<=theta & samp>=0) | (samp>=2*theta & samp <=3*theta)
  29. good.samp <- samp[good.idx]
  30. # With probability 10% take one of the bad guys
  31. if( 0.9 < runif(1) ) {
  32. good.samp <- c(good.samp,samp[!good.idx][1])
  33. # print ('bad!')
  34. } else { }
  35. # Return
  36. return(good.samp)
  37. }
  38.  
  39. # Now we get our three statistics
  40. tX <- function(x) {
  41. x <- sort(x)
  42. # Get maximum
  43. m <- max(x)
  44. # Convert data to distance from max/2
  45. x2m <- x - m/2
  46. # If only positive/negative then return the first as such
  47. if (all(x2m>0)) {
  48. fl <- x[length(x)]
  49. fr <- x[1]
  50. } else if (all(x2m<0)) {
  51. fl <- x[length(x)]
  52. fr <- x[1]
  53. } else {
  54. # Find the first point to the left
  55. idx.fl <- which(x2m<0)
  56. idx.fl <- idx.fl[length(idx.fl)]
  57. fl <- x[idx.fl]
  58. fr <- x[idx.fl+1]
  59. }
  60. # Return
  61. return(c(m,fl,fr))
  62. }
  63.  
  64. # Now do the check
  65. check.stat <- function(t,theta) {
  66. # case 1: the max is less than theta
  67. c1 <- (findInterval(t[1],c(0,theta),rightmost.closed = T,left.open = T)==1)*1
  68. # Case 2: fl and fr are between 2theta and 3theta (max < 3*theta)
  69. c2 <- (findInterval(t[2],c(2*theta,3*theta),rightmost.closed = T,left.open = T)==1) *
  70. (findInterval(t[3],c(2*theta,3*theta),rightmost.closed = T,left.open = T)==1) *
  71. (t[1]<=3*theta)
  72. # Case 3: fl between 0,theta, fr between 2theta,3theta
  73. c3 <- (findInterval(t[2],c(0,theta),rightmost.closed = T,left.open = T)==1) *
  74. (findInterval(t[3],c(2*theta,3*theta),rightmost.closed = T,left.open = T)==1)*
  75. (t[1]<=3*theta)
  76. # Check if any conditions are met
  77. check <- any(c(c1,c2,c3)==1)
  78. return(check)
  79. }
  80.  
  81. # Now we do some simulations
  82. nsim <- 1000
  83. sim.df <- data.frame(matrix(NA,ncol=2,nrow=nsim)) %>% set_colnames(c('lv','check')) %>% tbl_df
  84. dstore <- list()
  85. set.seed(nsim)
  86. for (k in 1:nsim) {
  87. l <- 1
  88. while(l < 2) {
  89. # Generate some data (at least length 2)
  90. data <- rtheta(theta=1,n=10)
  91. l <- length(data)
  92. }
  93. # Get the statistics
  94. t <- data %>% tX
  95. # Check the data
  96. accept <- t %>% check.stat(theta=1)
  97. # Store
  98. sim.df[k,] <- c(data[length(data)],accept)
  99. names(data) <- paste('v',1:length(data),sep='')
  100. dstore[[k]] <- data
  101. }
  102.  
  103. # -------- CHECK NOTHING SLIPPED THROUGH! ---------- #
  104.  
  105. d.df <- do.call('smartbind',dstore) %>% tbl_df %>% mutate(rid=1:nrow(.))
  106. # Are all obs below 0 and above 3*theta spotted as bad?
  107. sim.df %>% filter(lv<0 | lv>3*theta) %>% use_series(check) %>% table
  108. # Are all obs between theta,2*theta spotted as base
  109. sim.df %>% mutate(rid=1:nrow(.)) %>% filter(lv>theta & lv<2*theta) %>% use_series(check) %>% table
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement