# R: product of dummy variables when using factor in a linear regression

I have data in a dataframe with the following columns: date, time, symbol, price I am attempting to run the following model in R

 price ~ factor(time) + factor(symbol)*factor(time) + 0


where the first coefficient comes from a dummy variable of the time column in the dataframe and the second coefficient comes from the product of the dummy variable of the time column and the symbol column. I used lm() to attempt to do the model.

 fit<-lm(price~factor(time) + factor(symbol)*factor(time) + 0, data=mydata)


However, that didn't work as it gets me some really weird coefficients that didn't say anything. I guessing as that was matching effects in some way and not just the straight forward product. I tried making a new variable by multiplying the product of the factors and then putting it in the regression but that didn't work as r told me that * is not in the possible operations for factor(). So, how can you multiply two factors in a linear regression? Your help is much appreciated, thank you in advance.

edit: when I say weird I guess I should more say not what I want. I get coefficients for the firm, then coefficients for time then coefficients for firm and time. However, I only want the coefficients for firm and time to impact the model and ultimately I am really interested in what this does to the intraday effects which are approximated using the time dummy variables. If I am getting the interaction effects of the firm, time and then firm and time then this will impact the first coefficient as opposed to only firm and time affecting the first coefficient?

• Is there are any particular reason you're suppressing the intercept by including + 0 in the formula? – Marius Jun 27 '13 at 4:31
• What do you consider "really weird coefficients"? * means interaction plus main effects in formula syntax. Indeed factors are not something that can be multiplied. Think about what a factor actually is. – Roland Jun 27 '13 at 6:21
• This seems to be less a coding problem and more a conceptual problem. You need to clarify the statistical questions first and then get a handle on how to interpret interaction models... and looking at coefficients is NOT how one goes about accomplishing that task. These are issues that should be handled in a regression course. SO is not set up to repair major gaps in your statistical education. – DWin Jun 27 '13 at 6:33
• Can you write out the regression equation you want to estimate using maths instead of R code? I think people are getting confused about what your statistical model is. To start, you could use $y_{tsi}$ as your response/dependant variable, where $t$ is the time factor, $s$ is the symbol factor, and $i$ is any "repeated" observations with the same value for symbol and time. – probabilityislogic Mar 8 '14 at 21:07
• @Glen_b (and samooch and Marius): The formula expansion of factor(symbol)*factor(time) will include main effects for both symbol and time. Furthermore, the +0 will only change the labeling of the effects. Instead of an (Intercept) term you will see the estimates for interaction of the lowest levels for symbol and time. If you wanted to avoid estimating a main effect for time you would need to use factor(symbol):factor(time) – DWin Jun 17 '16 at 22:42

Try this (which makes sense in statistical model)

fit<-lm(price~factor(time) + factor(symbol)+factor(symbol)*factor(time), data=mydata)

• Wouldn't this be equivalent to lm(price~factor(time)*factor(symbol), mydata)? – Gala Aug 7 '13 at 8:07
• @GaëlLaurans yes it is. f1*f2 expands to f1+f2+f1:f2. stat.ethz.ch/R-manual/R-devel/library/stats/html/formula.html – Marc Claesen Aug 7 '13 at 9:03
• I had thought the ":" would be necessary in order to include an interaction term. Gael Laurans has usefully pointed out that a ":" relationship is implied by the OP's code and by a portion of Metrics'. So what remains to be reckoned with are the first 3 comments. – rolando2 Jan 5 '14 at 23:21

I'm not sure why you're using the factor() function, wouldn't a simple regression such as:

symbti <- symbol * time

price ~ time + symbti


do the trick?

• If time is a factor (a bit unlikely considering the name) it should be a factor and will be dummy encoded automatically by model.matrix. – Roland Jun 27 '13 at 6:18
• I am using factor, so that it will be a 0,1 dummy variable when used in the regression. – samooch Jun 27 '13 at 19:24