简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM