如何使用 `tempdisagg` 包中的 `td` 命令将每月数据分解为每日数据频率?
How can I use the `td` command from the `tempdisagg` package to disaggregate monthly data into daily data frequency?
我有一个每月的频率数据,我正试图将其分解为每天的频率数据。
所以我使用 R 中 tempdisagg
包中的 td
命令,代码如下:
dat=ts(data[,2])
result=td(dat~1, conversion = "average", to = "day", method = "chow-lin-maxlog")
然后我收到以下错误消息:
Error in td(dat ~ 1, conversion = "average", to = "day", method = "chow-lin-maxlog") : 'to' argument: unknown character string
我dat
使用的数据如下:
> dput(head(dat))
c(82.47703009, 84.63094431, 70.00659987, 78.81135651, 74.749746,82.95638213)
所以虽然这个数据dat
是按月频率计算的,但是开始和结束还没有反映出来。
实际上,开始日期是 1/1997,结束日期是 9/2019。
我可以在将月度数据 dat
分解为每日频率数据方面获得帮助吗?
看起来 tempdisagg 包不允许按月到每日分解。来自 td()
帮助文件 'to' 参数:
high-frequency destination frequency as a character string ("quarterly" or "monthly") or as a scalar (e.g. 2, 4, 7, 12). If the input series are ts objects, the argument is necessary if no indicator is given. If the input series are vectors, to must be a scalar indicating the frequency ratio.
您的错误消息“'to' 参数:未知字符串”是因为 to =
参数只接受 'quarterly' 或 'monthly' 作为字符串。
这里有一些关于将每月数据分解为每日数据的讨论:https://stats.stackexchange.com/questions/258810/disaggregate-monthly-forecasts-into-daily-data
经过一番搜索,似乎没有人一直使用按月到每日的分类数据。 tempdisagg
软件包似乎能够实现大多数其他人认为可能实现的功能——每年到每季度或每月,并且时间段是一致的偶数倍数。
埃里克,我在下面添加了一个脚本,据我所知,它应该可以说明您要执行的操作。
这里我们使用真实的价格数据,从每日价格 -> 每月价格 -> 每月 returns -> 平均每天 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))
这里有三张图表显示 1. 仅月度 returns,2. 月度 returns 的日平均值,3. 两者都在一起。由于它们是相同的,所以在第三张图片中重叠显示的只有一个。
使用 tempdisagg 1.0,可以轻松地将月度数据分解为每日数据,使总和或平均值与月度系列保持一致。
此 post 更详细地解释了新功能。
A bit of trickery 还可以从每月转换为每周。
这是一个可重现的例子,使用原始 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
由 reprex package (v2.0.0)
于 2021 年 7 月 15 日创建
我有一个每月的频率数据,我正试图将其分解为每天的频率数据。
所以我使用 R 中 tempdisagg
包中的 td
命令,代码如下:
dat=ts(data[,2])
result=td(dat~1, conversion = "average", to = "day", method = "chow-lin-maxlog")
然后我收到以下错误消息:
Error in td(dat ~ 1, conversion = "average", to = "day", method = "chow-lin-maxlog") : 'to' argument: unknown character string
我dat
使用的数据如下:
> dput(head(dat))
c(82.47703009, 84.63094431, 70.00659987, 78.81135651, 74.749746,82.95638213)
所以虽然这个数据dat
是按月频率计算的,但是开始和结束还没有反映出来。
实际上,开始日期是 1/1997,结束日期是 9/2019。
我可以在将月度数据 dat
分解为每日频率数据方面获得帮助吗?
看起来 tempdisagg 包不允许按月到每日分解。来自 td()
帮助文件 'to' 参数:
high-frequency destination frequency as a character string ("quarterly" or "monthly") or as a scalar (e.g. 2, 4, 7, 12). If the input series are ts objects, the argument is necessary if no indicator is given. If the input series are vectors, to must be a scalar indicating the frequency ratio.
您的错误消息“'to' 参数:未知字符串”是因为 to =
参数只接受 'quarterly' 或 'monthly' 作为字符串。
这里有一些关于将每月数据分解为每日数据的讨论:https://stats.stackexchange.com/questions/258810/disaggregate-monthly-forecasts-into-daily-data
经过一番搜索,似乎没有人一直使用按月到每日的分类数据。 tempdisagg
软件包似乎能够实现大多数其他人认为可能实现的功能——每年到每季度或每月,并且时间段是一致的偶数倍数。
埃里克,我在下面添加了一个脚本,据我所知,它应该可以说明您要执行的操作。
这里我们使用真实的价格数据,从每日价格 -> 每月价格 -> 每月 returns -> 平均每天 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))
这里有三张图表显示 1. 仅月度 returns,2. 月度 returns 的日平均值,3. 两者都在一起。由于它们是相同的,所以在第三张图片中重叠显示的只有一个。
使用 tempdisagg 1.0,可以轻松地将月度数据分解为每日数据,使总和或平均值与月度系列保持一致。
此 post 更详细地解释了新功能。
A bit of trickery 还可以从每月转换为每周。
这是一个可重现的例子,使用原始 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
由 reprex package (v2.0.0)
于 2021 年 7 月 15 日创建