The error occurred to me When I was trying to do the following work:
我在尝试做以下工作时犯了一个错误:
# 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)
, while this one works:
,而这个是有效的:
apply.monthly(data,mean)
意味着apply.monthly(数据)
I have checked similar questions posted, but it seems they do not apply to the situation here.
我检查过类似的问题,但似乎不适用于这里的情况。
Any advice?
任何建议吗?
Some further explanation:
一些进一步的解释:
The reason I need this is that I got a time series data set like the following,
我需要它的原因是,我得到了一个时间序列数据集如下,
1990-05 100
1990-04 80
1990-03 60
1990-02 20
1990-01 5
1989-12 110
1989-11 89
1989-10 78
...
In each year, y(t)=y_(t-1)+dy
, where dt
is value change in period t. But this pattern only happens in each year and each year separately. So basically, I want to retrieve the difference between each month in every specific year, that is:
在每一年,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
...
Hope I have made the explanation clear enough.
希望我已经解释得够清楚了。
Thanks,
谢谢,
1 个解决方案
#1
4
apply.monthly
and period.apply
are used to aggregate data to the specified period. diff
doesn't work because diff.xts
returns a vector the same length as the input. mean
works because it returns one value for a given input vector.
适用。每月和时期。apply用于将数据聚合到指定的时间段。diff不起作用,因为diff.xts返回一个与输入长度相同的向量。平均值之所以有效,是因为它为给定的输入向量返回一个值。
It's not clear to me what you expect apply.monthly(data, diff)
to do. It would be the same as calling diff(data)
and then adding NA
to the first value of each month.
我不清楚你希望申请什么。每月(数据、diff)。这将等同于调用diff(data),然后在每个月的第一个值中添加NA。
With your edit, I now understand what you are trying to do. You want the differences, but you want January of each year to be the level for that month, not the difference from December of the prior year.
有了你的编辑,我现在明白你想做什么了。你想要差异,但你想要每年的一月是那个月的水平,而不是前一年的十二月。
Here's one way to do it:
有一种方法:
# 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))
Here's another way, that you might find more appealing.
这是另一种方式,你可能会觉得更有吸引力。
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"]
#1
4
apply.monthly
and period.apply
are used to aggregate data to the specified period. diff
doesn't work because diff.xts
returns a vector the same length as the input. mean
works because it returns one value for a given input vector.
适用。每月和时期。apply用于将数据聚合到指定的时间段。diff不起作用,因为diff.xts返回一个与输入长度相同的向量。平均值之所以有效,是因为它为给定的输入向量返回一个值。
It's not clear to me what you expect apply.monthly(data, diff)
to do. It would be the same as calling diff(data)
and then adding NA
to the first value of each month.
我不清楚你希望申请什么。每月(数据、diff)。这将等同于调用diff(data),然后在每个月的第一个值中添加NA。
With your edit, I now understand what you are trying to do. You want the differences, but you want January of each year to be the level for that month, not the difference from December of the prior year.
有了你的编辑,我现在明白你想做什么了。你想要差异,但你想要每年的一月是那个月的水平,而不是前一年的十二月。
Here's one way to do it:
有一种方法:
# 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))
Here's another way, that you might find more appealing.
这是另一种方式,你可能会觉得更有吸引力。
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"]