user3509579
user3509579

Reputation: 21

Correlation matrix

I have a dataframe has the daily returns of around 20 companies over a period of 7 years. I am trying to calculation Ri*Rj for all companies for each date.

I am not sure of how to upload my data set here. I am listing a few example entries for the general format. (The dataframe is ordered by date using orderBy function) :

Company.Name     Date      Closing.price      Prev.Closing      r        
ABB           2002-08-12       24.16               24.78       0.02
ABAN          2002-08-12       172.5               179.5       0.12
ASHOK         2002-08-12       39.12               36.42       0.15
..
..
ABB           2002-08-13       25.6                24.16       0.21
ABAN          2002-08-13       175.7               172.5       0.02

I tried the following function however, i get the following error :

 Error in FUN(left, right) : non-numeric argument to binary operator 


correlation <- function(x)
{
  y <- matrix(NA, nrow=length(x), ncol=length(x))
  for(i in 1:length(x))
  {
     if(i!=i-1) {j=i}
    for(j in 1:(length(x)-1))  
    {
      y[i,j] <- x[i]*x[j]
    }
  }
}



 correlations <- aggregate(Companies_NSE$r, list(Companies_NSE$Date),
         FUN= correlation(x))

Upvotes: 1

Views: 369

Answers (1)

akrun
akrun

Reputation: 887901

You could try:

 library(matrixStats)
 with(df, aggregate(r, list(Date=Date), FUN= function(x) colProds(combn(x,2))))
 #        Date                      x
 #1 2002-08-12 0.0024, 0.0030, 0.0180
 #2 2002-08-13                 0.0042

Or using data.table

 library(data.table)
 setDT(df)[, list(Prod=colProds(combn(r,2))), by=Date]
 #         Date   Prod
 #1: 2002-08-12 0.0024
 #2: 2002-08-12 0.0030
 #3: 2002-08-12 0.0180
 #4: 2002-08-13 0.0042

Data

df <- structure(list(Company.Name = c("ABB", "ABAN", "ASHOK", "ABB", 
"ABAN"), Date = c("2002-08-12", "2002-08-12", "2002-08-12", "2002-08-13", 
"2002-08-13"), Closing.price = c(24.16, 172.5, 39.12, 25.6, 175.7
), Prev.Closing = c(24.78, 179.5, 36.42, 24.16, 172.5), r = c(0.02, 
0.12, 0.15, 0.21, 0.02)), .Names = c("Company.Name", "Date", 
"Closing.price", "Prev.Closing", "r"), class = "data.frame", row.names = c(NA, 
-5L))

Upvotes: 2

Related Questions