Advertisement
Guest User

logUniform.R

a guest
Jan 15th, 2016
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.81 KB | None | 0 0
  1. library(pipeR)
  2. library(ggplot2)
  3.  
  4. dens <- function(x) {
  5.   sapply(x, function(x)(
  6.     if (x >= 1 && x <= exp(1)) {
  7.         1 / x
  8.     } else {
  9.         0
  10.     }
  11.   ))
  12. }
  13.  
  14. dens2 <- function(x) {ifelse(0 <= x & x <= 1, 1, 0)}
  15.  
  16. stepSize <- 0.00001
  17.  
  18. domainData <- data.table(x = seq.int(from=-stepSize, to = 3, by = stepSize))
  19.  
  20. domainData <-
  21. domainData[, Y := dens(x)
  22.           ][, X := dens2(x)
  23.           ][, Y_CDF := cumsum(Y * stepSize)
  24.           ][, X_CDF := cumsum(X * stepSize)] %>>%
  25. melt.data.table(id.vars = c('x')) %>>%
  26. `[`(variable %in% c('Y', 'X'), type := 'density') %>>%
  27. `[`(variable %in% c('Y_CDF', 'X_CDF'), type := 'cdf')
  28.  
  29. ggplot(data = domainData, aes(x=x, color=variable, y=value)) +
  30. geom_line() +
  31. facet_grid(type~.) +
  32. theme_bw()
  33.  
  34. ggsave("~/loguniform.png", units = "in", width=4, height=6)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement