R-helpers: I would like to measure the correlation coefficient between the repeated measures of a single variable that is measured over time and is unbalanced. As an example, consider the Orthodont dataset from package nlme, where the model is: fit <- lmer(distance ~ age + (1 | Subject), data=Orthodont) I would like to measure the correlation b/t the variable "distance" at different ages such that I would have a matrix of correlation coefficients like the following: age08 age09 age10 age11 age12 age13 age14 age08 1 age09 1 age10 1 age11 1 age12 1 age13 1 age14 1 The idea would be to demonstrate that the correlations b/t repeated measures of the variable "distance" decrease as the time b/t measures increases. For example, one might expect the correlation coefficient? b/t age08 and age09 to be higher than that between age08 and age14. Is there a function that can calculate such correlation coefficients of a repeatedly measured variable "y" ("distance" in the Orthodont dataset) across some category "x" ("age" in the Orthodont dataset)? Thanks, Brant
Hi Brant, My version of Orthodont doesn't seem to have as many levesl of age as yours but the general idea is> library(MEMSS) > data(Orthodont) > library(reshape) # could use reshape function in stats package instead of cast > c.orth <- as.data.frame(cast(Orthodont, Subject + Sex ~ age, value="distance")) > names(c.orth)[3:6] <- paste("age", names(c.orth)[3:6], sep="_") > cor(c.orth[3:6])age_8 age_10 age_12 age_14 age_8 1.0000000 0.6255833 0.7108079 0.5998338 age_10 0.6255833 1.0000000 0.6348775 0.7593268 age_12 0.7108079 0.6348775 1.0000000 0.7949980 age_14 0.5998338 0.7593268 0.7949980 1.0000000 Best, Ista On Mon, Feb 28, 2011 at 12:07 AM, Brant Inman <brant.inman at mac.com> wrote:> R-helpers: > > I would like to measure the correlation coefficient between the repeated > measures of a single variable that is measured over time and is unbalanced. > ?As an example, consider the Orthodont dataset from package nlme, where the > model is: > > fit <- lmer(distance ~ age + (1 | Subject), data=Orthodont) > > I would like to measure the correlation b/t the variable "distance" at > different ages such that I would have a matrix of correlation coefficients > like the following: > > ? ? ?age08 age09 age10 age11 age12 age13 age14 > age08 ? ?1 > age09 ? ? ? ? ?1 > age10 ? ? ? ? ? ? ? ?1 > age11 ? ? ? ? ? ? ? ? ? ? ?1 > age12 ? ? ? ? ? ? ? ? ? ? ? ? ? ?1 > age13 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1 > age14 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1 > > The idea would be to demonstrate that the correlations b/t repeated measures > of the variable "distance" decrease as the time b/t measures increases. ?For > example, one might expect the correlation coefficient? b/t age08 and age09 > to be higher than that between age08 and age14. > Is there a function that can calculate such correlation coefficients of a > repeatedly measured variable "y" ("distance" in the Orthodont dataset) > across some category "x" ("age" in the Orthodont dataset)? > > Thanks, > > Brant > ______________________________________________ > R-help at 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
Brant Inman <brant.inman <at> mac.com> writes:> > R-helpers: > > I would like to measure the correlation coefficient between the repeatedmeasures of a single variable> that is measured over time and is unbalanced. As an example, consider theOrthodont dataset from package> nlme, where the model is: > > fit <- lmer(distance ~ age + (1 | Subject), data=Orthodont) > > I would like to measure the correlation b/t the variable "distance" atdifferent ages such that I would have> a matrix of correlation coefficients like the following: > > age08 age09 age10 age11 age12 age13 age14 > age08 1 > age09 1 > age10 1 > age11 1 > age12 1 > age13 1 > age14 1 > > The idea would be to demonstrate that the correlations b/t > repeated measures of the variable "distance" > decrease as the time b/t measures increases. For example, > one might expect the correlation > coefficient? b/t age08 and age09 to be higher than that > between age08 and age14. >This stuff is not currently possible in lmer/lme4 but is easy in nlme: library(nlme) Orthodont$age0 <- Orthodont$age/2-3 ## later code requires a time index of consecutive integers ## (which apparently must also start at 1, although not stated) fit <- lme(distance~age,random=~1|Subject,data=Orthodont) ## compute autocorrelation on the basis of lag only, plot a <- ACF(fit) plot(a,alpha=0.05) fit2 <- update(fit, correlation=corSymm(form=~age0|Subject)) fit3 <- update(fit, correlation=corAR1(form=~age0|Subject)) AIC(fit,fit2,fit3) ## at least on the basis of AIC, this extra complexity is ## not warranted anova(fit,fit2) ## likelihood ratio test
Ben, Thanks for the response. Your method generates an answer that is slightly different than what I was looking for. In the Orthodont dataset there are 4 age groups (8, 10, 12, 14). I would like to calculate the correlation of "distance" for all combinations of the categorical variable "age". The anticipated output would therefore be a matrix with 4 columns and 4 rows and a diagonal of ones. For example, in such a table I would be able to look at the mean within individual correlation coefficient for distance b/t ages 8 and 10 or, alternatively, ages 10 and 14?. Is there a function in nlme or lme4 that does this? Brant On Feb 28, 2011, at 02:24 AM, Ben Bolker <bbolker at gmail.com> wrote:> Brant Inman <brant.inman <at> mac.com> writes: > > > > > R-helpers: > > > > I would like to measure the correlation coefficient between the repeated > measures of a single variable > > that is measured over time and is unbalanced. As an example, consider the > Orthodont dataset from package > > nlme, where the model is: > > > > fit <- lmer(distance ~ age + (1 | Subject), data=Orthodont) > > > > I would like to measure the correlation b/t the variable "distance" at > different ages such that I would have > > a matrix of correlation coefficients like the following: > > > > age08 age09 age10 age11 age12 age13 age14 > > age08 1 > > age09 1 > > age10 1 > > age11 1 > > age12 1 > > age13 1 > > age14 1 > > > > The idea would be to demonstrate that the correlations b/t > > repeated measures of the variable "distance" > > decrease as the time b/t measures increases. For example, > > one might expect the correlation > > coefficient? b/t age08 and age09 to be higher than that > > between age08 and age14. > > > > This stuff is not currently possible in lmer/lme4 but is > easy in nlme: > > library(nlme) > Orthodont$age0 <- Orthodont$age/2-3 > ## later code requires a time index of consecutive integers > ## (which apparently must also start at 1, although not stated) > > fit <- lme(distance~age,random=~1|Subject,data=Orthodont) > > ## compute autocorrelation on the basis of lag only, plot > a <- ACF(fit) > plot(a,alpha=0.05) > > > fit2 <- update(fit, correlation=corSymm(form=~age0|Subject)) > fit3 <- update(fit, correlation=corAR1(form=~age0|Subject)) > > AIC(fit,fit2,fit3) > ## at least on the basis of AIC, this extra complexity is > ## not warranted > > anova(fit,fit2) ## likelihood ratio test > > ______________________________________________ > R-help at 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.