[R] Constructing Matrices

Greg Snow greg.snow at ihc.com
Thu Jan 20 18:08:54 CET 2005


Does the following do what you want (or at least get you closer)?

> tmp <- matrix(0,16,16)
> tmp[col(tmp)%%4 == row(tmp)%%4] <- 64
> tmp
...




Greg Snow, Ph.D.
Statistical Data Center
greg.snow at ihc.com
(801) 408-8111

>>> "Doran, Harold" <HDoran at air.org> 01/20/05 07:17AM >>>
I should probably have explained my data and model a little better.
Assume I have student achievement scores across four time points. I
estimate the model using gls() as follows

fm1 <- gls(score ~ time, long, correlation=corAR1(form=~1|stuid),
method='ML')

I can now extract the variance-covariance matrix for this model as
follows:

var.mat<-getVarCov(fm1)

Assume for sake of argument I have a sample size of 100 students. I
can
expand this to the full matrix as follows
I<-diag(100)
V<-kronecker(I,var.mat)

For my particular model, the scores within each student are assumed
correlated (AR1), but across student are uncorrelated. Now, for a
particular problem I am dealing with I need to make some adjustments
to
this matrix, V,  and reestimate the gls(). The adjustments I need to
make cannot be done using any of the existing varFunc classes, so I am
having to do this manually.

What I need to do is create a new matrix manually, add it to V, then
reestimate the gls. Creating this new matrix is the challenge I
currently face, let's call it v.prime. 

The issue at hand is creating v.prime to have non-zero covariance
terms
across students in very specific places. The matrix I used below is
only
for two students. But assume I am doing this for thousands of
students.
My goal is to create a full block-diagonal covariance matrix where the
correlation across students at time two is always perfectly correlated
and the correlation at time three is always perfectly correlated
across
students. So, within each block of v.prime, the variances are
uncorrelated, but across each block the variances are correlated. 

So, I need to construct v.prime such that it is one the same order of
V
to make them conformable for addition. More importantly, I need the
off-diagonal elements across students to represent a perfect
correlation
in very specific places. In the example below, if there was a 64 at
position (2,6) this would represent a perfect correlation between
student 1 and 2 at this point in time since the variance along the
diagonal at time 2 is 64. Since I am doing this for many students,
there
would need to be a 64 between student 1 and all other students (not
just
student 2) and so on.

>From here I can use R's matrix facilities to reestimate the gls.

I hope this clarifies a bit.

Harold

-----Original Message-----
From: Liaw, Andy [mailto:andy_liaw at merck.com] 
Sent: Thursday, January 20, 2005 8:41 AM
To: Doran, Harold; r-help at stat.math.ethz.ch 
Subject: RE: [R] Constructing Matrices

I'm still not clear on exactly what your question is.  If you can plug
in the numbers you want in, say, the lower triangular portion, you can
copy those to the upper triangular part easily; something like:

m[upper.tri(m)] <- m[lower.tri(m)]

Is that what you're looking for?

Andy

> From: Doran, Harold
> 
> Dear List:
> 
> I am working to construct a matrix of a particular form. For the most

> part, developing the matrix is simple and is built as follows:
> 
> vl.mat<-matrix(c(0,0,0,0,0,64,0,0,0,0,64,0,0,0,0,64),nc=4)
> 
> Now to expand this matrix to be block-diagonal, I do the following:
> 
> sample.size <- 100 # number of individual students
> I<- diag(sample.size)
> bd.mat<-kronecker(I,vl.mat)
> 
> This creates a block-diagonal matrix with variances along the
diagonal

> and covariances within-student to be zero (I am working with 
> longitudinal student achievement data). However, across student, I 
> want to have the correlation equal to 1 for each variance term. To 
> illustrate, here is a matrix for 2 students. The goal is for the 
> correlation between the second variance term for student 1 to be 
> perfectly correlated with the variance term for student 2. In other 
> words, I need to plug in 64 at position (6,2) and (2,6), another 64
at

> position (7,3) and (3,7) and another 64 at positions (8,4) and
(4,8).
> I'm having some difficulty conceptualizing how to construct this part

> of the matrix and would appreciate any thoughts.
> 
> Thank you,
> Harold
> 
> 
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
> [1,]    0    0    0    0    0    0    0    0
> [2,]    0   64    0    0    0    0    0    0
> [3,]    0    0   64    0    0    0    0    0
> [4,]    0    0    0   64    0    0    0    0
> [5,]    0    0    0    0    0    0    0    0
> [6,]    0    0    0    0    0   64    0    0
> [7,]    0    0    0    0    0    0   64    0
> [8,]    0    0    0    0    0    0    0   64
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help 
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html 
> 
> 


------------------------------------------------------------------------
------
Notice:  This e-mail message, together with any
attachments,...{{dropped}}

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help 
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list