0

I have a dataframe with the historical prices of IPC (^MXX), and im trying to make a matrix with lags as columns:

for(i in 1:length(IPC$Close)-1){
  for(l in 1:length(IPC$Close)-1){
    Lags[l,i] <- log(IPC$Close[l+i]-log(IPC$Close[l]))
  }
}

This works but... take so much time. How can i introduce the apply function?

Alex
  • 15
  • 5
  • 1
    Hi Alex, Welcome to Stackoverflow. Please go through [this link](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to give a reproducible example so that it is easy for others to help you. – Ronak Shah Oct 05 '17 at 02:34

3 Answers3

0

I did not understand your formula very well. But if you are trying to compute the matrix of lagged returns this could be a better way, using embed:

# the data
N=10
set.seed(123)
IPC=data.frame(Close=runif(N,10,20))
IPC$ret=c(NA,diff(log(IPC$Close)))
#IPC

# the matrix of lagged returns    
Nlags=2
embed(diff(log(IPC$Close)), Nlags+1)

            [,1]        [,2]        [,3]
[1,]  0.29001164 -0.23840447  0.32850576
[2,]  0.03005332  0.29001164 -0.23840447
[3,] -0.61837953  0.03005332  0.29001164
[4,]  0.37947945 -0.61837953  0.03005332
[5,]  0.21382720  0.37947945 -0.61837953
[6,] -0.19867561  0.21382720  0.37947945
[7,] -0.06306525 -0.19867561  0.21382720
Robert
  • 5,038
  • 1
  • 25
  • 43
0

Asuuming that you want to calculate all the possible lags with sapply/lapply in r

IPC=data.frame(Close=seq(100,120))
# both nested double sapply and outer worked identically in this case
t1 <-sapply(1:length(IPC$Close), function(x) sapply(1:length(IPC$Close),function(y) log(IPC$Close[y])-log(IPC$Close[x])))
t2 <-outer(log(IPC$Close), log(IPC$Close), FUN = "-")

# test case on simplier case
a=seq(1,5)
# both of the function below wll compute all the lags
# sapply, since lapply will output listed which require more processing
sapply(a, function(x) sapply(a, function(y) x-y)) 
outer(a, a, "-") 
# [,1] [,2] [,3] [,4] [,5]
# [1,]    0    1    2    3    4
# [2,]   -1    0    1    2    3
# [3,]   -2   -1    0    1    2
# [4,]   -3   -2   -1    0    1
# [5,]   -4   -3   -2   -1    0

but you should really look into time series(zoo, xts) and its respective functions such as lag() if you are really dealing with stock prices. Although I find it harder to work with sometimes.

MingH
  • 171
  • 1
  • 2
0

Normally one represents financial time series using a time series class such as xts. In that case we can use the xts method of lag like this:

library(quantmod) # also loads xts, zoo and TTR

getSymbols("GOOG") # get GOOG OHLCV data
## [1] "GOOG"

class(GOOG)
## [1] "xts" "zoo"

# take last few rows of GOOG and then display the
# original series closes (lag 0) with 3 lags
lag(tail(Cl(GOOG)), 0:3) 
##            GOOG.Close GOOG.Close.1 GOOG.Close.2 GOOG.Close.3
## 2017-09-26     924.86           NA           NA           NA
## 2017-09-27     944.49       924.86           NA           NA
## 2017-09-28     949.50       944.49       924.86           NA
## 2017-09-29     959.11       949.50       944.49       924.86
## 2017-10-02     953.27       959.11       949.50       944.49
## 2017-10-03     957.79       953.27       959.11       949.50
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341