xts::apply 错误:"Error in coredata.xts(x) : currently unsupported data type"
Error with xts::apply: "Error in coredata.xts(x) : currently unsupported data type"
我在尝试执行以下工作时发生错误:
# generate random integrals #
data <- xts(floor(runif(100, 1,101)),as.Date("1973-02-01") + c(1:100) - 1)
apply.monthly(data, diff,1,1)
,而这个有效:
apply.monthly(data,mean)
我查过类似的问题,但似乎不适用于这里的情况。
有什么建议吗?
一些进一步的解释:
我需要这个的原因是我得到了如下的时间序列数据集,
1990-05 100
1990-04 80
1990-03 60
1990-02 20
1990-01 5
1989-12 110
1989-11 89
1989-10 78
...
每一年,y(t)=y_(t-1)+dy
,其中dt
是t期的数值变化。但这种模式只发生在每一年,而且每年都是分开的。所以基本上,我想检索每个特定年份的每个月之间的差异,即:
1990-05 20 #100-80
1990-04 20 #80-60
1990-03 40 #60-20
1990-02 15 #20-5
1990-01 5 #5
1989-12 21 #110-89
1989-11 11 #89-78
...
希望我已经解释清楚了。
谢谢,
apply.monthly
和period.apply
用于将数据聚合到指定的时间段。 diff
不起作用,因为 diff.xts
returns 一个与输入长度相同的向量。 mean
有效,因为它 returns 一个给定输入向量的值。
我不清楚您希望 apply.monthly(data, diff)
做什么。这与调用 diff(data)
然后将 NA
添加到每个月的第一个值相同。
通过您的修改,我现在明白您要做什么了。您想要差异,但您希望每年的 1 月是该月的水平,而不是与上一年 12 月的差异。
这是一种方法:
# Load your data as an example
Lines <-
"1990-05 100
1990-04 80
1990-03 60
1990-02 20
1990-01 5
1989-12 110
1989-11 89
1989-10 78"
con <- textConnection(Lines)
# Ensure the timezone of your non-intraday xts object is UTC,
# or bad things can happen
x <- as.xts(read.zoo(con, FUN=as.yearmon), tzone="UTC")
close(con)
# Create a helper function
f <- function(x) {
y <- diff(x)
if (.indexmon(y)[1] == 0)
y[1] <- x[1]
y
}
# apply the function to each year subset and rbind the results
do.call(rbind, lapply(split(x,'years'), f))
这是另一种方式,您可能会觉得更有吸引力。
colnames(x) <- "level"
# calculate all differences
x$diff <- diff(x$level)
# set January differences to their respective level
jan <- .indexmon(x) == 0
x[jan, "diff"] <- x[jan, "level"]
我在尝试执行以下工作时发生错误:
# generate random integrals #
data <- xts(floor(runif(100, 1,101)),as.Date("1973-02-01") + c(1:100) - 1)
apply.monthly(data, diff,1,1)
,而这个有效:
apply.monthly(data,mean)
我查过类似的问题,但似乎不适用于这里的情况。
有什么建议吗?
一些进一步的解释:
我需要这个的原因是我得到了如下的时间序列数据集,
1990-05 100
1990-04 80
1990-03 60
1990-02 20
1990-01 5
1989-12 110
1989-11 89
1989-10 78
...
每一年,y(t)=y_(t-1)+dy
,其中dt
是t期的数值变化。但这种模式只发生在每一年,而且每年都是分开的。所以基本上,我想检索每个特定年份的每个月之间的差异,即:
1990-05 20 #100-80
1990-04 20 #80-60
1990-03 40 #60-20
1990-02 15 #20-5
1990-01 5 #5
1989-12 21 #110-89
1989-11 11 #89-78
...
希望我已经解释清楚了。
谢谢,
apply.monthly
和period.apply
用于将数据聚合到指定的时间段。 diff
不起作用,因为 diff.xts
returns 一个与输入长度相同的向量。 mean
有效,因为它 returns 一个给定输入向量的值。
我不清楚您希望 apply.monthly(data, diff)
做什么。这与调用 diff(data)
然后将 NA
添加到每个月的第一个值相同。
通过您的修改,我现在明白您要做什么了。您想要差异,但您希望每年的 1 月是该月的水平,而不是与上一年 12 月的差异。
这是一种方法:
# Load your data as an example
Lines <-
"1990-05 100
1990-04 80
1990-03 60
1990-02 20
1990-01 5
1989-12 110
1989-11 89
1989-10 78"
con <- textConnection(Lines)
# Ensure the timezone of your non-intraday xts object is UTC,
# or bad things can happen
x <- as.xts(read.zoo(con, FUN=as.yearmon), tzone="UTC")
close(con)
# Create a helper function
f <- function(x) {
y <- diff(x)
if (.indexmon(y)[1] == 0)
y[1] <- x[1]
y
}
# apply the function to each year subset and rbind the results
do.call(rbind, lapply(split(x,'years'), f))
这是另一种方式,您可能会觉得更有吸引力。
colnames(x) <- "level"
# calculate all differences
x$diff <- diff(x$level)
# set January differences to their respective level
jan <- .indexmon(x) == 0
x[jan, "diff"] <- x[jan, "level"]