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)