I am transitioning certain regression tasks from SAS to R. These are garden variety hedonic price regressions run against time-series cross-section sales datasets. As a typical example, consider a dataset called Sales that includes fields ParcelID, SaleYear, SalePrice plus a suite of property characteristics Bdrms, Baths, etc. (ParcelID,SaleYear) is a key for the table, and suppose it's successfully been read into an R dataframe.
I want to augment Sales with a series of annual dummies, e.g. d2000, d2001, ... d2014 based on the value of SaleYear. In SAS/SQL I do this using a select * statement containing a macro with a for loop that creates and names each dummy using a case statement. This yields a new dataset which includes the desired dummies.
Apparently R can do this more elegantly with factor() and model.matrix() and doubtless many other ways as well. My problem is that at this stage of my R career I'm not able to adapt solutions to similar problems posted on stackoverflow to my particular problem.
Also, our naming conventions require that all dummy variable names be of the form d_*.
Then there's the matter of specifying dummies in a regression call. Proc reg in SAS allows an indexed series of explanatory variables with integer suffix to specified in the model statement in an abbreviated form (a numbered range list), e.g. d_2000-d_2002 instead of d_2000 d_2001 d_2002. I believe there is a nice way to do this in R's lm() facility as well. I don't want, however, just to include dummies corresponding to all distinct values in SaleYear less a reference category selected by R. Model variations use different spans of years for development and testing, so I want to be able to conveniently specify the range of annual dummies to be included.
Thanks very much in advance. I realize these are rather naïve questions, but I hope to be able to answer such myself with a little more R practice and some suggestions. The interaction variables will be the next challenge.
Thanks again.
interaction
function that takes factors as arguments. – IRTFM