[英]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' 參數:
高頻目標頻率作為字符串(“每季度”或“每月”)或作為標量(例如 2、4、7、12)。 如果輸入系列是 ts 對象,則在未給出指示符時該參數是必需的。 如果輸入序列是向量,to 必須是表示頻率比的標量。
您的錯誤消息“'to' 參數:未知字符串”是因為to =
參數只接受 'quarterly' 或 'monthly' 作為字符串。
在此處的 stats stackexchage 上有一些關於將每月數據分解為每日數據的討論: https : //stats.stackexchange.com/questions/258810/disaggregate-monthly-forecasts-into-daily-data
經過一番搜索,似乎沒有人一直使用按月到日分類的數據。 tempdisagg
包似乎能夠完成大多數其他人認為可能的事情——每年到每季度或每月,以及一致的甚至倍數的時間段。
埃里克,我在下面添加了一個腳本,據我所知,它應該說明您正在嘗試做什么。
在這里,我們使用真實的定價數據從每日價格 -> 每月價格 -> 每月回報 -> 平均每日回報。
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. 僅月度回報,2. 月度回報的日均值,3. 兩者兼而有之。 由於它們是相同的,第三張圖像中的過度繪制僅顯示一個。
使用tempdisagg 1.0 ,可以輕松地將月度數據分解為每日數據,使總和或平均值與月度系列保持一致。
這篇文章更詳細地解釋了新功能。
一些技巧也使從每月轉換為每周成為可能。
這是一個可重復的示例,使用原始帖子的前六個月:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.