从现有数据集创建时间序列
Creating a Time Series from an Existing Data Set
我想将以下数据转换成时间序列 - 这样我就可以使用 autoplot()
。
我该怎么做才能使 "Year" 列位于 x 轴上? (我知道日期的格式必须是 01-01-2006,我同意):
Team PTS W GF GA S SA Year
NSH 88 38 214 233 2382 2365 2014
NSH 104 47 226 202 2614 2304 2015
NSH 96 41 224 213 2507 2231 2016
NSH 94 41 238 220 2557 2458 2017
NSH 117 53 261 204 2641 2650 2018
使用 as.ts()
会产生一些非常大且无法使用的数字的年份列。谢谢!我想使用新的时间序列框架进行预测:ARIMA、VARs 等
这是否给了你想要的东西:
df_ts <- ts(df[ , setdiff(names(df), c("Team", "Year"))],
start = 2014,
end = 2018,
frequency = 1)
class(df_ts)
#[1] "mts" "ts" "matrix"
我从强制转换中排除了列 Team
和 Year
,因为 Year
似乎不需要并且 Team
是字符类型。来自 ?ts
Time series must have at least one observation, and although they need not be numeric there is very limited support for non-numeric series.
使用ggfortify::autoplot.ts
作图
library(ggfortify)
autoplot(df_ts)
数据
df <- structure(list(Team = c("NSH", "NSH", "NSH", "NSH", "NSH"), PTS = c(88L,
104L, 96L, 94L, 117L), W = c(38L, 47L, 41L, 41L, 53L), GF = c(214L,
226L, 224L, 238L, 261L), GA = c(233L, 202L, 213L, 220L, 204L),
S = c(2382L, 2614L, 2507L, 2557L, 2641L), SA = c(2365L, 2304L,
2231L, 2458L, 2650L), Year = 2014:2018), .Names = c("Team",
"PTS", "W", "GF", "GA", "S", "SA", "Year"), class = "data.frame", row.names = c(NA,
-5L))
编辑
在您的图中显示缺失观察结果的一种方法是将隐式缺失观察结果转化为显式缺失观察结果。我将使用 tidyr
的 complete()
library(tidyr)
df_complete <- complete(df_incomplete, Year = min(Year):max(Year))
df_complete_ts <- ts(df_complete[ , setdiff(names(df_complete), c("Team", "Year"))],
start = 2011,
frequency = 1)
autoplot(df_complete_ts)
data2
df_incomplete <- structure(list(Team = c("NSH", "NSH", "NSH", "NSH", "NSH", "NSH",
"NSH"), PTS = c(88L, 88L, 88L, 104L, 96L, 94L, 117L), W = c(38L,
38L, 38L, 47L, 41L, 41L, 53L), GF = c(214L, 214L, 214L, 226L,
224L, 238L, 261L), GA = c(233L, 233L, 233L, 202L, 213L, 220L,
204L), S = c(2382L, 2382L, 2382L, 2614L, 2507L, 2557L, 2641L),
SA = c(2365L, 2365L, 2365L, 2304L, 2231L, 2458L, 2650L),
Year = c(2011L, 2012L, 2014L, 2015L, 2016L, 2017L, 2018L)), .Names = c("Team",
"PTS", "W", "GF", "GA", "S", "SA", "Year"), class = "data.frame", row.names = c(NA,
-7L))
我在 R 中成功使用 ts() 函数。对于年度数据,代码看起来像这样。
df <- ts(data, frequency = 1, start = 2014)
autoplot(df)
这应该会给您想要的结果。
我想将以下数据转换成时间序列 - 这样我就可以使用 autoplot()
。
我该怎么做才能使 "Year" 列位于 x 轴上? (我知道日期的格式必须是 01-01-2006,我同意):
Team PTS W GF GA S SA Year
NSH 88 38 214 233 2382 2365 2014
NSH 104 47 226 202 2614 2304 2015
NSH 96 41 224 213 2507 2231 2016
NSH 94 41 238 220 2557 2458 2017
NSH 117 53 261 204 2641 2650 2018
使用 as.ts()
会产生一些非常大且无法使用的数字的年份列。谢谢!我想使用新的时间序列框架进行预测:ARIMA、VARs 等
这是否给了你想要的东西:
df_ts <- ts(df[ , setdiff(names(df), c("Team", "Year"))],
start = 2014,
end = 2018,
frequency = 1)
class(df_ts)
#[1] "mts" "ts" "matrix"
我从强制转换中排除了列 Team
和 Year
,因为 Year
似乎不需要并且 Team
是字符类型。来自 ?ts
Time series must have at least one observation, and although they need not be numeric there is very limited support for non-numeric series.
使用ggfortify::autoplot.ts
作图
library(ggfortify)
autoplot(df_ts)
数据
df <- structure(list(Team = c("NSH", "NSH", "NSH", "NSH", "NSH"), PTS = c(88L,
104L, 96L, 94L, 117L), W = c(38L, 47L, 41L, 41L, 53L), GF = c(214L,
226L, 224L, 238L, 261L), GA = c(233L, 202L, 213L, 220L, 204L),
S = c(2382L, 2614L, 2507L, 2557L, 2641L), SA = c(2365L, 2304L,
2231L, 2458L, 2650L), Year = 2014:2018), .Names = c("Team",
"PTS", "W", "GF", "GA", "S", "SA", "Year"), class = "data.frame", row.names = c(NA,
-5L))
编辑
在您的图中显示缺失观察结果的一种方法是将隐式缺失观察结果转化为显式缺失观察结果。我将使用 tidyr
的 complete()
library(tidyr)
df_complete <- complete(df_incomplete, Year = min(Year):max(Year))
df_complete_ts <- ts(df_complete[ , setdiff(names(df_complete), c("Team", "Year"))],
start = 2011,
frequency = 1)
autoplot(df_complete_ts)
data2
df_incomplete <- structure(list(Team = c("NSH", "NSH", "NSH", "NSH", "NSH", "NSH",
"NSH"), PTS = c(88L, 88L, 88L, 104L, 96L, 94L, 117L), W = c(38L,
38L, 38L, 47L, 41L, 41L, 53L), GF = c(214L, 214L, 214L, 226L,
224L, 238L, 261L), GA = c(233L, 233L, 233L, 202L, 213L, 220L,
204L), S = c(2382L, 2382L, 2382L, 2614L, 2507L, 2557L, 2641L),
SA = c(2365L, 2365L, 2365L, 2304L, 2231L, 2458L, 2650L),
Year = c(2011L, 2012L, 2014L, 2015L, 2016L, 2017L, 2018L)), .Names = c("Team",
"PTS", "W", "GF", "GA", "S", "SA", "Year"), class = "data.frame", row.names = c(NA,
-7L))
我在 R 中成功使用 ts() 函数。对于年度数据,代码看起来像这样。
df <- ts(data, frequency = 1, start = 2014)
autoplot(df)
这应该会给您想要的结果。