0

I'm doing some research on truncated distributions, specifically on the Truncated Pareto distribution. This has a known density function and probability function, so one is able to design the quantile function and with that a 'random generate numbers' function.

But now that I have these functions, let's say that dtp(x,lower,upper,alpha) is my density function, how do I plot in fact the density? I know that there exists commands like density() which uses kernel estimation, but one should however be able to plot the density function with the aid of the density function itself and with random numbers following said distribution?

Riley
  • 933
  • 10
  • 26
  • Maybe like `curve(dtp(x, lower, upper, alpha))`. If so, then we can close your question as a dupe against this one: http://stackoverflow.com/q/26091323 – Frank Mar 30 '16 at 17:44
  • @Frank, I'll check it right away! – Riley Mar 30 '16 at 17:52
  • 1
    Good find on the dupe. Nothing special about plotting *density* functions. – Gregor Thomas Mar 30 '16 at 17:54
  • Sorry guys for the inconvenience. Do I delete this question or how should I handle this? – Riley Mar 30 '16 at 17:55
  • 1
    Nah, it's fine keeping up duplicate questions. Someone might find yours via google, and now that the two questions are linked, they can see the answers in both places. – Frank Mar 30 '16 at 17:56

1 Answers1

2

The standard way to plot is to have x values and y values, and then plot them. You have a function that converts x values to y values, which means that all you need to do is pick x values to plot and give them to your function, something like:

x = seq(0, 10, length.out = 100)
y = dtp(x = x)
plot(x, y, type = "l")

Note that I have no idea whether this is a reasonable domain for your density, if you have suitable default values for lower, upper, alpha or if you need to specify them, etc.

Alternatively, some functions like curve for base plot just take a function and a domain, you can pass through additional arguments.

curve(dtp, from = 0, to = 10, n = 101)
curve(dtp, from = 0, to = 10, n = 101, alpha = 0.2) # specifying alpha

If you prefer ggplot, then stat_function is the function for you.

library(ggplot2)
ggplot(data.frame(x = c(0, 10), aes(x = x)) +
  stat_function(fun = dtp)

ggplot(data.frame(x = c(0, 10), aes(x = x)) +
  stat_function(fun = dtp, args = list(alpha = 0.2))
  # passing alpha to dtp via args
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294