Help creating a symmetric matrix?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Help creating a symmetric matrix?

Matt Considine
Hi,
I am trying to work with the output of the MINE analysis routine found at
   http://www.exploredata.net

Specifically, I am trying to read the results into a matrix (ideally an
n x n x 6 matrix, but I'll settle right now for getting one column into
a matrix.)

The problem I have is not knowing how to take what amounts to being one
half of a symmetric matrix - excluding the diagonal - and getting it
into a matrix.  I have tried using "lower.tri" as found here
   https://stat.ethz.ch/pipermail/r-help/2008-September/174516.html
but it appears to only partially fill in the matrix.  My code and an
example of the output is below.  Can anyone point me to an example that
shows how to create a matrix with this sort of input?

Thank you in advance,
Matt

require(PortfolioAnalytics)
#load market index data
data(indexes)
#save data as a CSV
write.table(indexes, "C:/Rwork/indexes.csv", sep=",", col.names=TRUE,
   row.names=FALSE, quote=FALSE, na="NA")
#assumes rJava is installed, MINE.r and MINE.jar are in the working
directory
#read in MINE.r
source.with.encoding('C:/Rwork/MINE.r', encoding='UTF-8')
#run MINE on indexes
MINE("C:/Rwork/indexes.csv","all.pairs")
#read the output file of MINE analysis
x=read.csv("C:/Rwork/indexes.csv,B=n^0.6,k=15,Results.csv",header=TRUE)
#isolate one half of matrix
newx<-x[,1:3]
newx
             X.var          Y.var MIC..strength.
1     US.Equities Int.l.Equities        0.33740
2        US.Bonds       US.Tbill        0.26657
3        US.Tbill      Inflation        0.23388
4     Commodities      Inflation        0.23122
5     Commodities       US.Tbill        0.21476
6     US.Equities       US.Tbill        0.20829
7        US.Bonds      Inflation        0.20486
8  Int.l.Equities    Commodities        0.19439
9        US.Bonds    Commodities        0.19237
10    US.Equities    Commodities        0.18633
11       US.Bonds    US.Equities        0.17298
12    US.Equities      Inflation        0.17174
13 Int.l.Equities       US.Tbill        0.16822
14       US.Bonds Int.l.Equities        0.16480
15 Int.l.Equities      Inflation        0.15027

#v<-newx[,3]
#or, for the sake of this example
v<-c(0.33740, 0.26657, 0.23388, 0.23122, 0.21476, 0.20829, 0.20486,
0.19439, 0.19237,
0.18633, 0.17298, 0.17174, 0.16822, 0.16480, 0.15027)
z<-diag(6)
ind <- lower.tri(z)
z[ind] <- t(v)[ind]

z
         [,1]    [,2] [,3] [,4] [,5] [,6]
[1,] 1.00000 0.00000    0    0    0    0
[2,] 0.26657 1.00000    0    0    0    0
[3,] 0.23388 0.19237    1    0    0    0
[4,] 0.23122 0.18633   NA    1    0    0
[5,] 0.21476 0.17298   NA   NA    1    0
[6,] 0.20829 0.17174   NA   NA   NA    1


        [[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.
Reply | Threaded
Open this post in threaded view
|

Re: Help creating a symmetric matrix?

Rui Barradas
Matt Considine wrote
Hi,
I am trying to work with the output of the MINE analysis routine found at
   http://www.exploredata.net

Specifically, I am trying to read the results into a matrix (ideally an
n x n x 6 matrix, but I'll settle right now for getting one column into
a matrix.)

The problem I have is not knowing how to take what amounts to being one
half of a symmetric matrix - excluding the diagonal - and getting it
into a matrix.  I have tried using "lower.tri" as found here
   https://stat.ethz.ch/pipermail/r-help/2008-September/174516.html
but it appears to only partially fill in the matrix.  My code and an
example of the output is below.  Can anyone point me to an example that
shows how to create a matrix with this sort of input?

Thank you in advance,
Matt

#v<-newx[,3]
#or, for the sake of this example
v<-c(0.33740, 0.26657, 0.23388, 0.23122, 0.21476, 0.20829, 0.20486,
0.19439, 0.19237,
0.18633, 0.17298, 0.17174, 0.16822, 0.16480, 0.15027)
z<-diag(6)
ind <- lower.tri(z)
z[ind] <- t(v)[ind]

z
         [,1]    [,2] [,3] [,4] [,5] [,6]
[1,] 1.00000 0.00000    0    0    0    0
[2,] 0.26657 1.00000    0    0    0    0
[3,] 0.23388 0.19237    1    0    0    0
[4,] 0.23122 0.18633   NA    1    0    0
[5,] 0.21476 0.17298   NA   NA    1    0
[6,] 0.20829 0.17174   NA   NA   NA    1


        [[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.
Hello,

Aren't you complicating?

In the last line of your code, why use 'v[ind]' if 'ind' indexes the matrix, not the vector?

z<-diag(6)
ind <- lower.tri(z)
z[ind] <- v                        #This works
z

Rui Barradas
Reply | Threaded
Open this post in threaded view
|

Re: Help creating a symmetric matrix?

Sarah Goslee
Or the slightly shorter:

z<-diag(6)
z[row(z) > col(z)] <- v

which is what lower.tri() does,

and
z <- diag(6)
z[lower.tri(z)] <- v

also works.

Sarah

On Fri, Dec 23, 2011 at 9:31 PM, Rui Barradas <[hidden email]> wrote:

>
> Matt Considine wrote
>>
>> Hi,
>> I am trying to work with the output of the MINE analysis routine found at
>>    http://www.exploredata.net
>>
>> Specifically, I am trying to read the results into a matrix (ideally an
>> n x n x 6 matrix, but I'll settle right now for getting one column into
>> a matrix.)
>>
>> The problem I have is not knowing how to take what amounts to being one
>> half of a symmetric matrix - excluding the diagonal - and getting it
>> into a matrix.  I have tried using "lower.tri" as found here
>>    https://stat.ethz.ch/pipermail/r-help/2008-September/174516.html
>> but it appears to only partially fill in the matrix.  My code and an
>> example of the output is below.  Can anyone point me to an example that
>> shows how to create a matrix with this sort of input?
>>
>> Thank you in advance,
>> Matt
>>
>> #v<-newx[,3]
>> #or, for the sake of this example
>> v<-c(0.33740, 0.26657, 0.23388, 0.23122, 0.21476, 0.20829, 0.20486,
>> 0.19439, 0.19237,
>> 0.18633, 0.17298, 0.17174, 0.16822, 0.16480, 0.15027)
>> z<-diag(6)
>> ind <- lower.tri(z)
>> z[ind] <- t(v)[ind]
>>
>> z
>>          [,1]    [,2] [,3] [,4] [,5] [,6]
>> [1,] 1.00000 0.00000    0    0    0    0
>> [2,] 0.26657 1.00000    0    0    0    0
>> [3,] 0.23388 0.19237    1    0    0    0
>> [4,] 0.23122 0.18633   NA    1    0    0
>> [5,] 0.21476 0.17298   NA   NA    1    0
>> [6,] 0.20829 0.17174   NA   NA   NA    1
>>
>>
> Hello,
>
> Aren't you complicating?
>
> In the last line of your code, why use 'v[ind]' if 'ind' indexes the matrix,
> not the vector?
>
> z<-diag(6)
> ind <- lower.tri(z)
> z[ind] <- v                        #This works
> z
>
> Rui Barradas
>

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Help creating a symmetric matrix?

William Revelle
Dear Matt, Sarah and Rui,

To answer the original question for creating a symmetric matrix


>>> v<-c(0.33740, 0.26657, 0.23388, 0.23122, 0.21476, 0.20829, 0.20486,
>>> 0.19439, 0.19237,
>>> 0.18633, 0.17298, 0.17174, 0.16822, 0.16480, 0.15027)


z<-diag(6)
z[row(z) > col(z)] <- v
z <- z + t(z)
diag(z) <- 0  

> z
        [,1]    [,2]    [,3]    [,4]    [,5]    [,6]
[1,] 0.00000 0.33740 0.26657 0.23388 0.23122 0.21476
[2,] 0.33740 0.00000 0.20829 0.20486 0.19439 0.19237
[3,] 0.26657 0.20829 0.00000 0.18633 0.17298 0.17174
[4,] 0.23388 0.20486 0.18633 0.00000 0.16822 0.16480
[5,] 0.23122 0.19439 0.17298 0.16822 0.00000 0.15027
[6,] 0.21476 0.19237 0.17174 0.16480 0.15027 0.00000


Bill


On Dec 24, 2011, at 6:04 AM, Sarah Goslee wrote:

> Or the slightly shorter:
>
> z<-diag(6)
> z[row(z) > col(z)] <- v
>
> which is what lower.tri() does,
>
> and
> z <- diag(6)
> z[lower.tri(z)] <- v
>
> also works.
>
> Sarah
>
> On Fri, Dec 23, 2011 at 9:31 PM, Rui Barradas <[hidden email]> wrote:
>>
>> Matt Considine wrote
>>>
>>> Hi,
>>> I am trying to work with the output of the MINE analysis routine found at
>>>    http://www.exploredata.net
>>>
>>> Specifically, I am trying to read the results into a matrix (ideally an
>>> n x n x 6 matrix, but I'll settle right now for getting one column into
>>> a matrix.)
>>>
>>> The problem I have is not knowing how to take what amounts to being one
>>> half of a symmetric matrix - excluding the diagonal - and getting it
>>> into a matrix.  I have tried using "lower.tri" as found here
>>>    https://stat.ethz.ch/pipermail/r-help/2008-September/174516.html
>>> but it appears to only partially fill in the matrix.  My code and an
>>> example of the output is below.  Can anyone point me to an example that
>>> shows how to create a matrix with this sort of input?
>>>
>>> Thank you in advance,
>>> Matt
>>>
>>> #v<-newx[,3]
>>> #or, for the sake of this example
>>> v<-c(0.33740, 0.26657, 0.23388, 0.23122, 0.21476, 0.20829, 0.20486,
>>> 0.19439, 0.19237,
>>> 0.18633, 0.17298, 0.17174, 0.16822, 0.16480, 0.15027)
>>> z<-diag(6)
>>> ind <- lower.tri(z)
>>> z[ind] <- t(v)[ind]
>>>
>>> z
>>>          [,1]    [,2] [,3] [,4] [,5] [,6]
>>> [1,] 1.00000 0.00000    0    0    0    0
>>> [2,] 0.26657 1.00000    0    0    0    0
>>> [3,] 0.23388 0.19237    1    0    0    0
>>> [4,] 0.23122 0.18633   NA    1    0    0
>>> [5,] 0.21476 0.17298   NA   NA    1    0
>>> [6,] 0.20829 0.17174   NA   NA   NA    1
>>>
>>>
>> Hello,
>>
>> Aren't you complicating?
>>
>> In the last line of your code, why use 'v[ind]' if 'ind' indexes the matrix,
>> not the vector?
>>
>> z<-diag(6)
>> ind <- lower.tri(z)
>> z[ind] <- v                        #This works
>> z
>>
>> Rui Barradas
>>
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
> ______________________________________________
> [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.
>

William Revelle           http://personality-project.org/revelle.html
Professor           http://personality-project.org
Department of Psychology   http://www.wcas.northwestern.edu/psych/
Northwestern University   http://www.northwestern.edu/
Use R for psychology             http://personality-project.org/r
It is 6 minutes to midnight   http://www.thebulletin.org

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Help creating a symmetric matrix?

Sarah Goslee
On Sat, Dec 24, 2011 at 8:38 AM, William Revelle <[hidden email]> wrote:
> Dear Matt, Sarah and Rui,
>
> To answer the original question for creating a symmetric matrix

I read the original question as *only* wanting the complete lower
triangle, with diagonal of 1 and 0 in the upper triangle.

If your interpretation is correct, there's also this convenience function:

library(ecodist)
z <- full(v)

Sarah

>
>>>> v<-c(0.33740, 0.26657, 0.23388, 0.23122, 0.21476, 0.20829, 0.20486,
>>>> 0.19439, 0.19237,
>>>> 0.18633, 0.17298, 0.17174, 0.16822, 0.16480, 0.15027)
>
>
> z<-diag(6)
> z[row(z) > col(z)] <- v
> z <- z + t(z)
> diag(z) <- 0
>
>> z
>        [,1]    [,2]    [,3]    [,4]    [,5]    [,6]
> [1,] 0.00000 0.33740 0.26657 0.23388 0.23122 0.21476
> [2,] 0.33740 0.00000 0.20829 0.20486 0.19439 0.19237
> [3,] 0.26657 0.20829 0.00000 0.18633 0.17298 0.17174
> [4,] 0.23388 0.20486 0.18633 0.00000 0.16822 0.16480
> [5,] 0.23122 0.19439 0.17298 0.16822 0.00000 0.15027
> [6,] 0.21476 0.19237 0.17174 0.16480 0.15027 0.00000
>
>
> Bill
>
>
> On Dec 24, 2011, at 6:04 AM, Sarah Goslee wrote:
>
>> Or the slightly shorter:
>>
>> z<-diag(6)
>> z[row(z) > col(z)] <- v
>>
>> which is what lower.tri() does,
>>
>> and
>> z <- diag(6)
>> z[lower.tri(z)] <- v
>>
>> also works.
>>
>> Sarah
>>
>> On Fri, Dec 23, 2011 at 9:31 PM, Rui Barradas <[hidden email]> wrote:
>>>
>>> Matt Considine wrote
>>>>
>>>> Hi,
>>>> I am trying to work with the output of the MINE analysis routine found at
>>>>    http://www.exploredata.net
>>>>
>>>> Specifically, I am trying to read the results into a matrix (ideally an
>>>> n x n x 6 matrix, but I'll settle right now for getting one column into
>>>> a matrix.)
>>>>
>>>> The problem I have is not knowing how to take what amounts to being one
>>>> half of a symmetric matrix - excluding the diagonal - and getting it
>>>> into a matrix.  I have tried using "lower.tri" as found here
>>>>    https://stat.ethz.ch/pipermail/r-help/2008-September/174516.html
>>>> but it appears to only partially fill in the matrix.  My code and an
>>>> example of the output is below.  Can anyone point me to an example that
>>>> shows how to create a matrix with this sort of input?
>>>>
>>>> Thank you in advance,
>>>> Matt
>>>>
>>>> #v<-newx[,3]
>>>> #or, for the sake of this example
>>>> v<-c(0.33740, 0.26657, 0.23388, 0.23122, 0.21476, 0.20829, 0.20486,
>>>> 0.19439, 0.19237,
>>>> 0.18633, 0.17298, 0.17174, 0.16822, 0.16480, 0.15027)
>>>> z<-diag(6)
>>>> ind <- lower.tri(z)
>>>> z[ind] <- t(v)[ind]
>>>>
>>>> z
>>>>          [,1]    [,2] [,3] [,4] [,5] [,6]
>>>> [1,] 1.00000 0.00000    0    0    0    0
>>>> [2,] 0.26657 1.00000    0    0    0    0
>>>> [3,] 0.23388 0.19237    1    0    0    0
>>>> [4,] 0.23122 0.18633   NA    1    0    0
>>>> [5,] 0.21476 0.17298   NA   NA    1    0
>>>> [6,] 0.20829 0.17174   NA   NA   NA    1
>>>>
>>>>
>>> Hello,
>>>
>>> Aren't you complicating?
>>>
>>> In the last line of your code, why use 'v[ind]' if 'ind' indexes the matrix,
>>> not the vector?
>>>
>>> z<-diag(6)
>>> ind <- lower.tri(z)
>>> z[ind] <- v                        #This works
>>> z
>>>
>>> Rui Barradas
>>>
>>
>> --

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

Re: Help creating a symmetric matrix?

Matt Considine
In reply to this post by William Revelle
Thank you all for your help and best wishes for the holiday season.
Matt Considine

On 12/24/2011 8:38 AM, William Revelle wrote:

> Dear Matt, Sarah and Rui,
>
> To answer the original question for creating a symmetric matrix
>
>
>>>> v<-c(0.33740, 0.26657, 0.23388, 0.23122, 0.21476, 0.20829, 0.20486,
>>>> 0.19439, 0.19237,
>>>> 0.18633, 0.17298, 0.17174, 0.16822, 0.16480, 0.15027)
>
> z<-diag(6)
> z[row(z)>  col(z)]<- v
> z<- z + t(z)
> diag(z)<- 0
>
>> z
>          [,1]    [,2]    [,3]    [,4]    [,5]    [,6]
> [1,] 0.00000 0.33740 0.26657 0.23388 0.23122 0.21476
> [2,] 0.33740 0.00000 0.20829 0.20486 0.19439 0.19237
> [3,] 0.26657 0.20829 0.00000 0.18633 0.17298 0.17174
> [4,] 0.23388 0.20486 0.18633 0.00000 0.16822 0.16480
> [5,] 0.23122 0.19439 0.17298 0.16822 0.00000 0.15027
> [6,] 0.21476 0.19237 0.17174 0.16480 0.15027 0.00000
>
>
> Bill
>
>
> On Dec 24, 2011, at 6:04 AM, Sarah Goslee wrote:
>
>> Or the slightly shorter:
>>
>> z<-diag(6)
>> z[row(z)>  col(z)]<- v
>>
>> which is what lower.tri() does,
>>
>> and
>> z<- diag(6)
>> z[lower.tri(z)]<- v
>>
>> also works.
>>
>> Sarah
>>
>> On Fri, Dec 23, 2011 at 9:31 PM, Rui Barradas<[hidden email]>  wrote:
>>> Matt Considine wrote
>>>> Hi,
>>>> I am trying to work with the output of the MINE analysis routine found at
>>>>     http://www.exploredata.net
>>>>
>>>> Specifically, I am trying to read the results into a matrix (ideally an
>>>> n x n x 6 matrix, but I'll settle right now for getting one column into
>>>> a matrix.)
>>>>
>>>> The problem I have is not knowing how to take what amounts to being one
>>>> half of a symmetric matrix - excluding the diagonal - and getting it
>>>> into a matrix.  I have tried using "lower.tri" as found here
>>>>     https://stat.ethz.ch/pipermail/r-help/2008-September/174516.html
>>>> but it appears to only partially fill in the matrix.  My code and an
>>>> example of the output is below.  Can anyone point me to an example that
>>>> shows how to create a matrix with this sort of input?
>>>>
>>>> Thank you in advance,
>>>> Matt
>>>>
>>>> #v<-newx[,3]
>>>> #or, for the sake of this example
>>>> v<-c(0.33740, 0.26657, 0.23388, 0.23122, 0.21476, 0.20829, 0.20486,
>>>> 0.19439, 0.19237,
>>>> 0.18633, 0.17298, 0.17174, 0.16822, 0.16480, 0.15027)
>>>> z<-diag(6)
>>>> ind<- lower.tri(z)
>>>> z[ind]<- t(v)[ind]
>>>>
>>>> z
>>>>           [,1]    [,2] [,3] [,4] [,5] [,6]
>>>> [1,] 1.00000 0.00000    0    0    0    0
>>>> [2,] 0.26657 1.00000    0    0    0    0
>>>> [3,] 0.23388 0.19237    1    0    0    0
>>>> [4,] 0.23122 0.18633   NA    1    0    0
>>>> [5,] 0.21476 0.17298   NA   NA    1    0
>>>> [6,] 0.20829 0.17174   NA   NA   NA    1
>>>>
>>>>
>>> Hello,
>>>
>>> Aren't you complicating?
>>>
>>> In the last line of your code, why use 'v[ind]' if 'ind' indexes the matrix,
>>> not the vector?
>>>
>>> z<-diag(6)
>>> ind<- lower.tri(z)
>>> z[ind]<- v                        #This works
>>> z
>>>
>>> Rui Barradas
>>>
>> --
>> Sarah Goslee
>> http://www.functionaldiversity.org
>>
>> ______________________________________________
>> [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.
>>
> William Revelle           http://personality-project.org/revelle.html
> Professor           http://personality-project.org
> Department of Psychology   http://www.wcas.northwestern.edu/psych/
> Northwestern University   http://www.northwestern.edu/
> Use R for psychology             http://personality-project.org/r
> It is 6 minutes to midnight   http://www.thebulletin.org
>
>
>
>

______________________________________________
[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.