简体   繁体   English

如何使用 `tempdisagg` 包中的 `td` 命令将每月数据分解为每日数据频率?

[英]How can I use the `td` command from the `tempdisagg` package to disaggregate monthly data into daily data frequency?

I have a monthly frequency data which I am trying to disaggregate into a daily frequency data.我有一个每月频率数据,我试图将其分解为每日频率数据。 So I use the td command from the tempdisagg package in R using the code below:因此,我使用 R 中的tempdisagg包中的td命令使用以下代码:

 dat=ts(data[,2])
 result=td(dat~1, conversion = "average", to = "day", method = "chow-lin-maxlog")

Then I get the following error message:然后我收到以下错误消息:

 Error in td(dat ~ 1, conversion = "average", to = "day", method = "chow-lin-maxlog") : 'to' argument: unknown character string

The data I use for dat is as follows:我用于dat的数据如下:

在此处输入图片说明

 > dput(head(dat))
 c(82.47703009, 84.63094431, 70.00659987, 78.81135651, 74.749746,82.95638213)

So although this data dat is in monthly frequency, the start and end do not reflect this yet.所以虽然这个数据dat是按月频率的,但开始和结束还没有反映这一点。 In fact, the start date is 1/1997 and end date is 9/2019.实际上,开始日期是 1/1997,结束日期是 9/2019。

May I get help on disaggregating this monthly data dat into daily frequency data please?我可以得到帮助上分列该月度数据dat到日常的频率数据讨好?

It looks like the tempdisagg package doesn't allow for monthly to daily disaggregation.看起来 tempdisagg 包不允许每月到每天的分解。 From the td() help file 'to' argument:td()帮助文件 'to' 参数:

high-frequency destination frequency as a character string ("quarterly" or "monthly") or as a scalar (eg 2, 4, 7, 12).高频目标频率作为字符串(“每季度”或“每月”)或作为标量(例如 2、4、7、12)。 If the input series are ts objects, the argument is necessary if no indicator is given.如果输入系列是 ts 对象,则在未给出指示符时该参数是必需的。 If the input series are vectors, to must be a scalar indicating the frequency ratio.如果输入序列是向量,to 必须是表示频率比的标量。

Your error message "'to' argument: unknown character string" is because the to = argument only accepts 'quarterly' or 'monthly' as strings.您的错误消息“'to' 参数:未知字符串”是因为to =参数只接受 'quarterly' 或 'monthly' 作为字符串。

There is some discussion about disaggregating monthly data to daily on the stats stackexchage here: https://stats.stackexchange.com/questions/258810/disaggregate-monthly-forecasts-into-daily-data在此处的 stats stackexchage 上有一些关于将每月数据分解为每日数据的讨论: https : //stats.stackexchange.com/questions/258810/disaggregate-monthly-forecasts-into-daily-data

After some searching, it looks like nobody consistently using disaggregated monthly to daily data.经过一番搜索,似乎没有人一直使用按月到日分类的数据。 The tempdisagg package seems to be capable of what most others have found to be possible -- yearly to quarterly or monthly, and time periods that are consistent even multiples. tempdisagg包似乎能够完成大多数其他人认为可能的事情——每年到每季度或每月,以及一致的甚至倍数的时间段。

Eric, I've added a script below that should illustrate what you're trying to do, as I understand it.埃里克,我在下面添加了一个脚本,据我所知,它应该说明您正在尝试做什么。

Here we use real pricing data to move from daily prices -> monthly prices -> monthly returns -> average daily returns.在这里,我们使用真实的定价数据从每日价格 -> 每月价格 -> 每月回报 -> 平均每日回报。

library(quantmod)
library(xts)
library(zoo)
library(tidyverse)
library(lubridate)

# Get price data to use as an example
getSymbols('MSFT')

#This data has more information than we want, remove unwanted columns:
msft <- Ad(MSFT) 

#Add new column that acts as an 'indexed price' rather than 
# actual price data.  This is to show that calculated returns
# don't depend on real prices, data indexed to a value is fine.
msft$indexed <- scale(msft$MSFT.Adjusted, center = FALSE)

#split into two datasets  
msft2 <- msft$indexed
msft$indexed <- NULL


#msft contains only closing data, msft2 only contains scaled data (not actual prices)
#  move from daily data to monthly, to replicate the question's situation.
a <- monthlyReturn(msft)
b <- monthlyReturn(msft2)

#prove returns based on rescaled(indexed) data and price data is the same:
all.equal(a,b)

# subset to a single year
a <- a['2019']
b <- b['2019']

#add column with days in each month
a$dim <- days_in_month(a) 
a$day_avg <- a$monthly.returns / a$dim  ## <- This must've been left out

day_avgs <- data.frame(day_avg = rep(a$day_avg, a$dim))


# daily averages timesereis from monthly returns.
z <- zoo(day_avgs$day_avg, 
         seq(from = as.Date("2019-01-01"), 
             to = as.Date("2019-12-31"), 
             by = 1)) %>%
  as.xts()

#chart showing they are the same:
PerformanceAnalytics::charts.PerformanceSummary(cbind(a$monthly.returns, z))

Here are three charts showing 1. monthly returns only, 2. daily average from monthly returns, 3. both together.以下是三个图表,显示 1. 仅月度回报,2. 月度回报的日均值,3. 两者兼而有之。 As they are identical, overplotting in the third image shows only one.由于它们是相同的,第三张图像中的过度绘制仅显示一个。

每月回报

月收益的日均收益

每月和每日平均绘制在一起

With tempdisagg 1.0 , it is easy to disaggregate monthly data to daily, keeping the sum or the average consistent with the monthly series.使用tempdisagg 1.0 ,可以轻松地将月度数据分解为每日数据,使总和或平均值与月度系列保持一致。

This post explains the new feature in more detail.这篇文章更详细地解释了新功能。

A bit of trickery also makes it possible to convert from monthly to weekly. 一些技巧也使从每月转换为每周成为可能。

Here is a reproducible example, using the first six months of the original post:这是一个可重复的示例,使用原始帖子的前六个月:

x <- tsbox::ts_tbl(ts(c(82.47703009, 84.63094431, 70.00659987, 78.81135651, 74.749746, 82.95638213), start = 2020, frequency = 12))
x
#> # A tibble: 6 x 2
#>   time       value
#>   <date>     <dbl>
#> 1 2020-01-01  82.5
#> 2 2020-02-01  84.6
#> 3 2020-03-01  70.0
#> 4 2020-04-01  78.8
#> 5 2020-05-01  74.7
#> 6 2020-06-01  83.0

library(tempdisagg)
packageVersion("tempdisagg")
#> [1] '1.0'

m <- td(x ~ 1, to = "daily", method = "fast", conversion = "average")
predict(m)
#> # A tibble: 182 x 2
#>    time       value
#>    <date>     <dbl>
#>  1 2020-01-01  80.6
#>  2 2020-01-02  80.7
#>  3 2020-01-03  80.7
#>  4 2020-01-04  80.7
#>  5 2020-01-05  80.8
#>  6 2020-01-06  80.8
#>  7 2020-01-07  80.9
#>  8 2020-01-08  81.0
#>  9 2020-01-09  81.1
#> 10 2020-01-10  81.2
#> # … with 172 more rows

Created on 2021-07-15 by the reprex package (v2.0.0)reprex 包( v2.0.0 ) 于 2021 年 7 月 15 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM