Return calculation for panel data structureUse diff and lag from plm:

Lines="ticker date_f  date prc iticker 
A 20-Jun-03  15876 19.06 IXT 
A 23-Jun-03  15879 18.89 IXT 
A 24-Jun-03  15880 18.75 IXT 
A 25-Jun-03  15881 18.92 IXT 
A 26-Jun-03  15882 19.35 IXT 
A 27-Jun-03  15883 19.55 IXT 
AA 20-Jun-03 15876 26.24 IXB 
AA 23-Jun-03 15879 25.07 IXB 
AA 24-Jun-03 15880 24.96 IXB 
AA 25-Jun-03 15881 24.51 IXB 
AA 26-Jun-03 15882 25.24 IXB 
AA 27-Jun-03 15883 25.09 IXB 
AA 30-Jun-03 15886 25.5 IXB 
"
z <- read.table(textConnection(Lines), header = TRUE)
library(plm)
zz=pdata.frame(z, c("ticker","date"))
pr=zz$prc; str(pr)
zz$return=diff(pr)/lag(pr)
zz

Good luck!

Robert

From: Arsenio 
Sent: Tuesday, March 06, 2012 2:47 AM
To: r-sig-finance@r-project.org 
Subject: [R-SIG-Finance] Return calculation for panel data structure

Dear all,

I have a very simple question and i haven't been able to code it out. It's a simple return calculation of a form:
R(t)=(P(t)-P(t-1))/P(t-1) or the same as ratio of prices minus one. However it's in the panel data, where stocks belong to say a fund or some other grouping variable.


      ticker date_f 
     date prc iticker 
      A 20-Jun-03 
     15876 19.06 IXT 
      A 23-Jun-03 
     15879 18.89 IXT 
      A 24-Jun-03 
     15880 18.75 IXT 
      A 25-Jun-03 
     15881 18.92 IXT 
      A 26-Jun-03 
     15882 19.35 IXT 
      A 27-Jun-03 
     15883 19.55 IXT 
      AA 20-Jun-03 
     15876 26.24 IXB 
      AA 23-Jun-03 
     15879 25.07 IXB 
      AA 24-Jun-03 
     15880 24.96 IXB 
      AA 25-Jun-03 
     15881 24.51 IXB 
      AA 26-Jun-03 
     15882 25.24 IXB 
      AA 27-Jun-03 
     15883 25.09 IXB 
      AA 30-Jun-03 
     15886 25.5 IXB 


In  SAS, I would do it this way:

data work.1; /* This Calcs the excluded index return for INDEX constructed with PRICES (PR)!!!! */
       set work.1;
       by ticker iticker date_f ; /*sorting by stock, fund and date */

       lag_prc=lag(prc); /* creating the lagged price variable

       if first.iticker then /* if the it's a first date for the grouped unit then.. */
               lag_prc= . ; /* setting the first lagged value to missing if the first date for the grouped unit */
       return = (prc - lag_prc)/ lag_prc;
       drop lag_prc;
run;

The end result would be the same dataset with just one more column for returns for each stock, fund, date combination with the first return for the date set to missing.
I tried Return.calculate type of funcs from performance analytics and other solutions using plyr or loops, but they are all too bulky.

Any ideas how to accomplish this? Would appreciate any hints,

Arsenio 


--------------------------------------------------------------------------------
_______________________________________________
R-SIG-Finance@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
	[[alternative HTML version deleted]]

