Solve an Equation Including Integral

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Solve an Equation Including Integral

sstoline
Dear All: I submitted this post to the R-sig-Teaching List too


I am trying to solve an equation including an integral for unknown
parameter.


Is this can be done in R? if so, any helps will be highly appreciated



### The following function is a function of t, given exi, nu, and alpha.


fun<-function(exi,nu,alpha)
(nu+t^2)^(-(nu+1)/2)*exp(((nu+1)*exi*t)/((nu+t^2)^0.5))


### I want to solve "[Integral(-Inf,100) fun(exi,nu,alpha)]-(1-alpha)=0"
for t.

### That is, find the value of t which satisfy the equation:

             [Integral(-Inf,100) fun(exi,nu,alpha)]-(1-alpha)=0


### for example given: alpha = 0.05, nu = 20 , exi = 0.5, Solve for t.


with many thanks
steve

--------------------------
Steven M. Stoline
1123 Forest Avenue
Portland, ME 04112
[hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Solve an equation including integral

Ravi Varadhan-2
I think this is what you want:

myroot <- function(t, nu, exi, alpha){
  fun <- function(t, exi, nu) (nu+t^2)^(-(nu+1)/2)*exp(((nu+1)*exi*t)/((nu+t^2)^0.5))
  res <- alpha - integrate(fun, -Inf, t, nu=2, exi=0.5)$value
  return(res)
}

uniroot(myroot, c(-2, 2), nu=2, exi=0.5, alpha=.05)

Hope this is helpful,
Ravi

Ravi Varadhan, Ph.D. (Biostatistics), Ph.D. (Environmental Engg)
Associate Professor,  Department of Oncology
Division of Biostatistics & Bionformatics
Sidney Kimmel Comprehensive Cancer Center
Johns Hopkins University
550 N. Broadway, Suite 1111-E
Baltimore, MD 21205
410-502-2619


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Solve an equation including integral

Ravi Varadhan-2
In reply to this post by sstoline
There was a mistake in the previously sent function.  I had hard coded the values of parameters `nu' and `exi'.

Use this one:

myroot <- function(t, nu, exi, alpha){
  fun <- function(t, exi, nu) (nu+t^2)^(-(nu+1)/2)*exp(((nu+1)*exi*t)/((nu+t^2)^0.5))
  res <- alpha - integrate(fun, -Inf, t, nu=nu, exi=exi)$value
  return(res)
}

uniroot(myroot, c(-2, 2), nu=2, exi=0.5, alpha=.05)

Ravi


From: Ravi Varadhan
Sent: Friday, January 08, 2016 11:29 AM
To: [hidden email]; '[hidden email]' <[hidden email]>
Subject: Re: Solve an equation including integral

I think this is what you want:

myroot <- function(t, nu, exi, alpha){
  fun <- function(t, exi, nu) (nu+t^2)^(-(nu+1)/2)*exp(((nu+1)*exi*t)/((nu+t^2)^0.5))
  res <- alpha - integrate(fun, -Inf, t, nu=2, exi=0.5)$value
  return(res)
}

uniroot(myroot, c(-2, 2), nu=2, exi=0.5, alpha=.05)

Hope this is helpful,
Ravi

Ravi Varadhan, Ph.D. (Biostatistics), Ph.D. (Environmental Engg)
Associate Professor,  Department of Oncology
Division of Biostatistics & Bionformatics
Sidney Kimmel Comprehensive Cancer Center
Johns Hopkins University
550 N. Broadway, Suite 1111-E
Baltimore, MD 21205
410-502-2619


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Solve an equation including integral

Berend Hasselman

> On 8 Jan 2016, at 17:47, Ravi Varadhan <[hidden email]> wrote:
>
> There was a mistake in the previously sent function.  I had hard coded the values of parameters `nu' and `exi'.
>
> Use this one:
>
> myroot <- function(t, nu, exi, alpha){
>  fun <- function(t, exi, nu) (nu+t^2)^(-(nu+1)/2)*exp(((nu+1)*exi*t)/((nu+t^2)^0.5))
>  res <- alpha - integrate(fun, -Inf, t, nu=nu, exi=exi)$value
>  return(res)
> }
>
> uniroot(myroot, c(-2, 2), nu=2, exi=0.5, alpha=.05)
>

Given the original question, shouldn't alpha in the myroot function be replaced by (1-alpha)?
Like so

myroot <- function(t, nu, exi, alpha){
 fun <- function(t, exi, nu) (nu+t^2)^(-(nu+1)/2)*exp(((nu+1)*exi*t)/((nu+t^2)^0.5))
 res <- (1-alpha) - integrate(fun, -Inf, t, nu=nu, exi=exi)$value
 return(res)
}

If you have to do this frequently it would be better to move the declaration of fun to outside of myroot.

Berend

> Ravi
>
>
> From: Ravi Varadhan
> Sent: Friday, January 08, 2016 11:29 AM
> To: [hidden email]; '[hidden email]' <[hidden email]>
> Subject: Re: Solve an equation including integral
>
> I think this is what you want:
>
> myroot <- function(t, nu, exi, alpha){
>  fun <- function(t, exi, nu) (nu+t^2)^(-(nu+1)/2)*exp(((nu+1)*exi*t)/((nu+t^2)^0.5))
>  res <- alpha - integrate(fun, -Inf, t, nu=2, exi=0.5)$value
>  return(res)
> }
>
> uniroot(myroot, c(-2, 2), nu=2, exi=0.5, alpha=.05)
>
> Hope this is helpful,
> Ravi
>
> Ravi Varadhan, Ph.D. (Biostatistics), Ph.D. (Environmental Engg)
> Associate Professor,  Department of Oncology
> Division of Biostatistics & Bionformatics
> Sidney Kimmel Comprehensive Cancer Center
> Johns Hopkins University
> 550 N. Broadway, Suite 1111-E
> Baltimore, MD 21205
> 410-502-2619
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.