# Using imaginary parts (complex variables) with GLM in R

I have a dataset of accelerometer readings and I'm using fft to transform my data into frequency domain. Then, I would like to apply glm to find a model.

The problem is that glm does not allow the use of complex variables, and I can't just give up of the imaginary parts.

I'm trying to use Logistic Regression (that's why I'm using glm). Is there a way to do it with the complex variables?

• Not a duplicate but definitely of interest: stats.stackexchange.com/questions/66088/…
– Sycorax
Oct 30, 2015 at 16:40
• I do not understand where the problem lies and so I suspect I might be misreading the description of what you have done. If you're doing logistic regression, then the transformed data must be among the regressors, not the responses. Because (by definition) a complex number is an ordered pair of real numbers, any complex regressor is just two real regressors. If a procedure (such as glm) doesn't like that, just feed it the real parts and imaginary parts separately. Where is the difficulty?
– whuber
Oct 30, 2015 at 20:14
• @whuber I would like to use Logistic Regression with the transformed data. So, I would use the result of FFT, which comes with the imaginary parts. But glm does not accepts the imaginary parts, and I could just get rid of them. Your suggestion sounds good, but I have to "break" the complex number into a pair of real ones. Any hint of how can I do it? Oct 31, 2015 at 23:57
• See the manual page for complex numbers (?complex) and look at the Re and Im functions.
– whuber
Nov 1, 2015 at 14:58

GLM is geared more towards vectors and matrices within linear algebra, mainly for the use of calculating - computing shaders and rendering, transformations, etc. However, they do support Quaternions. In A 3D graphics environment if you try to rotate from all 3 axes individually and independently, it can lead to a phenomenon called Gimbal Lock through the use of Euler Angles of rotation.

You can do a Google and or Youtube search for more information on what they are and how they physically happen. Simply put, when Gimbal Lock occurs, you end up losing one full dimensional degree of freedom. To circumvent this... We prefer to use Quaternions with these types of rotations.

If you are only rotating on one axis, then conventional trig angles vial rotation matrices is fine such as the movement of a limb on a skeleton. Or a baseball player swinging a bat, a pendulum going back and forth... For those, Quaternions would be a bit of over kill. Now, for flight simulation, the rolling of a ball, or any other type of object that can rotate in all three dimensions, this is where we need something a little more sophisticated.

What exactly is a Quaternion? It is almost like a 1x4 or 4x1 vector or matrix, as it is a 4th dimensional unit. But that's only a small part of the truth.

A Quaternion $$Q$$ can be defined as $$q = q0 + iq1 + jq2 + kq3$$. Another representation is of the form: $$(\theta, \hat x, \hat y, \hat z)$$ where $$\theta$$ is the amount of rotation around $$(\hat x, \hat y, \hat z)$$ and $$(\hat x, \hat y, \hat z)$$ is a unit vector that defines the axis of rotation.

How can this help you? I don't know if GLM has any support for complex numbers directly, but I believe through the use of glm/gtc/quaternion.hpp and glm/gtx/quaternion.hppyou can represent both reals and complex numbers and convert from one to the other... You can easily convert a Quaternion to a 3x3 or 4x4 matrix...

You can find their respective documentations here:

And a Great Set Math Video Explanations found here:

The math behind them is fairly complex to look at and get use to as there are quite a few steps in doing it by hand, and many different kinds of tricks and techniques that can be used to work with them, solve form them, convert to another type such as a vector or matrix... extract the Euler angles from the, Extract the Real and or Imaginary Part out of them...

Since you can't use complex directly... Maybe this part of the library might be a good fit for you... You would have to somehow, change the complex type you are using into a Quaternion and from there use the Quaterinon objects to perform the math... At first glance the long method of the math behind them and deriving them would scare most people away... However, in computers and computation, especially with their memory layout and alignment, they are quite fast and efficient for computers to work with and to calculate, they are also extremely cache friendly.

I know this post is old and dated, but no one seemed to give an answer, so I figured I'd submit this, for anyone else who would come across this Q/A, as this could still be good useful information for future readers.

• Another possible solution, would be to include and or use the Eigen Library for doing Linear Algebra... found here: eigen.tuxfamily.org/index.php?title=Main_Page Feb 15, 2021 at 20:50

I would like to apply GLM to find a model

A problem with this question is that it asks for glm to do something, while it is not specified/defined in mathematical terms what sort of models it should look for. (It actually sounds like using GLM as an oracle and naively feeding it with numbers in the hope that something comes.)

I have to "break" the complex number into a pair of real ones. Any hint of how can I do it.

If this comment is the true question then it has been answered in another comment by Whuber. You can use the Re and Im functions. https://stat.ethz.ch/R-manual/R-patched/library/base/html/complex.html