sapply and matrix command
On Wed, Aug 8, 2012 at 9:17 AM, alijk1989 [via R]
<ml-node+s789695n4639595h61 at n4.nabble.com> wrote:
Hi,
I have made some progress speeding up my code. This is what I have at the
moment:
M1=sum(sapply(1:m, function(k){sum(sapply(1:m,function(j){w[k]*w[j]*LGD^2
(pmnorm(c(qnorm(Q[k]),qnorm(Q[j])),c(0,0),equicorr(2,rho[k,j]))-Q[k]*Q[j])}))}))
I tried setting up a function as so:
f1 <- function(k,j)
{w[k]*w[j]*LGD^2*(pmnorm(c(qnorm(Q[k]),qnorm(Q[j])),c(0,0),equicorr(2,rho[k,j]))-Q[k]*Q[j])}
Then run outer and sum as so:
sum(outer(1:m,1:m,foo))
Unfortunately outer doesn't seem to like the equicorr or matrix commands
like my problem above. Is there any way around this or am I stuck with the
double sapply?
Thanks again for your help!
I'm afraid we have no context for what you're trying to do -- the vast majority of us don't use Nabble. Could you enlighten us? Also, please do try to make your code more legible by breaking things up into multiple lines. Finally, try to make a reproducible example according to the hints herein: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Cheers, Michael