Dear all, I have a large vector of durations (in seconds) and I create an histogram as follows: hist(durations,breaks=500,xlim=c(0,2000),main="",xlab="Duration (Seconds)",ylab="Frequency (%)",prob=TRUE) Next, I would like to superimpose the exponential distribution with the maximum likelihood estimate of lambda. To do so, I first calculate get the estimate of lambda and then try to add the curve in the following way (with error): library(MASS) lambda=fitdistr(durations,"exponential")$estimate curve(rexp(10000,rate=lambda),add=TRUE) Error in curve(rexp(10000,rate=lambda),add=TRUE) : 'expr' must be a function, or a call or an expression containing 'x'. So I need to have an 'x', OK. But doing this does not work either: curve(dexp(durations,rate=lambda),add=TRUE) What am I doing wrong? -- View this message in context: http://r.789695.n4.nabble.com/Superimpose-exponential-density-function-to-histogram-tp4666468.html Sent from the R help mailing list archive at Nabble.com.
Rui Barradas
2013-May-07 14:37 UTC
[R] Superimpose exponential density function to histogram
Hello, Try curve(dexp(x, rate=lambda), add=TRUE) Hope this helps, Rui Barradas Em 07-05-2013 14:18, Manta escreveu:> Dear all, > > I have a large vector of durations (in seconds) and I create an histogram as > follows: > > hist(durations,breaks=500,xlim=c(0,2000),main="",xlab="Duration > (Seconds)",ylab="Frequency (%)",prob=TRUE) > > Next, I would like to superimpose the exponential distribution with the > maximum likelihood estimate of lambda. To do so, I first calculate get the > estimate of lambda and then try to add the curve in the following way (with > error): > > library(MASS) > lambda=fitdistr(durations,"exponential")$estimate > curve(rexp(10000,rate=lambda),add=TRUE) > > Error in curve(rexp(10000,rate=lambda),add=TRUE) : 'expr' must be a > function, or a call or an expression containing 'x'. > > So I need to have an 'x', OK. But doing this does not work either: > curve(dexp(durations,rate=lambda),add=TRUE) > > What am I doing wrong? > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Superimpose-exponential-density-function-to-histogram-tp4666468.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >