# When transform variables to int or to ordinal variables to compute a linear regression model in R?

I am working in R with a database that contains variables as seasons, months, days, temperature, humidity and cnt (integer values). I want to compute a linear model using cnt as the dependent variable and the others as independents. I know seasons, months and days are ordinal variables, but they can be represented as numbers.

Thus, my question is, to compute a linear model when should I transform my variables into ordinal and when is it possible to use them as integers?

• cnt: 100,50,230,... (integer)
• seasons: 1 to 4 (ordinal or integer?)
• month: 1 to 12 (ordinal or integer?)
• day: 1 to 7 (ordinal or integer?)
• temperature: 12.56 , 13.21, ... (numerical)
• humidity: 80.1 , 79.5 , ... (numerical)

Data source: DB day.csv from Bike Sharing Dataset Data Set.

Categorical variables can be used as integer when there are ordered and that order matters in a monotone way to the dependent variable.

For example, grades (A,B,C,D) can be used as integers to explain wages (for example) because they are ordered (A>B>C>D) so we can transform them into integers (A=4, B=3, C=2, D=1) to explain future wage.

In your case it seems that seasons, month and days are not related in a monotone way to the bike dataset so I will convert them into dummies (ej: season 1 = 00, 2 = 01, 3= 10, 4= 11) or factors

That does not mean that you never can use those variables as integers, for example, days after having an untreated infection could be used as integer to predict sepsis, for example.

• Thanks for your answer. In the case, I use months (January, .., November) and days (Monday, .., Saturday) as dummy variables, what would be the reference for my model: every Sunday from December? – rafaoc Apr 7 '19 at 20:38

as LocoGris said, season, month and day are the variables which you can think about using in a categorical way, whereas the others should be treated numerically. I would one-hot-encode (https://en.wikipedia.org/wiki/One-hot) those data. For example season $$1$$ would be $$\left(1,0,0,0\right)$$, whereas season $$3$$ would be $$\left(0,0,1,0\right)$$. This way, you can give the various seasons various weights, because you implicitly break up the seasonal value into $$4$$ values. You can also do this for the days and the months (turning them into $$7$$-d or respective $$12$$-d data). But aside from that: shouldn't the season and the month not be highly correlated?

Adding to the answers of LocoGris and Rho.Pi, you may use the days, weeks and season as numeric variables if you account for the periodicity (the trigonometric functions are useful in this case). It's even better if you use the number of days $$d$$ since a fixed date (say, 1st of January 2000, with negative values corresponding to dates before the reference date) and use this variable in the linear regression:

$$cnt = \beta_0 + \beta_T T + \beta_H H \\ + \alpha_w \sin (2 \pi d / 7) + \beta_w \cos (2 \pi d / 7) \\ + \alpha_m \sin (2 \pi d \times 12 / 365.25) + \beta_m \cos (2 \pi d \times 12 / 365.25) \\ + \alpha_s \sin (2 \pi d \times 4 / 365.25) + \beta_s \cos (2 \pi d \times 4 / 365.25)$$ where $$T$$ and $$H$$ stand for temperature and humidity, respectively and the subscripts $$w$$, $$m$$ and $$s$$ stand for week, month and season, respectively. The value 365.25 is the approximate number of days in the year, considering that some years are leap years (in case the date range goes beyond 100 years, the value should be slightly different).

The use of $$\sin$$ and $$\cos$$ functions allow to include periodic effects, in this case with 3 different periods. By using both $$\sin$$ and $$\cos$$ we model the amplitude of the effect, given by terms like $$\sqrt{\alpha_w^2 + \beta_w}$$, and the phase of the effect (the time at which the wave for that period is at its maximum) is given by $$\arctan \frac{\alpha_w}{\beta_w}$$.