# Multivariate values to acf() in R

I am trying to understand the concept of auto correlation and I am looking for some help to clear some doubts regarding my data.

I have a time series data and it has repeated experiments. Each sample has 4 time points and corresponding values for the genes under study. I have used ind to represent each sample in my data. So ind 1 means rat which is studied over 4 time points and the samples are in rows. I have 400 genes and corresponding values for each sample and each time point. The genes are in columns.

my dataset: M1

No      ind  tme      gene_1      gene_2      gene_3      gene_4      gene_5  gene_7

A1T1:2  1   -64 0.0307  0.0022  0.0010  0.0001  0.0007  0.0035

A1T2:2  1   8   0.0365  0.0031  0.0003  0.0002  0.0009  0.0043

A1T3:1  1   48  0.0182  0.0014  0.0001  0.0001  0.0005  0.0018

A1T4:1  1   96  0.0134  0.0010  0.0001  0.0001  0.0003  0.0015

A2T1:1  2   -64 0.0387  0.0032  0.0003  0.0002  0.0010  0.0051

A2T2:1  2   8   0.0264  0.0022  0.0010  0.0001  0.0007  0.0032

A2T3:1  2   48  0.0205  0.0017  0.0002  0.0001  0.0005  0.0022

A2T4:1  2   96  0.0161  0.0012  0.0001  0.0001  0.0004  0.0018


What I want to do is identify is simple: if there is autocorrelation in my data?

my code:

s1<-read.table("M1.txt", sep=" ",header=T)
s2<-s1[1:4,1]
s2.v<-as.vector(s2)
acf(s2.v)


I get a plot with 4 sub plots. I do not understand how to interpret the result. Also Can I give all the genes as input (for sample 1) to identify autocorrelation?

I am new to these concepts hence my understanding is basic. Thanking you for all the help.

• If your time series is 4 periods long, then autocorrelation cannot be reliably estimated. Jan 2, 2013 at 7:26
• @mpiktas- thanks for editing my post. Are you suggesting that I need a dataset with more time points or lesser time points? any specific reason why you say for 4 time points, autocorrelation cannot be estimated well?
– Aps
Jan 2, 2013 at 8:06
• You need more time points. The autocorrelation is still a correlation, so the same rules apply. You would not estimate correlation between two variables when you have only 4 data points. You can, but you won't be able to say much. Jan 2, 2013 at 8:10
• Perhaps a useful exercise would be to generate a set of random values and look for autocorrelation. Then generate a set of values based on a sine wave and look for autocorrelation. Jan 2, 2013 at 10:50

Autocorrelation tells you the net correlation between numeric vectors at different lags, or offsets. You can check for autocorrelation across all genes, but I don't think that's what you want to do. If you do, it's as simple as this (warning, this will produce tail(cumsum(1:400),n=1) plots):

s1 <- read.table("M1.txt", sep=" ",header=T)
df <- s1[,4:404]
acf(as.matrix(df))


I think maybe what you want to do here is calculate a correlation matrix:

cor(as.data.frame(s1))


Rather than the above 80,200 plots, this produces one correlation matrix, albeit without the lags. I don't know what the tme value means, but I assume this is not a timeseries or spatial data, so you don't want autocorrelation anyway. If you want cross-correlation only, you will need to use the ccf function two univariate series at a time, in order to begin to get at something called spectral coherence.

I also posted an answer to another question with a way to efficiently apply functions to each unique combination of columns:

combinations <- combn(colnames(df),2,function(x) cor(df[x],df[x]))