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 '15 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 '15 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 '15 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 '15 at 10:16

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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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