|
This post has NOT been accepted by the mailing list yet.
Hi,
I have had a bit of a search for this but I can find a clear answer. I am working on a problem which uses the matrix command inside sapply but it doesn't seem to be working for me. I have had a play about with lapply and matrix and I am getting some strange results as well. Here is a simple example to illustrate my point: A=20:30 > A [1] 20 21 22 23 24 25 26 27 28 29 30 > a=1:5 > matrix(c(1,A[1],A[2],1),2,2) [,1] [,2] [1,] 1 21 [2,] 20 1 > lapply(a,function(x) matrix(c(1,A[x],A[x],1),2,2)) [[1]] [,1] [,2] [1,] 1 20 [2,] 20 1 [[2]] [,1] [,2] [1,] 1 21 [2,] 21 1 [[3]] [,1] [,2] [1,] 1 22 [2,] 22 1 [[4]] [,1] [,2] [1,] 1 23 [2,] 23 1 [[5]] [,1] [,2] [1,] 1 24 [2,] 24 1 So it works fine when using just one variable but when I introduce a second variable in lapply as so it falls down: lapply(c(a,a),function(x,y) matrix(c(1,A[x],A[y],1),2,2)) [[1]] [,1] [,2] [1,] 1 20 [2,] 20 21 [[2]] [,1] [,2] [1,] 1 20 [2,] 21 21 [[3]] [,1] [,2] [1,] 1 20 [2,] 22 21 [[4]] [,1] [,2] [1,] 1 20 [2,] 23 21 [[5]] [,1] [,2] [1,] 1 20 [2,] 24 21 [[6]] [,1] [,2] [1,] 1 20 [2,] 20 21 [[7]] [,1] [,2] [1,] 1 20 [2,] 21 21 [[8]] [,1] [,2] [1,] 1 20 [2,] 22 21 [[9]] [,1] [,2] [1,] 1 20 [2,] 23 21 [[10]] [,1] [,2] [1,] 1 20 [2,] 24 21 Hopefully someone can point me in the right direction as with the above code I would expect to get matrices of the form: 1 A[x] A[y] 1 and get 25 matrices in total but instead I am getting 10 matrices incorrectly specified. Thanks in Advance! |
|
This post has NOT been accepted by the mailing list yet.
HI,
If you want every possible combination to be stored in list, this might be helpful, A<-20:30 b<-list() for(i in 1:11){ b[[i]]<-list() for(j in 1:11) { b[[i]][[j]]<-matrix(c(1,A[i],A[j],1),2,2) } } b[[1]][[2]] [,1] [,2] [1,] 1 21 [2,] 20 1 |
|
This post has NOT been accepted by the mailing list yet.
Hi,
Thanks for your reply. Ideally I was looking to avoid loops because in reality I'm going to have 2 loops of 5000 which doesn't run very quickly at all. The end results is going to be putting the matrix as a correlation matrix into pmnorm. Cheers! |
|
HI,
You can also try this: d<-1:25 A<-sample(combn(20:30,2)) B<-sample(combn(20:30,2)) lapply(d,function(x) matrix(c(1,A[x],B[x],1),2,2)) [[1]] [,1] [,2] [1,] 1 23 [2,] 27 1 [[2]] [,1] [,2] [1,] 1 21 [2,] 21 1 [[3]] [,1] [,2] [1,] 1 29 [2,] 23 1 [[4]] [,1] [,2] [1,] 1 25 [2,] 20 1 [[5]] [,1] [,2] [1,] 1 23 [2,] 28 1 ------------ ----------- [[24]] [,1] [,2] [1,] 1 24 [2,] 26 1 [[25]] [,1] [,2] [1,] 1 22 [2,] 22 1 A.K. |
|
Thanks again for the help looks like this will be useful for what I'm doing. Is there any way to use combn to return combinations of values with themselves:
e.g. >combn(1:3,2) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1 1 2 2 3 [2,] 1 2 3 2 3 3 |
|
Take a look at ?expand.grid
Michael On Aug 4, 2012, at 5:03 PM, "alijk1989 [via R]" <[hidden email]> wrote: > > > Thanks again for the help looks like this will be useful for what I'm doing. > Is there any way to use combn to return combinations of values with > themselves: > > e.g. > >> combn(1:3,2) > > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] 1 1 1 2 2 3 > [2,] 1 2 3 2 3 3 > > > > _______________________________________________ > If you reply to this email, your message will be added to the discussion below: > http://r.789695.n4.nabble.com/sapply-and-matrix-command-tp4637769p4639190.html > > To unsubscribe from sapply and matrix command, visit [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
|
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! |
|
On Wed, Aug 8, 2012 at 9:17 AM, alijk1989 [via R]
<[hidden email]> 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 ______________________________________________ [hidden email] 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. |
|
Hi Michael,
Thanks for your response. Here is a simple example of what I am trying to do: w=rep(0.02,10) Q=rep(0.02,10) rho=matrix(0.5,nrow=10,ncol=10) m=10 LGD=0.45 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])}))})) It uses the mnormt and QRM packages. I am trying to reproduce the following sum: \sum\limits_{j=1}^M \sum\limits_{k=1}^M w_j w_k LGD^2[N_2(N^{-1}[Q(j)],N^{-1}[Q(k)],\rho_{jk})-Q(j)Q(k)] |
|
On Wed, Aug 8, 2012 at 10:37 AM, alijk1989 [via R]
<[hidden email]> wrote: > > > Hi Michael, > > Thanks for your response. Here is a simple example of what I am trying to > do: > > w=rep(0.02,10) > Q=rep(0.02,10) > rho=matrix(0.5,nrow=10,ncol=10) > m=10 > LGD=0.45 > > 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])}))})) > > It uses the mnormt and QRM packages. > > I am trying to reproduce the following sum: > > \sum\limits_{j=1}^M \sum\limits_{k=1}^M w_j w_k > LGD^2[N_2(N^{-1}[Q(j)],N^{-1}[Q(k)],\rho_{jk})-Q(j)Q(k)] Hi Alijk, The problem ultimately comes from the fact that equicorr() is not nicely vectorized so you'll need to home-brew something to replace it, but that's not hard. However, I don't think that pmnorm is vectorized in the varcov argument: we can probably work out the double sapply() loop if you can assume rho is constant, otherwise I think you're stuck. Michael > > > > _______________________________________________ > If you reply to this email, your message will be added to the discussion below: > http://r.789695.n4.nabble.com/sapply-and-matrix-command-tp4637769p4639622.html > > To unsubscribe from sapply and matrix command, visit [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
