I am an R newbie, and I am facing what is for me a challenging problem. I am trying to write functions to solve the following problems. It seems as though R offers so many options I am not sure how to approach the problem. 

 

I have a data frame  with dates in the first column and returns in the remaining columns. I would like to give a user a variety of options.

 

I designate Series1 as the base security. I want to find the correlation for Series1 with every other security, but I want to do so:

 

1.      By seasons and years

2.      By seasons only

 

A third problem is a standard correlation matrix of all series but the output is by season and year.

 

Typically the season is quarters. But it would be nice if the user could designate the endpoints of each season.

 

In output 1 below, the correlations for 1Q2008 are computed by just using the data that is within 2008-01-01 to 2008-03-31. In output 2, there are only four rows of outputs, one for each quarter.

 

In output 3, the function would subset each quarter of each year and then use a standard correlation.

 

The input

 

> head(ReturnsAll)[,1:4]

       Dates   Series1   Series2   Series3

1 2008-01-03 -0.002 -0.002 -0.002

2 2008-01-04 -0.008 -0.009 -0.009

3 2008-01-07 -0.025 -0.024 -0.024

4 2008-01-08  0.012  0.012  0.012

5 2008-01-09 -0.012 -0.014 -0.015

6 2008-01-10 -0.023 -0.022 -0.021

> tail(ReturnsAll)[,1:4]

         Dates   Series1   Series2   Series3

850 2011-05-18  0.021  0.021  0.021

851 2011-05-19 -0.008 -0.008 -0.008

852 2011-05-20  0.009  0.009  0.009

853 2011-05-23 -0.020 -0.020 -0.020

854 2011-05-24  0.022  0.022  0.021

855 2011-05-25  0.021  0.021  0.021

 

 

Here is how the output matrices would look.

      Correlation between series 1 and all others
     
      By quarter and by year
      
      Date
     Series1
     Series2
     Series3
     
      1Q 2008
     1.00
     0.28
     0.11
     
      2Q 2008
     1.00
     -0.07
     -0.07
     
      3Q 2008
     1.00
     -0.08
     -0.32
     
      4Q 2008
     1.00
     0.16
     -0.34
     
      1Q 2009
     1.00
     0.09
     0.04
     
        
      Output 2
        
      Correlation between series 1 and all others
     
      By quarter only
       
      Date
     Series1
     Series2
     Series3
     
      1Q
     1.00
     -0.06
     0.58
     
      2Q
     1.00
     0.25
     0.48
     
      3Q
     1.00
     0.52
     0.59
     
      4Q
     1.00
     0.54
     0.11
     
        
      Output 3
        
      All correlations
       
      By quarter and by year
      
      Q1 2008
     Series1
     Series2
     Series3
     
      Series1
     1.00
     0.78
     0.73
     
      Series2
     0.78
     1.00
     0.98
     
      Series3
     0.73
     0.98
     1.00
     
        
      Q4 2010
     Series1
     Series2
     Series3
     
      Series1
     1.00
     0.97
     0.95
     
      Series2
     0.97
     1.00
     1.00
     
      Series3
     0.95
     1.00
     1.00
     

 

Thanks.



Ira

	[[alternative HTML version deleted]]

