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. Is there another way to do this other than loops? For now, the 'output' vector v is not so informative. Is there a way to 'show' me for what combinations of mu and sigma, the resulting values 'v' are from? Thanks. ----- ################################################### PhD candidate in Statistics School of Mathematics, Statistics and Actuarial Science, University of Kent ################################################### -- View this message in context: http://r.789695.n4.nabble.com/Quicker-way-to-apply-values-to-a-function-tp4497293p4497293.html Sent from the R help mailing list archive at Nabble.com.
I'd imagine one could solve this problem analytically (divergence/convergence *almost certainly* [hint cough!]....closed form value seems hard) but perhaps you want to loop over a matrix instead: v <- matrix(NA, ncol = length(mymu), nrow = length(mysigma)) rownames(v) <- mysigma colnames(v) <- mymu then in the loop: v[i,j] <- myint(...) Michael On Thu, Mar 22, 2012 at 7:17 PM, casperyc <casperyc at hotmail.co.uk> 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. > > Is there another way to do this other than loops? > > For now, the 'output' vector v is not so informative. Is there a way to > 'show' me for what combinations of mu and sigma, the resulting values 'v' > are from? > > Thanks. > > > > ----- > ################################################### > PhD candidate in Statistics > School of Mathematics, Statistics and Actuarial Science, University of Kent > ################################################### > > -- > View this message in context: http://r.789695.n4.nabble.com/Quicker-way-to-apply-values-to-a-function-tp4497293p4497293.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.
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.