[R] Quicker way to apply values to a function

R. Michael Weylandt michael.weylandt at gmail.com
Fri Mar 23 06:32:17 CET 2012


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.



More information about the R-help mailing list