Skip to content
Prev 146306 / 398500 Next

R: Securities earning covariance

It works perfectly, thank you so much.
Now I will try to put teh results into a suitable form (a data frame like this):

SEC_ID.x	SEC_ID.y	EARN_COV

Thank you again

Angelo Linardi

-----Messaggio originale-----
Da: Patrick Burns [mailto:pburns at pburns.seanet.com] 
Inviato: gioved? 5 giugno 2008 18.11
A: LINARDI ANGELO
Cc: r-help at r-project.org
Oggetto: Re: [R] Securities earning covariance

I would start by creating a matrix that held the returns with rows being the dates and columns being the securities.  You can do this by something along the lines of:

days <- as.character(df[, 'DAY'])
sec <- as.character(df[, 'SEC_ID']
earningmat <- array(NA, c(length(unique(days)),
   length(unique(sec))), list(sort(unique(days)),
   unique(sec)))
submat <- cbind(match(days, rownames(earningmat)),
    match(sec, colnames(earningmat)))
earningmat[submat] <- as.numeric(as.character(df[, 'EARNING']))

Notice that while the 'as.numeric-as.character' in the last line may not be needed -- if it is needed, it is needed in a big way.  If the 'EARNING' column is a factor (because there was at least one item that didn't appear to be numeric when it was read in), then skipping the 'as.numeric-as.character'
call will put the codes for the factor into the matrix.  It will be numeric as you expect, but complete garbage.

The trick with 'submat' is explained in any complete description of subscripting -- the subscripting section of Chapter 1 of S Poetry, for instance.

Once you have a suitable matrix, then you can use 'var' or some other function to get the variance matrix.  Depending on where you are going, a factor model variance may be better.
You can get 'factor.model.stat' from the public domain area of the Burns Statistics website.  This is especially useful if there are missing values in your matrix.


Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")
ANGELO.LINARDI at bancaditalia.it wrote:
** Le e-mail provenienti dalla Banca d'Italia sono trasmesse in buona fede e non 
comportano alcun vincolo ne' creano obblighi per la Banca stessa, salvo che cio' non 
sia espressamente previsto da un accordo scritto.
Questa e-mail e' confidenziale. Qualora l'avesse ricevuta per errore, La preghiamo di 
comunicarne via e-mail la ricezione al mittente e di distruggerne il contenuto. La 
informiamo inoltre che l'utilizzo non autorizzato del messaggio o dei suoi allegati 
potrebbe costituire reato. Grazie per la collaborazione.
-- E-mails from the Bank of Italy are sent in good faith but they are neither binding on 
the Bank nor to be understood as creating any obligation on its part except where 
provided for in a written agreement. This e-mail is confidential. If you have received it 
by mistake, please inform the sender by reply e-mail and delete it from your system. 
Please also note that the unauthorized disclosure or use of the message or any 
attachments could be an offence. Thank you for your cooperation. **