Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linear regression with xts objects and dummy interaction terms

Tags:

r

xts

I have time series data stored as xts object. When regressing the dependent variable on the independent variable and an interaction term with a dummy, the output turned out to be automatically a regression with the independent variable, the interaction term AND the dummy itself. Here's an example of what I have done:

 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  

This looks a bit odd to me... Is it correct or have I done something wrong?

Thanks! (and please excuse me for auch a basic question.. )

like image 565
user100525 Avatar asked Oct 31 '25 01:10

user100525


1 Answers

That's what * means in a formula. If you just want the interaction term, use : instead. From ?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.

and

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

So you would want to use lm(y ~ x + x:d) (and your initial attempt could be reduced to lm(y ~ x*d)--the first x is redundant).

like image 84
Joshua Ulrich Avatar answered Nov 02 '25 13:11

Joshua Ulrich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!