我使用的是R编程语言。我在这里学习本教程:https://blogs.rstudio.com/ai/posts/2018-06-25-sunspots-lstm/
我正试图以与这里的例子相同的方式准备我的数据:
# Core Tidyverse
library(tidyverse)
library(glue)
library(forcats)
# Time Series
library(timetk)
library(tidyquant)
library(tibbletime)
# Visualization
library(cowplot)
# Preprocessing
library(recipes)
# Sampling / Accuracy
library(rsample)
library(yardstick)
# Modeling
library(keras)
library(tfruns)
#here is what I am trying to copy
sun_spots <- datasets::sunspot.month %>%
tk_tbl() %>%
mutate(index = as_date(index)) %>%
as_tbl_time(index = index)
sun_spots
# A time tibble: 3,177 x 2
# Index: index
index value
<date> <dbl>
1 1749-01-01 58
2 1749-02-01 62.6
3 1749-03-01 70
4 1749-04-01 55.7
5 1749-05-01 85
6 1749-06-01 83.5
7 1749-07-01 94.8
8 1749-08-01 66.3
9 1749-09-01 75.9
10 1749-10-01 75.5
# ... with 3,167 more rows
在本例中,格式化后的数据的尺寸为3177 x 2。
我想,我应该能够模拟类似形式的数据(使用与教程中的数据相同的名称(:
index = seq(as.Date("1749/1/1"), as.Date("2016/1/1"),by="day")
index <- format(as.Date(index), "%Y/%m/%d")
value <- rnorm(97520,27,2.1)
final_data <- data.frame(index, value)
y.mon<-aggregate(value~format(as.Date(index),
format="%Y/%m"),data=final_data, FUN=sum)
y.mon$index = y.mon$`format(as.Date(index), format = "%Y/%m")`
y.mon$`format(as.Date(index), format = "%Y/%m")` = NULL
#resulting file is y.mon
现在,当我尝试将我的文件转换为所需的格式时:
y.mon_mod <- y.mon%>%
tk_tbl() %>%
mutate(index = as_date(index)) %>%
as_tbl_time(index = index)
我得到以下错误:
Error: Problem with `mutate()` input `index`.
x 'origin' must be supplied
i Input `index` is `as_date(index)`.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning message:
In tk_tbl.data.frame(.) :
Warning: No index to preserve. Object otherwise converted to tibble successfully.
有人知道为什么会发生这种错误吗?我检查了一下我的环境,上面写着";名称空间";库已加载。是因为我的";日期";(索引(变量的格式不正确?有人知道如何解决这个问题吗?
感谢
使index
列可以转换为日期对象。
library(dplyr)
library(lubridate)
library(tibbletime)
library(timetk)
y.mon %>%
mutate(index = paste0(index, '/01')) %>%
tk_tbl() %>%
mutate(index = as_date(index)) %>%
as_tbl_time(index = index) -> y.mon