2

可重现的代码:

library(quantmod)
getSymbols('SPY', from = '1950-01-01')
Y <- Cl(to.monthly(SPY))
start <- c(1993, 1)
end <- c(2013, 2)
y <- ts(log(Y), start = start, end = end, frequency = 12)

我希望start并且end设置为等于start(Y)且不end(Y)“手动”创建它们。

是否可以?

这是我想在不指定startend变量的情况下得到的:

> y
          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
1993 3.782825 3.793465 3.810876 3.784871 3.811539 3.807995 3.803101 3.840742
1994 3.875774 3.846097 3.797510 3.808660 3.824502 3.794815 3.826683 3.864092
1995 3.852061 3.892228 3.914221 3.943328 3.982295 3.996548 4.028205 4.032646
1996 4.153713 4.157006 4.169607 4.180369 4.202750 4.206333 4.160288 4.179451
1997 4.361951 4.371471 4.322409 4.383151 4.444532 4.480853 4.557135 4.503912
1998 4.588126 4.655103 4.699935 4.712589 4.691623 4.730127 4.716533 4.564348
1999 4.849370 4.816727 4.854995 4.892227 4.869072 4.919981 4.888468 4.883256
2000 4.938495 4.923187 5.013165 4.977354 4.961515 4.978663 4.962845 5.026115
2001 4.920127 4.819878 4.759521 4.841506 4.835885 4.808927 4.798679 4.737513
2002 4.728979 4.710881 4.740749 4.680834 4.674883 4.594716 4.512616 4.519394
2003 4.455045 4.441474 4.439588 4.520810 4.574195 4.581185 4.599052 4.619467
2004 4.731627 4.745106 4.728272 4.709170 4.726148 4.740837 4.708088 4.710521
2005 4.772040 4.792728 4.770346 4.751433 4.783149 4.780635 4.818183 4.808764
2006 4.848116 4.853826 4.866226 4.878779 4.848195 4.846389 4.850858 4.872445
2007 4.968076 4.948263 4.955827 4.999170 5.032527 5.013498 4.981687 4.994438
2008 4.922678 4.896496 4.882575 4.929136 4.944139 4.851874 4.842848 4.858183
2009 4.416790 4.303119 4.376009 4.470724 4.527533 4.521245 4.593199 4.629472
2010 4.676467 4.707185 4.762174 4.777526 4.694737 4.636863 4.702932 4.656908
2011 4.857329 4.891476 4.887262 4.915812 4.904534 4.882575 4.862367 4.805823
2012 4.877637 4.920127 4.947411 4.940713 4.878779 4.913390 4.925150 4.949894
2013 5.008633 5.007564                                                      
          Sep      Oct      Nov      Dec
1993 3.827336 3.846738 3.836006 3.841386
1994 3.832330 3.860309 3.819688 3.819030
1995 4.068685 4.065774 4.109397 4.118712
1996 4.228584 4.260424 4.330996 4.301901
1997 4.547223 4.522441 4.560382 4.575329
1998 4.622519 4.700480 4.754624 4.814702
1999 4.857873 4.919981 4.936486 4.989616
2000 4.967241 4.962495 4.884921 4.876647
2001 4.648613 4.661551 4.736637 4.738827
2002 4.404155 4.483229 4.543082 4.479947
2003 4.604670 4.656813 4.667675 4.712050
2004 4.716354 4.729156 4.769752 4.794716
2005 4.812510 4.788574 4.831588 4.824386
2006 4.894701 4.925731 4.945421 4.953147
2007 5.027689 5.041164 5.001662 4.985044
2008 4.753504 4.572957 4.500809 4.502473
2009 4.659564 4.640151 4.699935 4.713486
2010 4.737338 4.774829 4.774829 4.834296
2011 4.728714 4.832306 4.828234 4.832306
2012 4.969605 4.951239 4.956883 4.958710
2013  

我正在添加这些文本行,因为 SO 告诉我我没有使用足够数量的解释文本词。

4

2 回答 2

3

尝试以下操作:

as.ts(log(Y), start = head(index(Y), 1), end = tail(index(Y), 1))

这利用了indexxts 对象中存在的日期。

正如@Arun 在评论中提到的那样,以下选项更加直接:

ts(log(Y), start = start(Y), end = end(Y), frequency = 12)
as.ts(log(Y), start = start(Y), end = end(Y))

我会建议这种ts(log(Y)...方法,因为attributes也保留了。比较:

ytest1 <- as.ts(log(Y), start = start(Y), end = end(Y))
ytest2 <- ts(log(Y), start = start(Y), end = end(Y), frequency = 12)

identical(y, ytest1)
# [1] FALSE
all.equal(y, ytest1, check.attributes = FALSE)
# [1] TRUE
identical(y, ytest2)
# [1] TRUE
于 2013-02-05T11:02:09.177 回答
1

我建议使用以下辅助功能 - 您可以调整freq以设置周期性:

xts.to.ts <- function(X, freq = 12L) {
    ts(as.numeric(X), 
          start = c(.indexyear(X)[1] + 1900, .indexmon(X)[1] + 1),
          freq = freq)
}

然后X_ts <- xts.to.ts(X)

于 2018-12-21T00:43:24.123 回答