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?
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