# Why would R return NA as a lm() coefficient?

I am fitting an lm() model to a data set that includes indicators for the financial quarter (Q1, Q2, Q3, making Q4 a default). Using lm(Y~., data = data) I get a NA as the coefficient for Q3, and a warning that one variable was exclude because of singularities.

Do I need to add a Q4 column?

NA as a coefficient in a regression indicates that the variable in question is linearly related to the other variables. In your case, this means that $Q3 = a \times Q1 + b \times Q2 + c$ for some $a, b, c$. If this is the case, then there's no unique solution to the regression without dropping one of the variables. Adding $Q4$ is only going to make matters worse.

• I agree... there seems to be a problem with the dummy variables definitions. Commented Apr 3, 2012 at 23:42
• (+1). NA more generally means that the coefficient is not estimable. This can happen due to exact collinearity, as you've mentioned. But, it can also happen due to not having enough observations to estimate the relevant parameters (e.g. if $p > n$). If you predictors are categorical and you're adding interaction terms, an NA can also mean that there are no observations with that combination of levels of the factors. Commented Apr 4, 2012 at 0:59
• $p > n$ is just a special case of colinearity - if there are fewer observations than predictors, colinearity is a given. You're right about interaction terms though, although I'm pretty sure that's not what's happening here. Commented Apr 4, 2012 at 1:03
• The variables are not linearly related, as Q3=1 iff Q1=Q2=0. Moreover, using stepAIC() and forcing the model to include all three of those variables causes no problems. Also, I have roughly 3x's the number of observations to variables. My best guess is there is colinearity between Q3 and some other variable, which I guess is one not included by the stepAIC. Commented Apr 4, 2012 at 4:43

I found this behavior when attempting to fit observations vs time, where time was given as POSIXct. lm and lsfit() both determined that the x's were co-linear. The problem was solved by subtracting the mean of the time datum to do the fit.

This appears to be a deficiency in the underlying code -- there must be some single precision operations, or non-optimal order of operations. I have never seen it before, so it may be new.

• The problem is well known. It arises because of a huge condition number, due to the fact that POSIXct values for recent dates are in the billions. When squared--as is necessary in the lm calculations--these values wipe out squares of other entries (such as the intercept) even in double precision. I suspect everybody gets nailed by this problem at some point in their career ;-).
– whuber
Commented Sep 17, 2021 at 21:23
• Specifically, there is no single precision code involved. R doesn't use single precision anywhere in its computations. Commented Sep 18, 2021 at 0:14

I also got this behavior in R version 4.2.0 with an integer64 dataframe column.

It was being fetched by an SQL query via RPostgres, from a PostgreSQL column of type int8 (that's 8-byte/64-bit integer).

Luckily, the data didn't actually exceed the 2-billion 32-bit cap; so a simple downconversion helped:

df$$some_field <- as.integer(df$$some_field)


Before I did that, lm would produce both NaN's and NA's in the coefficients.

How to diagnose: use class(df\$some_field) to see which type the fields have.