[R] calculating correlation coefficients on repeated measures

Sarah Goslee sarah.goslee at gmail.com
Mon Dec 19 12:56:48 CET 2011


Hi Keith,

You do need to reorganize your data. cor() will work on any number of variables
as long as they are columns in a matrix or data frame.

There are a lot of ways to reorganize data, of various power and complexity.
Here's one simple way:

> library(ecodist)
> WW_Sample_table <- with(WW_Sample_SI, crosstab(Individual_ID, FeatherPosition, Delta13C))
> WW_Sample_table
             P1     P2     P3     P4     P5     P6    P7     P8     P9
WW_08I_01 -18.3 -18.53 -19.55 -20.18 -20.96 -21.08 -21.5 -17.42 -13.18
WW_08I_03 -22.3 -22.20 -22.18 -22.14 -21.55 -20.85 -23.1 -20.75 -20.90
> cor(WW_Sample_table)
   P1 P2 P3 P4 P5 P6 P7 P8 P9
P1  1  1  1  1  1 -1  1  1  1
P2  1  1  1  1  1 -1  1  1  1
P3  1  1  1  1  1 -1  1  1  1
P4  1  1  1  1  1 -1  1  1  1
P5  1  1  1  1  1 -1  1  1  1
P6 -1 -1 -1 -1 -1  1 -1 -1 -1
P7  1  1  1  1  1 -1  1  1  1
P8  1  1  1  1  1 -1  1  1  1
P9  1  1  1  1  1 -1  1  1  1

(With only two values, the correlation table is rather useless, but enough to
give the idea.)

However, cor.test() is what you'd need for significance testing, and it only
works on one pair of variables at a time. It's still easier to put them into
separate columns.

> WW_Sample_table <- data.frame(WW_Sample_table)
> with(WW_Sample_table, cor.test(P1, P2))

Sarah

On Mon, Dec 19, 2011 at 1:23 AM, Keith Larson <keith.larson at biol.lu.se> wrote:
> Dear list,
>
> I have 9 repeated measures (measurement variable ==  'Delta13C') for
> individuals (ID variable == 'Individual_ID'. Each repeated measure is
> "indexed" (right term?) by the variable 'FeatherPosition' and given as
> c('P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9'). I would like
> to calculate a correlation coefficient (r) and p.value for all
> measures of 'Delta13C' by individual. the function 'cor' only seems to
> work when comparing two individual measures (e.g. P1 and P2, P2 and
> P3, etc.) and only if I restructure my table. Any suggestions:
>
> In SAS with 'proc corr' I would like results that look like:
>
> Individual ID, r, p
> WW_08I_01,-0.03,0.94
> WW_08I_03,0.53,0.14
>
> Trying to get started in R!
> Keith
>
> Sample dataset:
>
> WW_Sample_SI <-
> structure(list(Individual_ID = structure(c(1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("WW_08I_01",
> "WW_08I_03"), class = "factor"), FeatherPosition = structure(c(1L,
> 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
> 9L), .Label = c("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8",
> "P9"), class = "factor"), Delta13C = c(-18.3, -18.53, -19.55,
> -20.18, -20.96, -21.08, -21.5, -17.42, -13.18, -22.3, -22.2,
> -22.18, -22.14, -21.55, -20.85, -23.1, -20.75, -20.9)), .Names =
> c("Individual_ID",
> "FeatherPosition", "Delta13C"), class = "data.frame", row.names = c("1",
> "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
> "14", "15", "16", "17", "18"))
>

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



More information about the R-help mailing list