如何在回归后保留拟合数据的xts格式?

时间:2014-06-23 06:54:19

标签: r regression xts

我对xts格式的时间序列数据进行了回归

> inputfit <- lm(y_t ~ y_tminus1
                   + cpi_t + cpi_tminus1 +
                     m1_t + m1_tminus1 +
                     ip_t + ip_tminus1, data = RegData)


> class(RegData)
[1] "xts" "zoo"
class(fitted(inputfit))
[1] "numeric"

lm()保留&#34; ts&#34;拟合数据的格式,但对于&#34; xts&#34;不做同样的事情。格式。这是为什么?我怎样才能克服这个问题?是否有可用的套餐?

另一种选择是我将拟合数据强制给xts,但我也不知道如何做到这一点。我的拟合数据看起来像这样

> head(fitted(inputfit))
Aug 2001 Sep 2001 Oct 2001 Nov 2001 Dec 2001 Jan 2002 
3.534743 3.285140 2.598543 2.271459 1.902562 1.712419 

如何将此数据强制转换为xts格式?

编辑1: 以下是使其可重现的代码:

require("Quandl")
library("zoo")

fromDate = "2001-01-01"
toDate = "2013-12-31"

## Macroeconomic Data

CPI_Data = Quandl("FRED/CPIAUCSL", start_date = fromDate, end_date = toDate, type = "xts")
IP_Data = Quandl("FRED/INDPRO", start_date = fromDate, end_date = toDate, type = "xts")
M1_Data = Quandl("FRED/M1SL", start_date = fromDate, end_date = toDate, type = "xts")

## Yield Curve Data
# 1 month
GS1M_Data = Quandl("FRED/GS1M", start_date = fromDate, end_date = toDate, type = "xts")

# Taking the lag difference of input data
inputminus1 <- lag(GS1M_Data,1)
# Taking the log Difference of CPI_Data
LOGCPI <- (diff(log(CPI_Data), 1))
#Taking the Lag difference of log of CPI_Data
CPIminus1 <- lag(LOGCPI,1)
#Taking the log Difference of M1_Data
LOGM1  <- (diff(log(M1_Data), 1))
#Taking the Lag difference of log of M1_Data
M1minus1  <- lag(LOGM1,1)  
#Taking the log Difference of IP_Data
LOGIP  <- (diff(log(IP_Data), 1))
#Taking the Lag difference of log differenced IP_Data
IPminus1  <- lag(LOGIP,1)
#Merging all the above values along with the original input Data
RegData = merge(GS1M_Data,inputminus1,LOGCPI,CPIminus1,LOGM1,M1minus1,LOGIP,IPminus1)
#Removing NAs
RegData = RegData[complete.cases(RegData),]
colnames(RegData) <- c("y_t", "y_tminus1",
                       "cpi_t", "cpi_tminus1",
                       "m1_t", "m1_tminus1",
                       "ip_t", "ip_tminus1")
# Regression
inputfit <- lm(y_t ~ y_tminus1
               + cpi_t + cpi_tminus1 +
                 m1_t + m1_tminus1 +
                 ip_t + ip_tminus1, data = RegData)

2 个答案:

答案 0 :(得分:1)

在我的测试中,fitted(inputfit)返回了相同的东西(一个命名的数字向量),无论我是在lm对象还是xts对象上运行ts。所以我怀疑是否确实要返回ts个对象。

无论如何,您可以使用fitted(inputfit)as.xts的输出转换回xts:

> head(as.xts(fitted(inputfit), dateFormat="yearmon"))
             [,1]
Aug 2001 3.534743
Sep 2001 3.285140
Oct 2001 2.598543
Nov 2001 2.271459
Dec 2001 1.902562
Jan 2002 1.712419

如果他们不想报名参加Quandl,那么人们就可以参与其中。

require(quantmod)
fromDate = "2001-01-01"
toDate = "2013-12-31"

## Macroeconomic Data
options(getSymbols.auto.assign=FALSE)
CPI_Data <- getSymbols("CPIAUCSL", from=fromDate, to=toDate, src="FRED")
IP_Data <- getSymbols("INDPRO", from=fromDate, to=toDate, src="FRED")
M1_Data <- getSymbols("M1SL", from=fromDate, to=toDate, src="FRED")
## Yield Curve Data
GS1M_Data <- getSymbols("GS1M", from=fromDate, to=toDate, src="FRED")
## Regression data
RegData <- merge(GS1M_Data, lag(GS1M_Data),
  diff(log(CPI_Data)), lag(diff(log(CPI_Data))),
  diff(log(M1_Data)), lag(diff(log(M1_Data))),
  diff(log(IP_Data)), lag(diff(log(IP_Data))))
colnames(RegData) <- c("y_t", "y_tminus1", "cpi_t", "cpi_tminus1",
                       "m1_t", "m1_tminus1", "ip_t", "ip_tminus1")
RegData <- RegData[complete.cases(RegData),]

# Regression
inputfit <- lm(y_t ~ y_tminus1 + cpi_t + cpi_tminus1 +
  m1_t + m1_tminus1 + ip_t + ip_tminus1, data = RegData)

答案 1 :(得分:1)

dyn包将与zoo对象(不是xts)一起使用。只需在lm前加dyn$,如下所示:

library(dyn)

# create test data
set.seed(123)
z <- zoo(rnorm(10))

class(fitted(dyn$lm(z ~ lag(z))))
## [1] "zoo"