如何将具有多个变量的POSIXct数据转换为时间序列格式
How to transfer POSIXct data with multipel variables into time series format
我有多个变量的数据。我的数据str
如下:
tibble [2,859 × 92] (S3: tbl_df/tbl/data.frame)
$ Date : POSIXct[1:2859], format: "2010-04-01" "2010-04-
02" "2010-04-05" "2010-04-06" ...
$ Num : num [1:2859] 1 2 3 4 5 6 7 8 9 10 ...
$ Price : num [1:2859] 3158 3158 3159 3148 3119 ... `
我想将数据转换成时间序列格式。我尝试了一些解决方案但没有奏效(例如,How can I transform a dataframe with POSIXct dates into a time series? 我收到此错误: DF <- data.frame(FinDat = date, FinDat$Price = sample(100, length(date), TRUE)) Error: unexpected '=' in "DF <- data.frame(FinDat = date, FinDat$Price ="
)
我的数据示例是:
structure(list(Date = structure(c(1270080000, 1270166400, 1270425600,
1270512000, 1270598400, 1270684800, 1270771200, 1271030400, 1271116800,
1271203200), tzone = "UTC", class = c("POSIXct", "POSIXt")),
Price = c(3157.957, 3157.957, 3158.681, 3148.222, 3118.709,
3145.347, 3129.263, 3161.251, 3166.183, 3164.966)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
我想使用 autoplot
函数和 auto.arima
发帖者在评论中表示他们想要绘制序列并使用 arima(使用 ts),因此我们需要将其转换为规则间隔的序列。将其转换为动物园,然后将时间转换为年 + 分数,其中分数 = 0、1/N、2/N、...、(N-1)/N,其中 N 是每年给出 tt 的最大点数。我们可以将它与 arima 一起使用。我们将需要至少 2 年的数据来执行某些分析,尽管下面的代码可以使用更少的数据。
library(zoo)
z <- read.zoo(dat)
plot(z) # or use autoplot(z) with ggplot2 or xyplot(z) with lattice
zz <- z
yr <- as.integer(as.yearmon(time(zz)))
N <- max(table(yr))
time(zz) <- yr + (ave(yr, yr, FUN = seq_along) - 1) / N
tt <- as.ts(zz)
tt
## Time Series:
## Start = c(2010, 1)
## End = c(2010, 10)
## Frequency = 10
## [1] 3157.957 3157.957 3158.681 3148.222 3118.709 3145.347 3129.263 3161.251
## [9] 3166.183 3164.966
我有多个变量的数据。我的数据str
如下:
tibble [2,859 × 92] (S3: tbl_df/tbl/data.frame)
$ Date : POSIXct[1:2859], format: "2010-04-01" "2010-04-
02" "2010-04-05" "2010-04-06" ...
$ Num : num [1:2859] 1 2 3 4 5 6 7 8 9 10 ...
$ Price : num [1:2859] 3158 3158 3159 3148 3119 ... `
我想将数据转换成时间序列格式。我尝试了一些解决方案但没有奏效(例如,How can I transform a dataframe with POSIXct dates into a time series? 我收到此错误: DF <- data.frame(FinDat = date, FinDat$Price = sample(100, length(date), TRUE)) Error: unexpected '=' in "DF <- data.frame(FinDat = date, FinDat$Price ="
)
我的数据示例是:
structure(list(Date = structure(c(1270080000, 1270166400, 1270425600,
1270512000, 1270598400, 1270684800, 1270771200, 1271030400, 1271116800,
1271203200), tzone = "UTC", class = c("POSIXct", "POSIXt")),
Price = c(3157.957, 3157.957, 3158.681, 3148.222, 3118.709,
3145.347, 3129.263, 3161.251, 3166.183, 3164.966)), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
我想使用 autoplot
函数和 auto.arima
发帖者在评论中表示他们想要绘制序列并使用 arima(使用 ts),因此我们需要将其转换为规则间隔的序列。将其转换为动物园,然后将时间转换为年 + 分数,其中分数 = 0、1/N、2/N、...、(N-1)/N,其中 N 是每年给出 tt 的最大点数。我们可以将它与 arima 一起使用。我们将需要至少 2 年的数据来执行某些分析,尽管下面的代码可以使用更少的数据。
library(zoo)
z <- read.zoo(dat)
plot(z) # or use autoplot(z) with ggplot2 or xyplot(z) with lattice
zz <- z
yr <- as.integer(as.yearmon(time(zz)))
N <- max(table(yr))
time(zz) <- yr + (ave(yr, yr, FUN = seq_along) - 1) / N
tt <- as.ts(zz)
tt
## Time Series:
## Start = c(2010, 1)
## End = c(2010, 10)
## Frequency = 10
## [1] 3157.957 3157.957 3158.681 3148.222 3118.709 3145.347 3129.263 3161.251
## [9] 3166.183 3164.966