如何使用R中的特定公式将季度数据分解为月度数据?
亲爱的 我尝试使用R将季度数据分解为月度数据。我不关心日期,因为我可以生成与值相对应的月份向量,没有问题。问题在于值向量和缺失数据的插补。请参见下面的示例:如何使用R中的特定公式将季度数据分解为月度数据?,r,time-series,data-manipulation,R,Time Series,Data Manipulation,亲爱的 我尝试使用R将季度数据分解为月度数据。我不关心日期,因为我可以生成与值相对应的月份向量,没有问题。问题在于值向量和缺失数据的插补。请参见下面的示例: Quarter Value 2010-Q1 10 2010-Q2 15 2010-Q3 18 2010-Q4 12 新的数据集应该如下所示 Month Value 2010-3 10 2010-4 11.67 2010-5 13.34 2010-6 15 2010-7 16 2010-8 17 2010-9 18 201
Quarter Value
2010-Q1 10
2010-Q2 15
2010-Q3 18
2010-Q4 12
新的数据集应该如下所示
Month Value
2010-3 10
2010-4 11.67
2010-5 13.34
2010-6 15
2010-7 16
2010-8 17
2010-9 18
2010-10 16
2010-11 14
2010-12 12
现在,每个季度内的月份使用以下公式填写
The first month of the quarter[i] = The previous quarter value [i-1] + ((The difference between the quarter [i] and [i-1])/3)
The second month of the quarter[i] = The previous quarter value [i-1] + 2*((The difference between the quarter [i] and [i-1])/3)
例如:
2020-Q1=10
2020-Q2=15
差/3=5/3
2020年4月=10+diff
2020年5月=10+2*diff
2020年6月=15(季度末保持不变)或可计算为10+3*diff
我想知道如何生成一个新的变量来分解上面提到的值
谢谢1)使用yearqtr
索引(直接表示没有月份或日期的年份和季度)将输入转换为zoo系列z
,然后用NAs填充,并应用na.approx
线性填充它们,给出值
。假设序列间隔规则,我们可以使用每年12个月的频率将第一个索引值转换为yearmon
(直接表示没有日期的年份和月份)。最后,将其保留为Value
,或者使用最后一行将其转换回数据帧DF2
。另一种可能是使用as.ts(Value)
将其转换为ts
系列
请注意,yearmon
类显示如下,但在内部表示年和月,表示年加上一个分数,等于0表示1月,1/12表示2月,…,11/12表示12月,因此as.integer(time(Value))
将给出年,周期(time(Value))
将给出月数(Jan=1,…,Dec=12)
从图形上看,它如下所示:
plot(Value, type = "o")
(图后续)
2)从(1)开始以z
的第二种方法是首先创建输出yearmon
时间序列tt
,将z
的时间索引转换为yearmon
给出z.ym
,然后合并它们生成NA,最后应用NA.approx
来填充它们
tt <- seq(as.yearmon(start(z)), as.yearmon(end(z)), 1/12)
z.ym <- aggregate(z, as.yearmon, c)
Value <- na.approx(merge(z.ym, zoo(, tt)))
tt声称的副本没有任何回答上述问题的答案。谢谢!我对value列更感兴趣。我可以毫无问题地向它添加一个时间序列变量。我的问题可能不清楚。如何将值变量单独更改为我在问题中提到的形式。忘记时间吧。
plot(Value, type = "o")
tt <- seq(as.yearmon(start(z)), as.yearmon(end(z)), 1/12)
z.ym <- aggregate(z, as.yearmon, c)
Value <- na.approx(merge(z.ym, zoo(, tt)))
Lines <- "Quarter Value
2010-Q1 10
2010-Q2 15
2010-Q3 18
2010-Q4 12"
DF <- read.table(text = Lines, header = TRUE)