[R] Compute correlation matrix for panel data with specific ordering
Serguei Kaniovski
Serguei.Kaniovski at wifo.ac.at
Mon Jun 29 13:20:54 CEST 2009
I apologize for not being specific enough in my previous posting. Assume
you have panel data in the form:
df <- data.frame( cbind( rep( c( "AUT" , "BEL" , "DEN" , "GER" ) , 4) ,
cbind( rep( c( 1999 , 2000 , 2001 , 2002 ) , 4 ) ), sample( 10 , 16 ,
replace=T) ) )
names(df) <- c( "country" , "year" , "x" )
1. I would like to compute the correlation matrix between countries
based on the annual observations of the variable x. I tried the following:
library( combinat )
temp <- split( df$x, df$year )
apply( combn(4,2) , 2 , function(x) cor( temp[[1]] , temp[[2]] ) )
This gives wrong answer. Why?
2. The pairwise correlations computed as above should be in the order:
GER with BEL, GER with DEN, GER with AUT, BEL with DEN, BEL with AUT,
DEN with AUT.
That is, the correctly sorted vector of factors is:
SORT <- c( "GER" , "BEL" , "DEN" , "AUT" ) not c( "AUT" , "BEL" , "DEN"
, "GER" )
May be there is an altogether better way of achieving what I want?
Serge
More information about the R-help
mailing list