2014-02-11 51 views
2

如何使用xts對象進行線性迴歸? lm(xtsObject ~ index(xtsObject))不起作用,我試過了。使用XTS對象進行線性迴歸

我的數據是一家公司的每日股票價格。但index給出了從功能開始的秒數。怎麼解決?

+5

dyn包可以對動物園對象執行迴歸。 –

回答

3

xtsObject和時間索引(如您已經這樣做)提取數據到數據框中,給每個數據框一個合適的名稱。使用該名稱參考公式中的變量並將該數據框作爲參數數據傳遞。例如,使用?xts示例數據:

require("xts") 
data(sample_matrix) 
xtsObject <- as.xts(sample_matrix, descr="my new xts object") 

## the example ts has several variables Open High Low Close, 
## here I take just one, "Open" 
df <- data.frame(xtsObject['/'][,"Open"], Time = index(xtsObject)) 
head(df) 

> head(df) 
       Open  Time 
2007-01-02 50.03978 2007-01-02 
2007-01-03 50.23050 2007-01-03 
2007-01-04 50.42096 2007-01-04 
2007-01-05 50.37347 2007-01-05 
2007-01-06 50.24433 2007-01-06 
2007-01-07 50.13211 2007-01-07 

現在擬合模型

mod <- lm(Open ~ Time, data = df) 
summary(mod) 

> mod <- lm(Open ~ Time, data = df) 
> summary(mod) 

Call: 
lm(formula = Open ~ Time, data = df) 

Residuals: 
    Min  1Q Median  3Q  Max 
-1.16144 -0.47952 -0.08462 0.57053 1.44329 

Coefficients: 
       Estimate Std. Error t value Pr(>|t|)  
(Intercept) 3.199e+02 1.199e+01 26.68 <2e-16 *** 
Time  -2.302e-07 1.020e-08 -22.57 <2e-16 *** 
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.6146 on 178 degrees of freedom 
Multiple R-squared: 0.741, Adjusted R-squared: 0.7395 
F-statistic: 509.2 on 1 and 178 DF, p-value: < 2.2e-16 

lm()一無所知XTS對象,所以如果有疑問,做簡單的事情,它傳遞的東西它確實知道。

請注意,您可以使用coredata(xtsObject)而不是xtsObject['/'],例如,

> head(coredata(xtsObject)) 
     Open  High  Low Close 
[1,] 50.03978 50.11778 49.95041 50.11778 
[2,] 50.23050 50.42188 50.23050 50.39767 
[3,] 50.42096 50.42096 50.26414 50.33236 
[4,] 50.37347 50.37347 50.22103 50.33459 
[5,] 50.24433 50.24433 50.11121 50.18112 
[6,] 50.13211 50.21561 49.99185 49.99185 
+0

我相信你已經提出了一個誤導性的但並非所有在一起不正確的解決方案。閱讀下面的答案,讓我知道你的想法。 –

+0

這不是一個統計幫助網站,而是一個關於編程的網站--OP提出了* no *數據的問題。我編寫了一些數據,並展示瞭如何去做他們想做的事情。由OP決定他們想要的趨勢分量的規模是什麼 - 我能看到的與我實際展示的唯一問題(並不是我會爲我自己的數據做這件事)是估計的趨勢效應是微小的考慮到趨勢變量的大規模。我可以想到爲什麼從0開始趨勢沒有用處的許多原因 - 另一種方法是將時間變量居中。 –

+0

我分別不同意。如果您知道時間趨勢始於1167724800,則您的方法沒問題。但是,這種方法的結果不是很可讀。這就是爲什麼人們在實踐中選擇從1或0開始的時間趨勢(https://en.wikipedia.org/wiki/Linear_trend_estimation)。例如,如果時間趨勢從一開始,則截距代表數據的平均值。出於這個原因,我建議人們考慮下面概述的方法。另外,使用'dynlm'或'dyn'比轉換爲data.frame來使用'lm'更有意義。 –

0

加文辛普森的解決方案是危險的。要看到這一點,請注意,當您在時間趨勢上方運行迴歸爲as.numeric(df$Time)時。此時間趨勢始於1167724800.通常,時間趨勢從0開始。這很重要,因爲如果您不知道時間趨勢的起源,則會錯誤地解釋係數估計。我在下面提出了幾個更好的選擇。

data(sample_matrix) 
xtsObject <- as.xts(sample_matrix, descr="my new xts object") 

#Option 1, the best by far, no need to transform to a data.frame 
library(dynlm) 
dynlm(Open ~ trend(Open), data = xtsObject) 

#Option 2, another option 
library(dynlm) 
xtsObject$t <- 0:(nrow(xtsObject)-1) 
dynlm(Open ~ t, data = xtsObject) 

#Option 3, the data.frame route 
df <- data.frame(xtsObject['/'][,"Open"], t = 1:nrow(xtsObject)) 
lm(Open ~ t, df) 
+0

我的回答是*不*危險。這不是統計建議的網站。我展示了一種使用R來獲得OP所需的方法。你關於不「意識到起源的意識......」是關於稻草人的論點。人們會希望,如果有人用數據擬合迴歸,人們會知道他們的數據是如何被格式化/編碼的,並據此採取行動。 –