Search code examples
rdatelogistic-regression

How does the glm() function in R treat a date variable?


I'm including a date variable in a logit model using glm(). My aim is to have fixed effects based on date, meaning that the model will control for each date. The variable inputted into the function is a date class. Does glm() treat a date class variable like fixed effects? It is not clear to me, because the model doesn't show a coefficient for each date as I would expect. If not, I will transform the date to a factor class.


Solution

  • A date is a numeric variable

    unclass(Sys.Date())
    #[1] 17297
    

    ...so it will be treated like any other number by glm().

    coef(glm(x ~ y, data=data.frame(x=1:4,y=Sys.Date()+0:3)))
    #(Intercept)           y 
    #     -17296           1 
    

    I suspect you want to make it a factor() if you want it as groups with coefficients.

    coef(glm(x ~ y, data=data.frame(x=1:4,y=factor(Sys.Date()+0:3))))
    #(Intercept) y2017-05-12 y2017-05-13 y2017-05-14 
    #          1           1           2           3