1

I did a regression on a time series data in xts format

> 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() preserves "ts" format for fitted data but doesn't do the same for "xts" format. Why is that? How can I overcome this problem? Is there already a package available?

The other option is that I coerce the fitted data to xts but I don't know how to do that either. My fitted data looks like this

> 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 

How can I coerce this data to an xts format?

Edit 1: Here is the code to make it reproducible:

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)
Cleb
  • 25,102
  • 20
  • 116
  • 151
user3212376
  • 237
  • 1
  • 4
  • 12
  • Can you make this a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by adding test data? This will make it easier to test possible solutions. – MrFlick Jun 23 '14 at 07:02
  • Hi @MrFlick , I have added the reproducible code. – user3212376 Jun 23 '14 at 08:02

2 Answers2

1

In my tests, fitted(inputfit) returned the same thing (a named numeric vector) whether I ran lm on a ts object or an xts object. So I'm skeptical that a ts object is really being returned.

Regardless, you can convert the output of fitted(inputfit) back to xts using as.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

And here's an example people can run if they don't want to sign up for 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)
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Thanks Joshua! I also managed to coerce the fitted data back to xts using the same method described by you. Right now, I don't have access to the setup which had the example where ts object was being preserved. In that example the time series data was different though. I will try and experiment a bit more once I have the setup back and report my results accordingly. – user3212376 Jun 23 '14 at 14:28
  • I couldn't help but notice `options(getSymbols.auto.assign=FALSE)`. Only if I knew about this option earlier, I would have also posted the question with `quantmod` instead of `Quandl`. Thanks again. – user3212376 Jun 23 '14 at 14:39
1

The dyn package will work with zoo objects (not xts). Just preface lm with dyn$ as shown:

library(dyn)

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

class(fitted(dyn$lm(z ~ lag(z))))
## [1] "zoo"
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341