[R] Quicker way to apply values to a function

Petr Savicky savicky at cs.cas.cz
Fri Mar 23 10:15:04 CET 2012


On Thu, Mar 22, 2012 at 04:17:20PM -0700, casperyc wrote:
> Hi all,
> myint=function(mu,sigma){
> 	integrate(function(x) dnorm(x,mu,sigma)/(1+exp(-x)),-Inf,Inf)$value
> }
> 
> mymu=seq(-3,3,length(1000))
> mysigma=seq(0,1,length(500))[-1]
> 
> k=1
> v=c()
> for (j in 1:length(mymu)) {
> 	for (i in 1:length(mysigma)) {
> 		v[k]=myint(mymu[j],mysigma[i])
> 		k=k+1
> 	}
> }
> 
> 
> Basically, I want to investigate for what values of mu and sigma, the
> integral is divergent.

Hi.

The function dnorm(x,mu,sigma)/(1+exp(-x)) has a finite integral
over (-Inf, Inf) for every mu, sigma. The reason is that

  dnorm(x,mu,sigma)

is nonnegative and

  0 < 1/(1+exp(-x)) < 1

So, the integral of dnorm(x,mu,sigma)/(1+exp(-x)) is upper bounded
by the integral of dnorm(x,mu,sigma), which is 1.

Hope this helps.

Petr Savicky.



More information about the R-help mailing list