tags:

views:

123

answers:

1

I'm creating a time-series object with new variables using the transform() function in R and cannot find the proper function to calculate the difference in variable C between today and yesterday.

This is what I've got so far:

                 O       H       L       C  Typical Range 
2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85 
2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71  
2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17  
2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55  
2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08  
2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43

The next row will be added with the following function:

SPX <- transform(SPX, Return = (C - C(yesterday) ) / C(yesterday)))

Obviously, C(yesterday) is incorrect. I've tried lag(), diff() and haven't found the correct combination.

Bonus question: How do you get the Typical variable to show only to the hundreth?

+1  A: 

The correct function is diff if you're trying to calculate the difference of C between today and yesterday.

> SPX$Return <- diff(SPX$C)
> SPX
                 O       H       L       C  Typical Range Return
2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85     NA
2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71  12.35
2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17  -1.17
2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55  -7.71
2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08  -4.60
2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43   0.07

But it looks like you want to calculate the rate of change instead, which you can do with the ROC function from TTR.

> SPX$Return <- ROC(SPX$C)
> SPX
                 O       H       L       C  Typical Range        Return
2010-07-23 1092.17 1103.73 1087.88 1102.66 1098.090 15.85            NA
2010-07-26 1102.89 1115.01 1101.30 1115.01 1110.440 13.71  1.113793e-02
2010-07-27 1117.36 1120.95 1109.78 1113.84 1114.857 11.17 -1.049869e-03
2010-07-28 1112.84 1114.66 1103.11 1106.13 1107.967 11.55 -6.946068e-03
2010-07-29 1108.07 1115.90 1092.82 1101.53 1103.417 23.08 -4.167314e-03
2010-07-30 1098.44 1106.44 1088.01 1101.60 1098.683 18.43  6.354596e-05
Joshua Ulrich
Thanks Josh, I've never heard of ROC() before. Nice. I was messing around with Delt() from quantmod which appears to do the same thing but was running into the same problems I'm getting with diff() and that is the following:Error in `$<-.data.frame`(`*tmp*`, "Diff", value = c(1.74000000000001, : replacement has 900 rows, data has 901But ROC works fine. Hmmm.
Milktrader
A point of clarification: my example above uses an xts object. The error is probably because Delt and diff don't include the leading NA for the class of object you're using (a data.frame?).
Joshua Ulrich
After doing a str(SPX) it does say data.frame. Hmmm, this is strange as I thought I used quantmod to download the data earlier but I've done so many changes that I lost that part of my history file, so I can't remember. It does explain the error though. I'll try it again making sure I have an xts object
Milktrader
Good news, I was able to find my .Rhistory file intact with how I started this problem and saved it. I started by using getSymbols("^GSPC") to get the data. I assigned to this object the name of SPX and it was at that point an xts object. BUT, when I operated the transform() function on the object, it changed into a plain vanilla data.frame object, which is why I was getting the errors with diff(). All is now well. Thanks Josh, and welcome to stackoverflow.
Milktrader