gpt4 book ai didi

R:如何通过前一天的信息改变证券交易所日指数时间序列的缺口(假期)?

转载 作者:行者123 更新时间:2023-12-04 19:04:10 27 4
gpt4 key购买 nike

我使用 R 语言并处理来自不同国家的时间序列每日股票指数。为了在不同指标之间进行比较(例如相关性、因果关系等),我需要所有系列都具有相同的行数,但是由于不同国家/地区的不同假期,每个系列中的行数会发生变化。

我正在处理从雅虎财经中提取的文件,格式为 .csv,例如...

> head(sp)
> Date Open High Low Close Volume Adj.Close
>1288 2010-01-04 1116.56 1133.87 1116.56 1132.99 3991400000 1132.99
>1287 2010-01-05 1132.66 1136.63 1129.66 1136.52 2491020000 1136.52
>1286 2010-01-06 1135.71 1139.19 1133.95 1137.14 4972660000 1137.14

我需要...例如,假设 2010-01-07 天是假期,在这种情况下,文件中的下一行(第 1285 行)是 2010-01-08 天:
> head(sp)
> Date Open High Low Close Volume Adj.Close
>1288 2010-01-04 1116.56 1133.87 1116.56 1132.99 3991400000 1132.99
>1287 2010-01-05 1132.66 1136.63 1129.66 1136.52 2491020000 1136.52
>1286 2010-01-06 1135.71 1139.19 1133.95 1137.14 4972660000 1137.14
>1285 2010-01-08 1140.52 1145.39 1136.22 1144.98 4389590000 1144.98

需要用前一天的数据填补 2010-01-07 的空白,例如:
> head(sp)
> Date Open High Low Close Volume Adj.Close
>1288 2010-01-04 1116.56 1133.87 1116.56 1132.99 3991400000 1132.99
>1287 2010-01-05 1132.66 1136.63 1129.66 1136.52 2491020000 1136.52
>1286 2010-01-06 1135.71 1139.19 1133.95 1137.14 4972660000 1137.14
>1285 2010-01-07 1135.71 1139.19 1133.95 1137.14 4972660000 1137.14
>1284 2010-01-08 1140.52 1145.39 1136.22 1144.98 4389590000 1144.98

我怎么能做到这一点???

我的代码是(查看我尝试使用的所有库来解决我的问题 kkk)
>library(PerformanceAnalytics)
>library(tseries)
>library(urca)
>library(zoo)
>library(lmtest)
>library(timeDate)
>library(timeSeries)

>setwd("C:/Users/Fatima/Documents/R")

>sp = read.csv("SP500.csv", header = TRUE, stringsAsFactors = FALSE)
>sp$Date = as.Date(sp$Date)
>sp = sp[order(sp$Date), ]

抱歉我的英语不好

最佳答案

包 xts 在这里很有用:

DF <- read.table(text = "           Date    Open    High     Low   Close     Volume Adj.Close
1288 2010-01-04 1116.56 1133.87 1116.56 1132.99 3991400000 1132.99
1287 2010-01-05 1132.66 1136.63 1129.66 1136.52 2491020000 1136.52
1286 2010-01-06 1135.71 1139.19 1133.95 1137.14 4972660000 1137.14
1285 2010-01-08 1140.52 1145.39 1136.22 1144.98 4389590000 1144.98", header = TRUE)

DF$Date <- as.Date(DF$Date)

library(xts)
X <- as.xts(DF[,-1], order.by = DF$Date)
na.locf(merge(X, seq(min(DF$Date), max(DF$Date), by = 1)))
# Open High Low Close Volume Adj.Close
#2010-01-04 1116.56 1133.87 1116.56 1132.99 3991400000 1132.99
#2010-01-05 1132.66 1136.63 1129.66 1136.52 2491020000 1136.52
#2010-01-06 1135.71 1139.19 1133.95 1137.14 4972660000 1137.14
#2010-01-07 1135.71 1139.19 1133.95 1137.14 4972660000 1137.14
#2010-01-08 1140.52 1145.39 1136.22 1144.98 4389590000 1144.98

编辑:

回应您的评论:您可以像这样排除周末:
dates <- seq(min(DF$Date), max(DF$Date), by = 1)
#you might have to adjust the following to the translations in your locale
dates <- dates[!(weekdays(dates) %in% c("Saturday", "Sunday"))]
na.locf(merge(X, dates))

关于R:如何通过前一天的信息改变证券交易所日指数时间序列的缺口(假期)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29145528/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com