나는 장착 데이터회귀 분석을 수행 한 후 적합한 데이터에 대해 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()
보존 "TS"형식 XTS 형식의 시계열 데이터에 대한 회귀 분석을했지만 "XTS"형식에 대해 동일한 작업을 수행하지 않습니다. 왜 그런가요? 이 문제를 어떻게 극복 할 수 있습니까? 이미 패키지를 사용할 수 있습니까?
다른 옵션은 적합 데이터를 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)
당신이 이것을 [재현 예 (만들 수 http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible- 예) 테스트 데이터를 추가하여? 이렇게하면 가능한 솔루션을 더 쉽게 테스트 할 수 있습니다. – MrFlick
안녕하세요 @ MrFlick, 재현 가능한 코드를 추가했습니다. – user3212376