On Apr 9, 2012, at 3:17 PM, Kerapi wrote:

Hi!,
I'm really hoping someone out there will be able to help me.

I recently started my MSc dissertation on Population Projection Matrices, which has been going well until now. I am trying to set-up a general script that does a pairwise comparison of all elements in my matrices.

So for example, given that I have the following matrix S:

S

       [,1]     [,2]         [,3]

[1,] 0.00000 0.007361183 0.0001634936

[2,] 13.88458 0.353988378 0.0000000000

[3,] 0.00000 16.086367098 0.3572819557

I'd like to create a matrix SA for each element in my matrix compared to another element like so:

SA

S-[1,1]

        [,1]        [,2]        [,3]

[1,] [1,1]-[1,1] [1,2]-[1,1] [1,3]-[1,1]

[2,] [2,1]-[1,1] [2,2]-[1,1] [2,3]-[1,1]

[3,] [3,1]-[1,1] [3,2]-[1,1] [3,3]-[1,1]


Try this:

kS <- -1*kronecker(c(S),S, "-")
dim(kS) <- c(3, 3, 3, 3)

The row number is the thrid dimension and the col nimber is the second dimension. If you want them to be rearrange to be in the ( r,c) order you can use aperm.

> kSr <- aperm(kS, c(1, 4, 2, 3))

#The zero entires were used because I could easily tell what a correct answer should be.
> kSr[,,3,1]
         [,1]         [,2]         [,3]
[1,]  0.00000  0.007361183 0.0001634936
[2,] 13.88458  0.353988378 0.0000000000
[3,]  0.00000 16.086367098 0.3572819557
> kSr[,,1,1]
         [,1]         [,2]         [,3]
[1,]  0.00000  0.007361183 0.0001634936
[2,] 13.88458  0.353988378 0.0000000000
[3,]  0.00000 16.086367098 0.3572819557
> kSr[,,2,3]
         [,1]         [,2]         [,3]
[1,]  0.00000  0.007361183 0.0001634936
[2,] 13.88458  0.353988378 0.0000000000
[3,]  0.00000 16.086367098 0.3572819557

Test a non-zero entry
> kSr[,,2,2]
           [,1]       [,2]         [,3]
[1,] -0.3539884 -0.3466272 -0.353824884
[2,] 13.5305916  0.0000000 -0.353988378
[3,] -0.3539884 15.7323787  0.003293578
> S - S[2,2]
           [,1]       [,2]         [,3]
[1,] -0.3539884 -0.3466272 -0.353824884
[2,] 13.5305916  0.0000000 -0.353988378
[3,] -0.3539884 15.7323787  0.003293578

> sapply(1:3, function(rw) sapply(1:3,
                  function(cl) identical(S-S[rw,cl],  kSr[,,rw,cl] )) )
     [,1] [,2] [,3]
[1,] TRUE TRUE TRUE
[2,] TRUE TRUE TRUE
[3,] TRUE TRUE TRUE

--
David.


S-[1,2]

[...]



S-[1,3]

[...]

... and so on

The aim is to be able to prove existing rules and trends across matrix dimensions.

For example, I've been trying to test whether the first line of values decreases or remains the same from column 1 to the penultimate column: S[1,j] >= S[1,l>j]

The last thing I tried was:

M<-Matlab2R("[0 24.724 1377.48;0.029328 0.26 0;0 0.014 0.78]")

w <- abs(Re(eigen(M)$vectors[,1]))

v <- abs(Re(eigen(t(M))$vectors[,1]))

w <- w/sum(w)

v <- v/(t(v)%*%w)

S <- (v%*%t(w))/as.vector(t(v)%*%w)

S



n<- length(S)

M.comp <- array(S,dim=c(n,n,n,n))

for (i in 1:n) {

for (j in 1:n) {

for (k in 1:n) {

for (l in 1:n) {

poscompa[1,3,1,j-1] <- S[i,j]<=S[k,l] # if=1, then TRUE and if=0, then FALSE

}}}}

M.comp

sum(poscompa[1,3,1,1:n]) # should give me the value for n. I know this doesn't test it accurately, but first I need to get the loops to work right.

I'm getting an array of errors such as "can't find function poscompa (or compa)", "subscripts out of range" or "type of closure not valid" regardless of what I try. Using COMPAR (poscompa and compa) was the last recommendation I got, but I'm starting to wonder if there might be other ways to go about this. All out-of-the-box ideas I've come up with and tried haven't gotten me much farther. I've now practically exhausted my creative thinking, and I'm becoming very frustrated. I'd really like to get this script going since my current one would make my life hell for large populations (60x60 population matrices).

Any ideas on how I could move forward?



Many, many thanks!

Marta

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to