Search code examples
rregressionquantmod

R quantmod data merge regression error


This R code throws an error, namely

Error in .xts(e, .index(e1), .indexCLASS = indexClass(e1), .indexFORMAT = indexFormat(e1), : index length must match number of observations

Code:

library('quantmod')
library('foreach')


JNK <- getSymbols('JNK', from='2010-01-01',auto.assign=FALSE)[,6]
GSPC <- getSymbols('^GSPC', from='2010-01-01',auto.assign=FALSE)[,6]


JNK <- diff(log(JNK))
GSPC <- diff(log(GSPC))

Data <- na.omit(merge(JNK,GSPC, all=FALSE))
m <- lm(JNK ~ GSPC, data=Data)
plot(m)

Could anyone help me figure out what I'm doing wrong?


Solution

  • The actual column names of Data are JNK.Adjusted and GSPC.Adjusted. Hence, you should specify the complete names in the lm call:

    m <- lm(JNK.Adjusted ~ GSPC.Adjusted, data=Data)
    plot(m)
    

    Otherwise, the plot function will look for the columns JNK and GSPC but will not find them in Data.