[R] Covariance/Correlation matrix for repeated measures data frame

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Wed Sep 6 18:12:21 CEST 2006

```try the following:

dat <- data.frame(id = rep(1:100, each = 6), time = rep(1:6, 100), pot
= rnorm(600))

# for a balanced data-set
mat <- matrix(dat\$pot, ncol = 6, byrow = TRUE)
cor(mat)

# for a unbalanced data-set
dat <- dat[-sample(600, 100), ]
mat <- t(sapply(split(dat, dat\$id), function(x){
out <- rep(NA, 6)
out[x\$time] <- x\$pot
out
}))

cor(mat, use = "pairwise.complete.obs")

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm

----- Original Message -----
From: "Afshartous, David" <afshart at exchange.sba.miami.edu>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, September 06, 2006 5:59 PM
Subject: [R] Covariance/Correlation matrix for repeated measures data
frame

> All,
>
> I have a repeated measures data frame and was wondering if the
> covariance matrix can be
> calculated via some created indexing or built-in R function.
>
> Specifically, say there are 3 variables, where potassium
> concentration
> is measured 6 times on each patient.
> Patient number (discrete)
> Time (1 to 6, discrete)
> Potassium (continuous variable)
>
> I want the covariance/correlation matrix for the cov/corr between
> Potassium at time i and time j.
>
> Is this possible in the current dataframe format?  Or do I have to
> define new varialbes, say Time i and Time j,
> and then compute the cov/corr between Time i and Time j for all
> combinations?
>
> Cheers,
> Dave
>
>
>
>
> David Afshartous, PhD
> University of Miami
> Rm KE-408
> Coral Gables, FL 33124
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help