r - How can I preserve xts format for fitted data after regression? -
r - How can I preserve xts format for fitted data after regression? -
i did regression on time series info in xts format
> inputfit <- lm(y_t ~ y_tminus1 + cpi_t + cpi_tminus1 + m1_t + m1_tminus1 + ip_t + ip_tminus1, info = regdata) > class(regdata) [1] "xts" "zoo" class(fitted(inputfit)) [1] "numeric" lm() preserves "ts" format fitted info doesn't same "xts" format. why that? how can overcome problem? there bundle available?
the other alternative coerce fitted info xts don't know how either. fitted info looks 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 coerce info xts format?
thanks in advance!!!
edit 1: here code create reproducible.
require("quandl") library("zoo") fromdate = "2001-01-01" todate = "2013-12-31" ## macroeconomic info 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 info # 1 month gs1m_data = quandl("fred/gs1m", start_date = fromdate, end_date = todate, type = "xts") # taking lag difference of input info inputminus1 <- lag(gs1m_data,1) # taking log difference of cpi_data logcpi <- (diff(log(cpi_data), 1)) #taking lag difference of log of cpi_data cpiminus1 <- lag(logcpi,1) #taking log difference of m1_data logm1 <- (diff(log(m1_data), 1)) #taking lag difference of log of m1_data m1minus1 <- lag(logm1,1) #taking log difference of ip_data logip <- (diff(log(ip_data), 1)) #taking lag difference of log differenced ip_data ipminus1 <- lag(logip,1) #merging above values along original input info 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, info = regdata) hope helps!!!
in tests, fitted(inputfit) returned same thing (a named numeric vector) whether ran lm on ts object or xts object. i'm skeptical ts object beingness returned.
regardless, can convert output of fitted(inputfit) 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 illustration people can run if don't want sign quandl.
require(quantmod) fromdate = "2001-01-01" todate = "2013-12-31" ## macroeconomic info 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 info gs1m_data <- getsymbols("gs1m", from=fromdate, to=todate, src="fred") ## regression info 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, info = regdata) r regression xts
Comments
Post a Comment