Dear R list, I am having a little trouble understanding the R code. I want to compute expectation of normal pdf. I did the following: integrate(x*dnorm(x, rate=1), -Inf, Inf) Error in match.fun(f) : object 'x' not found If I did this, I get, integrate(dexp(x, rate=1), -Inf, Inf) Error in dexp(x, rate = 1) : object 'x' not found How should I fix this? I remember when I did it for curve(), it was fine. curve(pexp(x, rate = 1/2), from = 0, to = 5) What am I not getting here? Thank you so much!
Error in match.fun(f) : object 'x' not found
3 messages · C W, Uwe Ligges, Peter Dalgaard
On 21.02.2017 18:29, C W wrote:
Dear R list, I am having a little trouble understanding the R code. I want to compute expectation of normal pdf. I did the following: integrate(x*dnorm(x, rate=1), -Inf, Inf)
integrate needs a function as first argument, hence: integrate(function(x) x*dnorm(x, rate=1), -Inf, Inf) and then you have the next error that is more obvious to fix...
Error in match.fun(f) : object 'x' not found If I did this, I get, integrate(dexp(x, rate=1), -Inf, Inf) Error in dexp(x, rate = 1) : object 'x' not found
same here.
How should I fix this? I remember when I did it for curve(), it was fine. curve(pexp(x, rate = 1/2), from = 0, to = 5)
curve() is an exception in that it can use an unevaluated function call. Best, Uwe Ligges
What am I not getting here? Thank you so much! [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
The curve() function is being (overly?) clever in allowing nonstandard evaluation to let you specify an expression for the function argument. For integrate(), you need to go the standard way and set up an actual function of 1 argument and pass that. It's not all that hard:
f <- function(x) x*dnorm(x, mean=1.234) integrate(f, -Inf, Inf)
1.234 with absolute error < 3.1e-07 (In fact, reading the examples on the help page for integrate() might have gotten you there faster than writing for help...) Notice, incidentally, that curve() also works with functions:
curve(f, -2, 4)
-pd
On 21 Feb 2017, at 18:29 , C W <tmrsg11 at gmail.com> wrote:
Dear R list, I am having a little trouble understanding the R code. I want to compute expectation of normal pdf. I did the following: integrate(x*dnorm(x, rate=1), -Inf, Inf) Error in match.fun(f) : object 'x' not found If I did this, I get, integrate(dexp(x, rate=1), -Inf, Inf) Error in dexp(x, rate = 1) : object 'x' not found How should I fix this? I remember when I did it for curve(), it was fine. curve(pexp(x, rate = 1/2), from = 0, to = 5) What am I not getting here? Thank you so much! [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com