[R-sig-Finance] MC simulations from R-SIG-Finance Digest, Vol 25, Issue, 17 (Daniele Amberti)

Joe Byers joe-byers at utulsa.edu
Fri Jun 23 18:05:38 CEST 2006


Jens,

I am new to R as you are. I noticed in your code you brute force the lag 
of your data series. 

diffAA<-logAA[1:100]-logAA[2:101]
diffA<-logA[1:100]-logA[2:101]
diffBBB<-logBBB[1:100]-logBBB[2:101]

You might look at the zoo package.  It has some really good functions for time series as well.  For example the lag operator lag.zoo.

My example converts my price data.frame column(variable) gas$price to a zoo object, lags one day,converts it to a vector to get rid of some of the zoo class components, and assigns it to PriceLag1 in my data.frame.  I do some statements looking at daily prices changes +, -, and NA.  The NA is because some time series functions do not handle missing values.  Also, my code is for a technical analysis project I am working on.  In this example I loose one degree of freedom at the beginning of my data series, hence the k=-1.  

#lagging Gas Prices pair today with yesterday hence k=-1 below
gas$PriceLag1<-as.vector(lag.zoo(as.zoo(gas$Price),k=-1,na.pad=TRUE)); # use zoo so can pad on the run
gas$PriceD[which(gas$Price>gas$PriceLag1)]<-1;  # Price_t>Price_t-1 1
gas$PriceD[which(gas$Price<gas$PriceLag1)]<- -1;# Price_t<Price_t-1 -1
gas$PriceD[which(gas$Price==gas$PriceLag1)]<- 0;# Price_t=Price_t-1 0
gas$PriceD[which(gas$PriceLag1==NA)]<- 0;# Handle the lost degrees of freedom

You might try
diffAAA<-logAAA-as.vector(lag.zoo(as.zoo(logAAA),k=-1, na.pad=TRUE));# diff today with the lag one day back

Using the zoo functions alleviates hard coding dimensions, inserting dimension tests, and adds dynamic reusable code functionality.  There also may be more efficient ways to use the zoo package that I have here, but it worked for me.

Good Luck
Joe



-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20060623/6efe5c73/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: joe-byers.vcf
Type: text/x-vcard
Size: 104 bytes
Desc: not available
Url : https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20060623/6efe5c73/attachment.vcf 


More information about the R-SIG-Finance mailing list