Re: [R] how to include integrate in a function that can be solved with uniroot?
Thanks Michael, Op 11/14/2011 3:30 PM, R. Michael Weylandt schreef: You need to explicitly pass th to your function with the ... argument of integrate. That was a point I was missing! Thanks again, This solved my problems for this time. Gerrit. E<- function(th){ integrate(function(x,th) x*g(x, th), 0, Inf, th)$value } Also, it's value, not Value, which might be producing errors of another sort. Michael On Mon, Nov 14, 2011 at 9:16 AM, Gerrit Draisma wrote: Thanks Michael, I see now how to include integrate function in the EV function. And apologies: I now realize that my code was sloppy. I intended to write E<- function(th) { + integrate( f = function(x,th){x*g(x,th)}, + 0,Inf)$Value} E(1/10) But that does not work either, Gerrit. Op 11/14/2011 2:50 PM, R. Michael Weylandt schreef: Try this: EV<- function(lamb){ fnc<- function(x) x * dexp(x, lamb) integrate(fnc, 0, Inf)$value } Your problem is that there's nothing to translate th to lambda in your code for E. Michael On Mon, Nov 14, 2011 at 5:32 AM, Gerrit Draisma wrote: Hallo, I am trying to define expectation as an integral and use uniroot to find the distribution parameter for a given expectation. However I fail to understand how to define properly the functions involved and pass the parameters correctly. Can anyone help me out? Thanks, Gerrit Draisma. This what I tried: === # exponential density g<- function(x,lambda){ lambda *exp(-lambda*x) } # expectation with lambda=1/10 integrate(f = function(x,lambda=1/10) {x*g(x,lambda)}, 0,Inf) 10 with absolute error<6.7e-05 # *how to write this as a function?* E<- function(lambda) { + integrate( f = function(x,th){x*g(x,lambda)}, + 0,Inf)$Value} E(1/10) NULL # *how to include this function in uniroot to find lambda* # *for a given expectation?* mu<- 10 uniroot(f<-function(th){E(th)-mu},lower=1,upper=100) Error in if (is.na(f.lower)) stop("f.lower = f(lower) is NA") : argument is of length zero __ R-help@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. __ R-help@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.
Re: [R] how to include integrate in a function that can be solved with uniroot?
You need to explicitly pass th to your function with the ... argument of integrate. E <- function(th){ integrate(function(x,th) x*g(x, th), 0, Inf, th)$value } Also, it's value, not Value, which might be producing errors of another sort. Michael On Mon, Nov 14, 2011 at 9:16 AM, Gerrit Draisma wrote: > Thanks Michael, > > I see now how to include integrate function in the EV function. > And apologies: > I now realize that my code was sloppy. > I intended to write >> E<- function(th) { >> + integrate( f = function(x,th){x*g(x,th)}, >> + 0,Inf)$Value} >> E(1/10) > But that does not work either, > > Gerrit. > > Op 11/14/2011 2:50 PM, R. Michael Weylandt schreef: >> >> Try this: >> >> EV<- function(lamb){ >> fnc<- function(x) x * dexp(x, lamb) >> integrate(fnc, 0, Inf)$value >> } >> >> Your problem is that there's nothing to translate th to lambda in your >> code for E. >> >> Michael >> >> On Mon, Nov 14, 2011 at 5:32 AM, Gerrit Draisma >> wrote: >>> >>> Hallo, >>> I am trying to define expectation as an integral >>> and use uniroot to find the distribution parameter >>> for a given expectation. >>> >>> However I fail to understand how to define properly >>> the functions involved and pass the parameters correctly. >>> >>> Can anyone help me out? >>> >>> Thanks, >>> Gerrit Draisma. >>> >>> >>> This what I tried: >>> === # exponential density g<- function(x,lambda){ lambda *exp(-lambda*x) } # expectation with lambda=1/10 integrate(f = function(x,lambda=1/10) {x*g(x,lambda)}, 0,Inf) >>> >>> 10 with absolute error< 6.7e-05 # *how to write this as a function?* E<- function(lambda) { >>> >>> + integrate( f = function(x,th){x*g(x,lambda)}, >>> + 0,Inf)$Value} E(1/10) >>> >>> NULL # *how to include this function in uniroot to find lambda* # *for a given expectation?* mu<- 10 uniroot(f<-function(th){E(th)-mu},lower=1,upper=100) >>> >>> Error in if (is.na(f.lower)) stop("f.lower = f(lower) is NA") : >>> argument is of length zero >>> >>> >>> __ >>> R-help@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. >>> >> > __ R-help@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.
Re: [R] how to include integrate in a function that can be solved with uniroot?
Try this: EV <- function(lamb){ fnc <- function(x) x * dexp(x, lamb) integrate(fnc, 0, Inf)$value } Your problem is that there's nothing to translate th to lambda in your code for E. Michael On Mon, Nov 14, 2011 at 5:32 AM, Gerrit Draisma wrote: > Hallo, > I am trying to define expectation as an integral > and use uniroot to find the distribution parameter > for a given expectation. > > However I fail to understand how to define properly > the functions involved and pass the parameters correctly. > > Can anyone help me out? > > Thanks, > Gerrit Draisma. > > > This what I tried: > === >> # exponential density >> g <- function(x,lambda){ lambda *exp(-lambda*x) } >> >> # expectation with lambda=1/10 >> integrate(f = function(x,lambda=1/10) {x*g(x,lambda)}, 0,Inf) > 10 with absolute error < 6.7e-05 >> >> # *how to write this as a function?* >> E <- function(lambda) { > + integrate( f = function(x,th){x*g(x,lambda)}, > + 0,Inf)$Value} >> E(1/10) > NULL >> >> # *how to include this function in uniroot to find lambda* >> # *for a given expectation?* >> mu <- 10 >> uniroot(f<-function(th){E(th)-mu},lower=1,upper=100) > Error in if (is.na(f.lower)) stop("f.lower = f(lower) is NA") : > argument is of length zero >> > > > __ > R-help@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. > __ R-help@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.
[R] how to include integrate in a function that can be solved with uniroot?
Hallo, I am trying to define expectation as an integral and use uniroot to find the distribution parameter for a given expectation. However I fail to understand how to define properly the functions involved and pass the parameters correctly. Can anyone help me out? Thanks, Gerrit Draisma. This what I tried: === > # exponential density > g <- function(x,lambda){ lambda *exp(-lambda*x) } > > # expectation with lambda=1/10 > integrate(f = function(x,lambda=1/10) {x*g(x,lambda)}, 0,Inf) 10 with absolute error < 6.7e-05 > > # *how to write this as a function?* > E <- function(lambda) { + integrate( f = function(x,th){x*g(x,lambda)}, + 0,Inf)$Value} > E(1/10) NULL > > # *how to include this function in uniroot to find lambda* > # *for a given expectation?* > mu <- 10 > uniroot(f<-function(th){E(th)-mu},lower=1,upper=100) Error in if (is.na(f.lower)) stop("f.lower = f(lower) is NA") : argument is of length zero > __ R-help@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.