每天转移" data.frame"到" ts"在R

时间:2016-11-29 06:18:51

标签: r time-series

我有一个数据集"率"如下:

       Date  Price
1 2012-11-01 6.2411
2 2012-11-02 6.2415
3 2012-11-05 6.2454
4 2012-11-06 6.2456
5 2012-11-07 6.2437
6 2012-11-08 6.2429

> class(rate)
[1] "data.frame"

尝试使用R代码将此数据集传输到时间序列数据:

rate<-ts(data = rate, start =1, freq=1)
> class(rate)
[1] "mts"    "ts"     "matrix"

> head(rate)
     Date  Price
[1,] 15645 6.2411
[2,] 15646 6.2415
[3,] 15649 6.2454
[4,] 15650 6.2456
[5,] 15651 6.2437
[6,] 15652 6.2429

如您所见,日期变为数字。因此我使用as.date()函数:

rate[,1] <- as.Date(rate[,1],origin = "1899-12-30")
> head(rate)
       Date  Price
[1,] -719162 6.2411
[2,] -718797 6.2415
[3,] -718432 6.2454
[4,] -718067 6.2456
[5,] -717701 6.2437
[6,] -717336 6.2429

有没有人可以帮我解决这个问题?谢谢。

2 个答案:

答案 0 :(得分:1)

您的代码存在的问题是您尝试将日期列转换为之后输入日期,您将整个数据框强制转换为时间序列矩阵。这样做的正确方法是首先转换&#34;日期&#34;键入日期,按照从开始年份(2012年)开始的每日增量计算开始日期,然后使用该信息转换&#34; Price&#34;列到时间序列。

# Here is your data in "dput" form
rate = structure(list(Date = c("2012-11-01", "2012-11-02", "2012-11-05", 
                           "2012-11-06", "2012-11-07", "2012-11-08"), 
                  Price = c(6.2411, 6.2415, 6.2454, 6.2456, 6.2437, 6.2429)), 
             .Names = c("Date", "Price"), class = "data.frame", row.names = c(NA, -6L))

# Convert Date column to type "Date"
rate$Date = as.Date(rate$Date, format = "%Y-%m-%d")

# Convert "11-01" to day of the year 
dayOfYear = as.numeric(format(rate[1,1], "%j"))

# Use 2012 and dayOfYear as starting date
rate_ts = ts(rate$Price, start = c(2012, dayOfYear), frequency = 365)

> class(rate_ts)
[1] "ts"

> rate_ts
Time Series:
Start = c(2012, 306) 
End = c(2012, 311) 
Frequency = 365 
[1] 6.2411 6.2415 6.2454 6.2456 6.2437 6.2429

此处,"%j"只是告诉format.Date函数将Date(2012-11-01)的第一个元素转换为一年中的某一天。

我还想指出,由于你的ts是每天,你应该使用frequency = 365代替frequency = 1

绘图

# Plot time series without x-axis
plot(rate_ts, ylab = "Price", xaxt = "n")

# Extract first and last date value of rate_ts
tsp = attributes(rate_ts)$tsp

# Plot x-axis
axis(1, at = seq(tsp[1], tsp[2], along = rate_ts), 
     labels = format(rate$Date, "%Y-%m-%d"))

最后一行允许您通过更改format()的第二个参数来格式化x轴。 at =参数允许您指定刻度。

enter image description here

Jake Burkhead对this answer

中绘图方法的认可

答案 1 :(得分:0)

df <- data.frame(c(6.2411, 6.2415, 6.2454, 6.2456, 6.2437, 6.2429))
names(df) <- "price"
rownames(df) <- c("2012-11-01", "2012-11-02", "2012-11-05", "2012-11-06", "2012-11-07", "2012-11-08")
df
#            price
# 2012-11-01 6.2411
# 2012-11-02 6.2415
# 2012-11-05 6.2454
# 2012-11-06 6.2456
# 2012-11-07 6.2437
# 2012-11-08 6.2429

您正在尝试分配不定期天。因此,lubridate可能不会背诵您的问题。当您使用df[,1]进行需要ts对象的进一步操作时,df[,1]将自动强制到此类。您可以使用df[,1]执行任何分析。

请注意: 观察点不均匀(不均匀/不规则)间隔(国家假日等可能导致观察数量的差异)。当考虑多个系列以及价格时,您可能会想到交叉点。在这种情况下你应该忽略不规则性的影响,因为对于许多观测,观测的间距是相同的,因此不是那么高度不规则,并且还考虑到使用线性插值将数据转换为等间距观测的事实可以引入一些重要且难以量化的偏见(见:斯科尔斯和威廉姆斯)。

M. Scholes and J. Williams, “Estimating betas from nonsynchronous data”, Journal of Financial Economics 5: 309–327, 1977.

对Excel2016的推广:
假设您在Excel文件(DatePrice.xlsx)中有数千个日期和价格数据:

   A   B    
1 Date Price    
2 2012-11-01 6.2411
3 2012-11-02 6.2415
...

然后,执行以下操作:

library(readxl)
# Use the path returned from getwd() function that is R's working directory
df <- as.data.frame(read_excel("C://Users//User//Documents//Revolution//DatePrice.xlsx"))
names(df) <- c("date","price")
rownames(df) <- df[,1]
df[,1] <- NULL
df

同样,df[,1]将是在任何进一步分析中用于强制操作的时间序列。例如;

如何继续解决方案的示例性分析:

price <- df[,1]
plot(ts(price)); abline(a=mean(ts(price)), b=0) # graphically, price~ I(1)

#Stationarity analysis (even with 6 obs, it produces results!)
library(fUnitRoots); unitrootTest(price) # formally, price~ I(1)  p=0.6889

plot(diff(ts(price), differences=1)) # graphically, Delta(price) ~ I(0)
unitrootTest(diff(ts(price), differences=1)) # formally, Delta(price) ~ I(0) p=1e-04<0.05