[S] Vectorizing problem

Kent E. Holsinger (kent@darwin.eeb.uconn.edu)
31 Jul 1998 15:43:07 -0400


Folks,

There is probably a very simple solution to this problem, but I'm not
very good at programming in S-Plus, and I haven't been able to find it.

I have the following set of functions:

> fst
function(s, mu, m)
{
p <- p.bar(s, mu, m)
const <- integrate(raw, 0, 1, sel = s, mut = mu, mig = m)$integral
p2 <- integrate(raw.x2, 0, 1, sel = s, mut = mu, mig = m)$integral/const
vp <- p2 - p^2
vp/(p * (1 - p))
}
> p.bar
function(s, mu, m)
{
const <- integrate(raw, 0, 1, sel = s, mut = mu, mig = m)$integral
p <- integrate(raw.x, 0, 1, sel = s, mut = mu, mig = m)$integral
p/const
}
> raw
function(x, sel, mut, mig)
{
s <- rep(sel, length(x))
mu <- rep(mut, length(x))
m <- rep(mig, length(x))
(exp( - s * (1 - x)) + exp( - s * x)) * (x * (1 - x))^(mu + (m/2) - 1)
}
> raw.x
function(x, sel, mut, mig)
{
s <- rep(sel, length(x))
mu <- rep(mut, length(x))
m <- rep(mig, length(x))
x * (exp( - s * (1 - x)) + exp( - s * x)) * (x * (1 - x))^(mu + (m/2) - 1)
}
> raw.x2
function(x, sel, mut, mig)
{
s <- rep(sel, length(x))
mu <- rep(mut, length(x))
m <- rep(mig, length(x))
(x^2) * (exp( - s * (1 - x)) + exp( - s * x)) * (x * (1 - x))^(mu + (m/2) - 1)
}

I also have vectors of data for s, mu, and m. What I'd like to do is
find a better way of doing this:

for (i in 1:length(s)) {
result[i] <- fst(s=s[i],mu=mu[i],m=m[i])
}

I have a feeling that I should be able to use apply() somehow, but I
haven't figured out how.

Thanks.

Kent

-- 
Kent E. Holsinger                Kent@Darwin.EEB.UConn.Edu
                                 http://darwin.eeb.uconn.edu
-- Department of Ecology & Evolutionary Biology          
-- University of Connecticut, U-43                                       
-- Storrs, CT   06269-3043                                               

----------------------------------------------------------------------- This message was distributed by s-news@wubios.wustl.edu. To unsubscribe send e-mail to s-news-request@wubios.wustl.edu with the BODY of the message: unsubscribe s-news