使用XTS对象进行线性回归

时间:2014-02-11 03:34:27

标签: r xts linear-regression

如何使用xts对象进行线性回归? lm(xtsObject ~ index(xtsObject))不起作用,我试过了。

我的数据是公司的每日股票价格。但是index给出了从纪元到lm函数的秒数。怎么解决?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

# Load library
library(tsbox)

# Convert xts to dataframe
dataframe = ts_data.frame(xts)

# See dataframe header
head(dataframe)

# Run regression
fit = lm(value ~ time, dataframe)

# Find result
summary(fit)

答案 2 :(得分:0)

Gavin Simpson的解决方案很危险。要看到这一点,请注意,当您在上面运行回归时,时间趋势为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)