xts 对象和虚拟交互项的线性回归

linear regression with xts objects and dummy interaction terms

我将时间序列数据存储为 xts 对象。当对自变量的因变量和与虚拟变量的交互项进行回归时,输出结果自动是与自变量、交互项和虚拟变量本身的回归。这是我所做的示例:

 x <- xts(rnorm(100,0,1), Sys.Date()-100:1)
 y <- xts(rnorm(100, 1, 1), Sys.Date()-100:1)
 d <- xts(order.by = index(x))
 d <- merge(d, dummy = 1)
 d["/2016-09-06"] <- 0

 Call:
 lm(formula = y ~ x + x * d)

 > Coefficients:
  (Intercept)            x            d          x:d  
        0.95559      0.07350      0.29469     -0.09851  

我觉得这有点奇怪...这是正确的还是我做错了什么?

谢谢! (请原谅我问一个基本的问题..)

这就是 * 在公式中的意思。如果您只需要交互项,请改用 :。来自 ?formula:

The terms themselves consist of variable and factor names separated by ‘:’ operators. Such a term is interpreted as the interaction of all the variables and factors appearing in the term.

The ‘*’ operator denotes factor crossing: ‘a*b’ interpreted as ‘a+b+a:b’.

所以你会想要使用 lm(y ~ x + x:d)(你的初始尝试可以减少到 lm(y ~ x*d)——第一个 x 是多余的)。