这个问题是关于 GRATIS 包中的 generate_msts() 函数。
我添加了一些新内容(使该函数具有将其输出转换为可爱的 tsibble 格式或保留原始“列表”格式的选项)并准备对 CRAN 的更新。
新代码添加如下(代码的详细信息以及问题底部显示的示例)
我想知道我应该得到 tsibble 索引吗?但是生成的数据好像没有索引?
output <- if (output_format == "list") {
res #this is output name defined before
} else if (output_format == "tsibble") {
as_tsibble(res)
}
return(output)
}
作为指导,我在Vignette中更新了此函数的相应示例。然后事情变得有线。
如果我没有保存生成的时间序列输出(例如 x <- my_function()),小插图 就无法编织出来。(不过我可以在独立的普通RMD文件中直接使用这个功能)
直接使用此代码可以在 RStudio 内部显示输出,但不能编织出来。
my_function(seasonal.periods = c(7, 365), n = 800, nComp = 2,output_format="tsibble")
Error in Fun(X[[i]],...): 'list' object cannot be coerced to type 'integer' Calls: <Anonymous>...
as.data.frame -> head -> head.data.frame -> lappy -> FUN Execution halted.
但是,这很好用。它可以编织出小插图并显示出小标题。
x <- my_function(seasonal.periods = c(7, 365), n = 800, nComp = 2,output_format="tsibble")
head(x)
但是,这样非常不方便,每次使用前都要保存。我想知道这是否是因为我在包中使用的任何默认设置或小插图没有改变?还是在更改 R 包中的函数后需要做一些额外的步骤?或者甚至我添加的 if else 内容需要改进?
我试图devtools::document("C:/Users/mreal/Documents/GitHub/package_name");devtools::install("C:/Users/mreal/Documents/GitHub/package_name")
更新重建功能。但这仍然无助于小插曲。
我也试过rm(list=ls())
了console
。它也不起作用
我在小插图中使用的代码如下
Github 链接:
https://github.com/BocongZhao823/gratis/blob/master/vignettes/QuickStart.Rmd
---
title: "Introduction to gratis"
author: "Bocong Zhao"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Introduction to gratis}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
{r initial, echo = FALSE, cache = FALSE, results = 'hide'}
library(knitr)
opts_chunk$set(
warning = FALSE, message = FALSE, echo = TRUE,
fig.width = 7, fig.height = 6, fig.align = 'centre',
comment = "#>"
)
original <- options("tibble.print_min")
options(tibble.print_min = 5)
# <---- Do stuff with changed option, e.g. print some tibbles ---->
options(tibble.print_min = original)
{r, message=FALSE, include = FALSE}
library(forecast)
library(tsibble)
{r setup}
# load package
library(gratis)
## Generate mutiple seasonal time series
Time series can exhibit multiple seasonal pattern of different length, especially when series observed at a high frequency such as daily or hourly data.
We use function **generate_msts()** to generate mutiple seasonal time series.
**Definitions**
Here are the definitions of parameter settings in function generate_msts():
|parameter settings | Definition|
|:----|:-----|
|seasonal.periods | a vector of seasonal periods of the time series to be generated|
|nComp|number of mixing components when simulating time series using MAR models|
|n |length of the generated time series|
**Example**
Suppose we want to use MAR model to generate a time series with **2** mixing components and the length **800** from random parameter spaces. Particularly, this time series has two seasonal periods **7** and **365**.
{r fig.height = 6, fig.width = 7}
# Generate mutiple seasonal time series with 'tsibble' output format
x <- generate_msts(seasonal.periods = c(7, 365), n = 800, nComp = 2,output_format="tsibble")
head(x)
**Plot time series**
{r fig.height = 6, fig.width = 7}
# Generate mutiple seasonal time series with 'list' output format
x <- generate_msts(seasonal.periods = c(7, 365), n = 800, nComp = 2,output_format="list")
autoplot(x)
(生成的.R文件)包内使用的R代码如下
** Github 链接**
https://github.com/BocongZhao823/gratis/blob/master/R/generate_ts.R
#' Generate mutiple seasonal time series from random parameter spaces of the mixture autoregressive (MAR) models.
#'
#' Generate mutiple seasonal time series from random parameter spaces of the mixture autoregressive (MAR) models.
#' @param seasonal.periods a vector of seasonal periods of the time series to be generated.
#' @param n length of the generated time series.
#' @param nComp number of mixing components when simulating time series using MAR models.
#' @param output_format An optional argument which allows to choose output format between "list" and "tsibble"
#' @return a time series with multiple seasonal periods.
#' @export
#' @examples
#' x <- generate_msts(seasonal.periods = c(7, 365), n = 800, nComp = 2, output_format= "list")
#' forecast::autoplot(x)
generate_msts <- function(seasonal.periods = c(7, 365), n = 800, nComp = NULL,output_format="list") {
x.list <- map(seasonal.periods, function(p) {
generate_ts(n.ts = 1, freq = p, n = n, nComp = nComp)$N1$x
})
names(x.list) <- paste0("Season", seasonal.periods)
x.list[1:(length(x.list) - 1)] <- lapply(x.list[1:(length(x.list) - 1)], function(x) {
x - trendcycle(stl(x, "per"))
})
weights <- msts_weights(length(seasonal.periods))
res <- as_tibble(scale(x.list %>% bind_cols())[, ]) %>%
mapply("*", ., weights) %>%
as_tibble() %>%
mutate(x = rowSums(.)) %>%
select(x) %>%
msts(seasonal.periods = seasonal.periods)
# New content
output <- if (output_format == "list") {
res
} else if (output_format == "tsibble") {
as_tsibble(res)
}
return(output)
}
# ===========================================================================
# Simulated weights for the simulation of msts
# ===========================================================================
msts_weights <- function(n.periods) {
gamma <- runif(n.periods, 0)
weights <- gamma / sum(gamma)
return(weights)
}