Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Create function to calculate expected value
- EXP <- function(n) { if(n%%1 != 0) { stop("Error: Input is not a positive integer") }
- else if(n < 1) { stop("Error: Input is not a positive integer") }
- else if(n == 1) { 0 }
- else { log(n) + sum(choose(n,2:n)*(-1)^(2:n)*log(2:n)) } };
- #Plot expected value function
- N <- 20;
- NNN <- (1:N);
- EEE <- rep(0, N); for(n in 1:N) { EEE[n] <- EXP(n) };
- DATA <- data.frame(n = NNN, Expectation = EEE);
- SPLINE <- as.data.frame(spline(NNN, EEE));
- library(ggplot2);
- ggplot(data = DATA, aes(x = n, y = Expectation)) +
- geom_point(colour = "DarkBlue") +
- geom_line(data = SPLINE, aes(x = x, y = y), colour = "Blue") +
- scale_x_continuous(name = "Number of Data Points", labels = (1:N), breaks = (1:N)) +
- ggtitle("Expected value of log-ratio of maximum-to-minimum") +
- labs(subtitle = "(Data from an exponential distribution)") +
- xlab("Number of Data Points") + ylab("Expected Value");
Add Comment
Please, Sign In to add comment