9

I'm trying to find a model for my data but I get the message "Coefficients: (3 not defined because of singularities)" These occur for winter, large and high_flow

I found this: https://stats.stackexchange.com/questions/13465/how-to-deal-with-an-error-such-as-coefficients-14-not-defined-because-of-singu

which said it may be incorrect dummy variables, but I've checked that none of my columns are duplicates.

when I use the function alias() I get:

Model :
S ~ A + B + C + D + E + F + G + spring + summer + autumn + winter + small + medium + large + low_flow + med_flow + high_flow

Complete :
          (Intercept) A  B  C  D  E  F  G  spring summer autumn small medium
winter     1           0  0  0  0  0  0  0 -1     -1     -1      0     0    
large      1           0  0  0  0  0  0  0  0      0      0     -1    -1    
high_flow  1           0  0  0  0  0  0  0  0      0      0      0     0    
          low_flow med_flow
winter     0        0      
large      0        0      
high_flow -1       -1      

columns A-H of my data contain numeric values the remaining columns take 0 or 1, and I have checked there are no conflicting values (i.e. if spring = 1 for a case, autumn=summer=winter=0)

model_1 <- lm(S ~ A+B+C+D+E+F+G+spring+summer+autumn+winter+small+medium+large+low_flow+med_flow+high_flow, data = trainOne)
summary(model_1)

Can someone explain the error please?

EDIT: example of my data before I changed it to binary

season  size   flow  A  B   C   D   E   F   G  S
spring small  medium 52 72 134  48 114 114 142 11
autumn small  medium 43 21  98 165 108  23  60 31
spring medium medium 41 45 161  86 177 145  32 12
autumn large  medium 40 86 132  80  82 138 186 16
winter medium  high  49 32 147 189 125  43 144 67
summer large   high  43  9 158  64  14 146  15 71
Laura
  • 177
  • 1
  • 12

3 Answers3

8

The issue is perfect collinearity. Namely,

spring + summer + autumn + winter == 1
small + medium + large == 1
low_flow + med_flow + high_flow == 1
Constant term == 1

By this I mean that those identities hold for each observation individually. (E.g., only one of the seasons is equal to one.)

So, for instance, lm cannot distinguish between the intercept and the sum of all the seasons' effects. Perhaps this or this will help to get the idea better. More technically, the OLS estimates involve a certain matrix that is not invertible in this case.

To fix this, you may run, e.g.,

model_1 <- lm(S ~ A + B + C + D + E + F + G + spring + summer + autumn + small + medium + low_flow + med_flow, data = trainOne)

Also see this question.

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
  • I see! I'm reluctant to drop variables as I would have to drop two of my flow/ size variables which are important. I started off with a column 'size' containing small medium or large, is there a way to keep the column as it is and assign a numeric value based on the entry, so the column stores the characters "small" and 1? From this I could do the same to 'flow' and keep the binary season variables – Laura Dec 31 '18 at 16:25
  • @Laura, that's the whole point that right now you actually have too much information and by dropping those three variables from the equation you wouldn't lose anything. I suggest to read those two references (and perhaps some others) to see how the coefficients can be interpreted. Indeed as @42- suggests, you may want to add `-1` or `+0` as to remove the intercept. In that case, e.g., the coefficient of `spring` would be relative to the effect of omitted `winter`. (I added another reference.) – Julius Vainora Dec 31 '18 at 16:33
3

@JuliusVainora has already given you a good explanation of how the error occurs, which I will not repeat. However, Julius' answer is only one method and might not be satisfying if you don't understand that there really is a value for cases where winter = 1, large=1 and high_flow=1. It can readily be seen in the display as the value for "(Intercept)". You might be able to make the result more interpretable by adding +0 to your formula. (Or it might not, depending on the data situation.)

However, I think that you really should re-examine how your coding of categorical variables is done. You are using a method of one dummy variable per level that you are copying from some other system, perhaps SAS or SPSS? That's going to predictably cause problems for you in the future, as well as being a painful method to code and maintain. R's data.frame function already automagically creates factor's that encode multiple levels in a single variable. (Read ?factor.) So your formula would become:

 S ~ A + B + C + D + E + F + G + season + size + flow

I think SAS and maybe SPSS uses a system where the global mean is used as the reference level. (Could be wrong about that. Haven't used them since 1997.) In that situation there would be coefficients for the contrasts of all levels, but you would need to interpret those values a bit differently. You then need to calculate differences in coefficients to arrive at the values of any contrast. By using "treatment contrasts, the coefficients themselves are immediately interpretable.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • That is what I want my formula to be! How do I turn them into factors (these store characters and a numeric?) I have tried `modelData$flow <- factor(modelData$flow, ordered = TRUE, levels = c(1,2,3))` but I get NA – Laura Dec 31 '18 at 16:32
  • To answer that question I would need to see how the data existed before it was input inot R and what sort of transormation you have done. – IRTFM Dec 31 '18 at 16:37
  • please see my edit. I didn't 'transform' as such, I manually created variables spring etc and assigned 0 or 1 depending on the season column – Laura Dec 31 '18 at 16:46
  • So presumably you did something like `trainOne <- read.table(file="C:/path/filename", header=TRUE)`. In which case the season, size and flow varaibles are already factors and you wouldn't need to do any of that extra dummy coding. Try that formula that I gave. – IRTFM Dec 31 '18 at 17:11
  • Oh I didnt think that lm would work on non-numerical... thanks :) – Laura Dec 31 '18 at 17:21
  • Does [this question](https://stackoverflow.com/q/68975876/16760971) also have the same problem? – Reza Aug 29 '21 at 18:39
  • 1
    Actually the automatic creation of factors is no longer the default behavior of the read.* functions. To get factors you need to use `factor` or set colClasses. The regression function will still consider character vectors as denoting discrete levels and create the correct factor coding for you so the behavior w.r.t. regression is unchanged. – IRTFM Jul 07 '23 at 19:59
0

Some of you variables could be perfectly collinear. Take a look at the variables and how they correlate with each other. You can start inspecting the data with cor(dataset), this will return a correlation matrix of your dataset.

Leevo
  • 1,683
  • 2
  • 17
  • 34