0

Considering the following random data:

set.seed(123456)
# generate random normal data
x <- rnorm(100, mean = 20, sd = 5)
weights <- 1:100
df1 <- data.frame(x, weights)

#
library(ggplot2)
ggplot(df1, aes(x)) + stat_ecdf()

We can create a general cumulative distribution plot.

But, I want to compare my curve to that from data used 20 years ago. From the paper, I only know that the data is "best modeled by a shifted exponential distribution with an x intercept of 1.1 and a mean of 18"

How can I add such a function to my plot?

+ stat_function(fun=dexp, geom = "line", size=2, col="red", args = (mean=18.1))

but I am not sure how to deal with the shift (x intercept)

lukeg
  • 1,327
  • 3
  • 10
  • 27

2 Answers2

0

I am not entirely sure if I understand concept of mean for exponential function. However, generally, when you pass function as an argument, which is fun=dexp in your case, you can pass your own, modified functions in form of: fun = function(x) dexp(x)+1.1, for example. Maybe experimenting with this feature will get you to the solution.

statespace
  • 1,644
  • 17
  • 25
0

I think scenarios like this are best handled by making your function first outside of the ggplot call.

dexp doesn't take a parameter mean but uses rate instead which is the same as lambda. That means you want rate = 1/18.1 based on properties of exponential distributions. Also, I don't think dexp makes much sense here since it shows the density and I think you really want the probability with is pexp.

your code could look something like this:

library(ggplot2)
test <- function(x) {pexp(x, rate = 1/18.1)}
ggplot(df1, aes(x)) + stat_ecdf() +
    stat_function(fun=test, size=2, col="red")

enter image description here

you could shift your pexp distributions doing this:

test <- function(x) {pexp(x-10, rate = 1/18.1)}
ggplot(df1, aes(x)) + stat_ecdf() +
    stat_function(fun=test, size=2, col="red") +
    xlim(10,45)

enter image description here

just for fun this is what using dexp produces:

enter image description here

Nate
  • 10,361
  • 3
  • 33
  • 40