R/plot_dens.R

Defines functions plot_dens

Documented in plot_dens

#' @title Plot the projection estimator of a density
#'
#' @description Plot the estimated density function (see \code{\link{est_dens}}) for a given set of data using a given orthonormal basis
#' of some dimension. If known, compare the plot to the plot of the actual density function.
#' @import ggplot2
#' @param basis an object of class \code{\link{Basis}} 
#' @param data a numeric vector containing a sample of i.i.d. real random variables with common density.
#' @param D a numeric value, the dimension of the subspace in which the estimation will be calculated.
#' @param pd a numeric value. Another dimension of a subspace in which the estimation can be calculated in.
#'  The default is the dimension given by the model selection algorithm (see \code{\link{perfect_D}}).
#' @param resol a numeric value, specifying the quality of the plot (length.out of processed values). The default is 300.
#' @param dens a function, the true density of the distribution. Since the true density may not be known, the default is NULL. 
#' @param compare logical value. If TRUE the projection estimator (see \code{\link{est_dens}}) using \code{D} is compared to (if default is used) the 
#' projection estimator of the dimension generated by the model selection algorithm (see \code{\link{perfect_D}}) and (if passed) the true density \code{dens}.
#' If FALSE (the default) only the estimated density is plotted.
#' @export
#' @return no return
#' @examples
#' trig_bas <- Trig_Basis$new(1000)
#' data <- rnorm(100, 0.5, 0.088)
#' plot_dens(trig_bas, data, 5)
#' \dontrun{
#' dens1 <- function(x) dnorm(x, 0.5, 0.088)
#' plot_dens(trig_bas, data, 5, dens = dens1, compare = TRUE)
#' }
plot_dens <- function(basis, data, D, pd = perfect_D(basis, data), resol = 300, dens = NULL, compare = FALSE) {
  stopifnot(is.numeric(D))
  stopifnot(D <= basis$.dimension)
  stopifnot(D > 0)
  stopifnot(length(D) == 1)
  stopifnot(any(class(basis) %in% "Basis"))
  stopifnot(is.numeric(data))
  stopifnot(is.logical(compare))
  x <- seq(0, 1, length.out = resol)
  df <- data.frame(x)
  df["est_dens"] <- est_dens(basis, data, D)(x)
  if (is.null(dens) == FALSE) {
    stopifnot(class(dens) == "function")
    df["true_dens"] <- dens(x)
  }
  if (compare == TRUE) {
    df["adap_est"] <- est_dens(basis, data, pd)(x)
  }
  p <- ggplot(df, aes(x = x))
  p <- p + geom_line(aes_string(y = "est_dens", color = shQuote("estimated density")), size = 1)
  p <- p + labs(title = "Estimated Density", y = "y")
  if (is.null(dens) == TRUE && compare == TRUE){
    p <- p + geom_line(aes_string(y = "adap_est", color = shQuote("estimated density using parameter 'pd'")), size = 1)
    p <- p + scale_color_discrete(name = "Densities", l = 70, c = 150)
  }
  else if (is.null(dens) == FALSE && compare == TRUE) {
    p <- p + geom_line(aes_string(y = "true_dens", color = shQuote("true_density")), size = 1)
    p <- p + geom_line(aes_string(y = "adap_est", color = shQuote("estimated density using parameter 'pd'")), size = 1)
    p <- p + scale_color_discrete(name = "Densities", l = 70, c = 150)
  }
  else {
    p <- p + theme(legend.position = "none") + scale_color_discrete(l = 0)
  }
  plot(p)
}
nschaefer1211/OSE documentation built on Dec. 31, 2020, 12:59 a.m.