我想为所有变量创建一个时间序列对象。如果我只使用一个变量,那很简单
w=ts(mydat$x1,frequency = 12,start=c(2015,1))
但是我有很多变量。
其中一部分。
mydat=structure(list(date = structure(c(3L, 2L, 6L, 1L, 7L, 5L, 4L), .Label = c("apr-15",
"feb-15", "jan-15", "jul15", "jun-15", "march-15", "may-15"), class = "factor"),
x1 = c(653411L, 620453L, 742567L, 578548L, 720100L, 553740L,
588145L), x2 = c(242108L, 210841L, 255046L, 185243L, 257159L,
182594L, 246051L), x3 = c(234394L, 289563L, 341791L, 293608L,
306807L, 285190L, 279252L), x4 = c(309228L, 226175L, 292387L,
183745L, 223322L, 161218L, 201499L)), .Names = c("date",
"x1", "x2", "x3", "x4"), class = "data.frame", row.names = c(NA,
-7L))
如何在数据集中创建时间序列对象以进行下一步分析?
答案 0 :(得分:2)
只需提供您希望的所有列:
myts <- stats::ts(mydat[,-1], frequency = 12, start = c(2015,1))
> myts
x1 x2 x3 x4
Jan 2015 653411 242108 234394 309228
Feb 2015 620453 210841 289563 226175
Mar 2015 742567 255046 341791 292387
Apr 2015 578548 185243 293608 183745
May 2015 720100 257159 306807 223322
Jun 2015 553740 182594 285190 161218
Jul 2015 588145 246051 279252 201499