I am trying to fit a model using wind data (0, 359) and time of day (0, 23), but I am concerned that they will poorly fit into a linear regression because they are not themselves linear parameters. I would like to transform them using Python. I have seen some mention of calculating a vector mean by way of taking the sin and cos of the degrees, at least in the wind case, but not a whole lot.

Is there a Python library or relevant method that might be helpful?

  • 1
    $\begingroup$ Thank you for asking this as a question. Note that asking for code or libraries is off-topic (the bulk of your question is certainly on-topic), so that aspect may or may not be covered by answers here. $\endgroup$ Apr 26, 2015 at 15:27
  • $\begingroup$ What is the response variable (outcome, dependent variable) here? Are wind direction and time of day both predictors? $\endgroup$
    – Nick Cox
    Apr 26, 2015 at 16:53
  • $\begingroup$ @NickCox Yes, both wind direction and time of day are predictors. The outcome is a integer value representing particle concentration (air pollution). There are also other other predictors, including temperature, humidity, etc ... but these don't need to be transformed I believe. $\endgroup$
    – compguy24
    Apr 27, 2015 at 1:07
  • 1
    $\begingroup$ I've taken the liberty of editing the title. Previous title "Linear distribution of degrees around a circle" did not capture the question at all in my view. $\endgroup$
    – Nick Cox
    Apr 27, 2015 at 10:16

1 Answer 1


Wind direction (here measured in degrees, presumably as a compass direction clockwise from North) is a circular variable. The test is that the conventional beginning of the scale is the same as the end, i.e. $0^\circ = 360^\circ$. When treated as a predictor it is probably best mapped to sine and cosine. Whatever your software, it is likely to expect angles to be measured in radians, so the conversion will be some equivalent of

$ \sin(\pi\ \text{direction} / 180), \cos(\pi\ \text{direction} / 180)$

given that $2 \pi$ radians $= 360^\circ$. Similarly time of day measured in hours from midnight can be mapped to sine and cosine using

$ \sin(\pi\ \text{time} / 12), \cos(\pi\ \text{time} / 12)$


$ \sin(\pi (\text{time} + 0.5) / 12), \cos(\pi (\text{time} + 0.5) / 12)$

depending on exactly how time was recorded or should be interpreted.

Sometimes nature or society is obliging and dependence on the circular variable takes the form of some direction being optimal for the response and the opposite direction (half the circle away) being pessimal. In that case a single sine and cosine term may suffice; for more complicated patterns you may need other terms. For much more detail a tutorial on this technique of circular, Fourier, periodic, trigonometric regression may be found here, with in turn further references. The good news is that once you have created sine and cosine terms they are just extra predictors in your regression.

There is a large literature on circular statistics, itself seen as part of directional statistics. Oddly, this technique is often not mentioned, as focus in that literature is commonly on circular response variables. Summarising circular variables by their vector means is a standard descriptive method but is not required or directly helpful for regression.

Some details on terminology Wind direction and time of day are in statistical terms variables, not parameters, whatever the usage in your branch of science.

Linear regression is defined by linearity in parameters, i.e. for a vector $y$ predicted by $X\beta$ it is the vector of parameters $\beta$, not the matrix of predictors $X$, that is more crucial. So, in this case, the fact that predictors such as sine and cosine are measured on circular scales and also restricted to $[-1, 1]$ is no barrier to their appearing in linear regression.

Incidental comment For a response variable such as particle concentration I'd expect to use a generalised linear model with logarithmic link to ensure positive predictions.

  • $\begingroup$ Won't these two terms, sin and cos, be correlated or collinear and then harm the model? $\endgroup$
    – skan
    Nov 16, 2022 at 0:19
  • 1
    $\begingroup$ No; recall that sine and cosine are related by sin$^2 + $ cos$^2 = 1$. Their relationship is circular, not linear. Over a very narrow range they could be correlated. $\endgroup$
    – Nick Cox
    Nov 16, 2022 at 0:34
  • $\begingroup$ Hi Nick, I've read your article and how you there describe that for conversions to day or month you use (hour − 0.5)/24 or (month − 0.5)/12. I think it might be a silly question, but is the number in the denominator correct for the hour transformation? As if hour = 24 it's equal to hour 0? I.e., is the denominator chosen as the max value of your transformation or the number of different instances (the latter would make sense as 0, 1,..., 23 is 24 units)? @NickCox $\endgroup$
    – OLGJ
    May 9, 2023 at 13:28
  • $\begingroup$ The answer depends on how you count. If the first hour (an interval) is recorded as 1 and the last as 24 then the midpoint of each hour runs from 0.5 to 23.5. But naturally if the first hour is recorded as 0 and the last as 23, then add 0.5 instead. Otherwise put, no one I know regards month number as running from 0 to 11, and in any case I use 1 to 12 for month number. I am implying similarly that 24 hours are ordered 1 to 24, which is conventional time rounded up to the nearest integer. But what recipe you use depends on your numbering system. $\endgroup$
    – Nick Cox
    May 9, 2023 at 14:08
  • 1
    $\begingroup$ Or indeed 0.5 to 1439.5. It is rarely going to matter. $\endgroup$
    – Nick Cox
    May 9, 2023 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.