如何从雅虎财经数据中删除 NA
How to delete NA from Yahoo Finance Data
我想从雅虎财经检索不同的收盘价。不幸的是,向量具有不同的长度,这也是由于 NA。如何删除这些数据系列以执行回归?
AMZN <- diff(log(tseries::get.hist.quote(instrument="AMZN", start= START_DATE, end=END_DATE, quote=c( "Close"), provider= "yahoo", compression="d", retclass="zoo")))
nrow(AMZN) #250
SDAX <- diff(log(tseries::get.hist.quote(instrument="^SDAXI", start= START_DATE, end=END_DATE, quote=c( "Close"), provider= "yahoo", compression="d", retclass="zoo")))
nrow(SDAX) #254
EURAUD <- diff(log(tseries::get.hist.quote(instrument="EURAUD=X", start= START_DATE, end=END_DATE, quote=c( "Close"), provider= "yahoo", compression="d", retclass="zoo")))
nrow(EURAUD) #260
然后我将各个数据组合成一个向量。由于长度不同,我有 NA 数据。但是NA数据的行必须清理,否则无法进行回归分析。
zDataPreFX <- merge(SDAX, AMZN, EURAUD)
所以如果有任何 NA,这应该可以解决问题,而我选择的 Start/enddate 没有。顺便说一句,您创建的是每日变化率,而不是收盘价。
AMZN <- diff(log(na.omit(tseries::get.hist.quote(instrument="AMZN", start= START_DATE, end=END_DATE, quote=c( "Close"), provider= "yahoo", compression="d", retclass="zoo"))))
nrow(AMZN) #250
SDAX <- diff(log(na.omit(tseries::get.hist.quote(instrument="^SDAXI", start= START_DATE, end=END_DATE, quote=c( "Close"), provider= "yahoo", compression="d", retclass="zoo"))))
nrow(SDAX) #254
EURAUD <- diff(log(na.omit(tseries::get.hist.quote(instrument="EURAUD=X", start= START_DATE, end=END_DATE, quote=c( "Close"), provider= "yahoo", compression="d", retclass="zoo"))))
nrow(EURAUD) #260
您可以通过合并合并所有数据。然后使用 na.omit
删除其中包含 NA 值的行。请参阅下面的代码示例。
start_date = "2021-01-01"
end_date="2021-12-31"
AMZN <- diff(log(tseries::get.hist.quote(instrument = "AMZN",
start = start_date,
end = end_date,
quote = c("Close"),
provider = "yahoo",
compression ="d",
retclass="zoo")))
SDAX <- diff(log(tseries::get.hist.quote(instrument = "^SDAXI",
start = start_date,
end = end_date,
quote=c("Close"),
provider= "yahoo",
compression="d",
retclass="zoo")))
EURAUD <- diff(log(tseries::get.hist.quote(instrument = "EURAUD=X",
start = start_date,
end = end_date,
quote=c("Close"),
provider= "yahoo",
compression="d",
retclass="zoo")))
all <- merge(AMZN, SDAX, EURAUD)
head(all)
Close.AMZN Close.SDAX Close.EURAUD
2021-01-04 NA NA 0.0251187798
2021-01-05 0.009954627 0.003818769 0.0056628563
2021-01-06 -0.025211817 0.013925576 -0.0083800866
2021-01-07 0.007548605 0.010638004 -0.0038076972
2021-01-08 0.006474567 -0.002391856 0.0008108246
2021-01-11 -0.021754382 -0.009921183 -0.0001139825
all_cleaned <- na.omit(all)
head(all_cleaned)
Close.AMZN Close.SDAX Close.EURAUD
2021-01-05 0.009954627 0.003818769 0.0056628563
2021-01-06 -0.025211817 0.013925576 -0.0083800866
2021-01-07 0.007548605 0.010638004 -0.0038076972
2021-01-08 0.006474567 -0.002391856 0.0008108246
2021-01-11 -0.021754382 -0.009921183 -0.0001139825
2021-01-12 0.002123521 0.009845704 -0.0008616212
我想从雅虎财经检索不同的收盘价。不幸的是,向量具有不同的长度,这也是由于 NA。如何删除这些数据系列以执行回归?
AMZN <- diff(log(tseries::get.hist.quote(instrument="AMZN", start= START_DATE, end=END_DATE, quote=c( "Close"), provider= "yahoo", compression="d", retclass="zoo")))
nrow(AMZN) #250
SDAX <- diff(log(tseries::get.hist.quote(instrument="^SDAXI", start= START_DATE, end=END_DATE, quote=c( "Close"), provider= "yahoo", compression="d", retclass="zoo")))
nrow(SDAX) #254
EURAUD <- diff(log(tseries::get.hist.quote(instrument="EURAUD=X", start= START_DATE, end=END_DATE, quote=c( "Close"), provider= "yahoo", compression="d", retclass="zoo")))
nrow(EURAUD) #260
然后我将各个数据组合成一个向量。由于长度不同,我有 NA 数据。但是NA数据的行必须清理,否则无法进行回归分析。
zDataPreFX <- merge(SDAX, AMZN, EURAUD)
所以如果有任何 NA,这应该可以解决问题,而我选择的 Start/enddate 没有。顺便说一句,您创建的是每日变化率,而不是收盘价。
AMZN <- diff(log(na.omit(tseries::get.hist.quote(instrument="AMZN", start= START_DATE, end=END_DATE, quote=c( "Close"), provider= "yahoo", compression="d", retclass="zoo"))))
nrow(AMZN) #250
SDAX <- diff(log(na.omit(tseries::get.hist.quote(instrument="^SDAXI", start= START_DATE, end=END_DATE, quote=c( "Close"), provider= "yahoo", compression="d", retclass="zoo"))))
nrow(SDAX) #254
EURAUD <- diff(log(na.omit(tseries::get.hist.quote(instrument="EURAUD=X", start= START_DATE, end=END_DATE, quote=c( "Close"), provider= "yahoo", compression="d", retclass="zoo"))))
nrow(EURAUD) #260
您可以通过合并合并所有数据。然后使用 na.omit
删除其中包含 NA 值的行。请参阅下面的代码示例。
start_date = "2021-01-01"
end_date="2021-12-31"
AMZN <- diff(log(tseries::get.hist.quote(instrument = "AMZN",
start = start_date,
end = end_date,
quote = c("Close"),
provider = "yahoo",
compression ="d",
retclass="zoo")))
SDAX <- diff(log(tseries::get.hist.quote(instrument = "^SDAXI",
start = start_date,
end = end_date,
quote=c("Close"),
provider= "yahoo",
compression="d",
retclass="zoo")))
EURAUD <- diff(log(tseries::get.hist.quote(instrument = "EURAUD=X",
start = start_date,
end = end_date,
quote=c("Close"),
provider= "yahoo",
compression="d",
retclass="zoo")))
all <- merge(AMZN, SDAX, EURAUD)
head(all)
Close.AMZN Close.SDAX Close.EURAUD
2021-01-04 NA NA 0.0251187798
2021-01-05 0.009954627 0.003818769 0.0056628563
2021-01-06 -0.025211817 0.013925576 -0.0083800866
2021-01-07 0.007548605 0.010638004 -0.0038076972
2021-01-08 0.006474567 -0.002391856 0.0008108246
2021-01-11 -0.021754382 -0.009921183 -0.0001139825
all_cleaned <- na.omit(all)
head(all_cleaned)
Close.AMZN Close.SDAX Close.EURAUD
2021-01-05 0.009954627 0.003818769 0.0056628563
2021-01-06 -0.025211817 0.013925576 -0.0083800866
2021-01-07 0.007548605 0.010638004 -0.0038076972
2021-01-08 0.006474567 -0.002391856 0.0008108246
2021-01-11 -0.021754382 -0.009921183 -0.0001139825
2021-01-12 0.002123521 0.009845704 -0.0008616212