Guest User

Untitled

a guest
Jul 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. #Create function to calculate expected value
  2.  
  3. EXP <- function(n) { if(n%%1 != 0) { stop("Error: Input is not a positive integer") }
  4. else if(n < 1) { stop("Error: Input is not a positive integer") }
  5. else if(n == 1) { 0 }
  6. else { log(n) + sum(choose(n,2:n)*(-1)^(2:n)*log(2:n)) } };
  7.  
  8.  
  9. #Plot expected value function
  10.  
  11. N <- 20;
  12. NNN <- (1:N);
  13. EEE <- rep(0, N); for(n in 1:N) { EEE[n] <- EXP(n) };
  14. DATA <- data.frame(n = NNN, Expectation = EEE);
  15. SPLINE <- as.data.frame(spline(NNN, EEE));
  16.  
  17. library(ggplot2);
  18. ggplot(data = DATA, aes(x = n, y = Expectation)) +
  19. geom_point(colour = "DarkBlue") +
  20. geom_line(data = SPLINE, aes(x = x, y = y), colour = "Blue") +
  21. scale_x_continuous(name = "Number of Data Points", labels = (1:N), breaks = (1:N)) +
  22. ggtitle("Expected value of log-ratio of maximum-to-minimum") +
  23. labs(subtitle = "(Data from an exponential distribution)") +
  24. xlab("Number of Data Points") + ylab("Expected Value");
Add Comment
Please, Sign In to add comment