constantine
constantine

Reputation: 11

Autocorrelation of multiple columns in R

Hello guys I am a newbie to R

I am working on stock data that consists of price and return of stock index on columns. I would like to get the all the autocorrelation of each stock index return.

I tried using the following code but the results is not what I wanted. It includes extra information that I do not need, like the correlation of different stock index return

acf(data[ , 10:17] , lag.max = 10, type = "correlation", pl=F , na.action = na.exclude)

The result that I want is something like

acf(data$TSX_Rt, lag.max = 10, type = "correlation", pl=F , na.action = na.exclude)
acf(data$CAC_Rt, lag.max = 10, type = "correlation", pl=F , na.action = na.exclude)
acf(data$DAX_Rt, lag.max = 10, type = "correlation", pl=F , na.action = na.exclude)
acf(data$Eurostoxx50_Rt, lag.max = 10, type = "correlation", pl=F , na.action = na.exclude)
acf(data$NIKKEI225_Rt, lag.max = 10, type = "correlation", pl=F , na.action = na.exclude)
acf(data$FTSE100_Rt, lag.max = 10, type = "correlation", pl=F , na.action = na.exclude)
acf(data$SP500_Rt, lag.max = 10, type = "correlation", pl=F , na.action = na.exclude)
acf(data$IBOVESPA_Rt, lag.max = 10, type = "correlation", pl=F , na.action = na.exclude)

I would like to know if there are any ways that I can get this result faster and easier without repeating so many lines.

Thank you

Upvotes: 1

Views: 210

Answers (1)

Duck
Duck

Reputation: 39585

Maybe a loop can help:

#Loop
for(i in 10:17)
{
  acf(data[ ,i] ,
      lag.max = 10,
      type = "correlation",
      pl=F ,
      na.action = na.exclude)
}

Upvotes: 1

Related Questions