Skip to content

Linear Mixed Effects Models: numpy.linalg.LinAlgError: Singular matrix #6140

Open

Description

@KaninchenM

I'm going to find the correlation between the contribution of each type and the 'score'. So I use the proportion of each type as X, the score as Y. I have 150 types and obviously each sample has: x1+x2+...x150=1. Y(score) ∈(0,10) and Y is float. Xi ∈[0,1] and also is float. Each sample has the lable 'region' which shows the area it comes from. I want to use the mixed liner model, where the 150 types(x1,x2...x150) make fixed effect and the 'region' makes the random effect.
I have tried:

md = smf.mixedlm(formula_md,df,groups=df['region'])
mdf = md.fit() 

where the formula_md is:

score ~ class_0 + class_1+ class_2 ... +class_149 - 1

but got the error: numpy.linalg.LinAlgError: Singular matrix
Does this mean that there is collinearity in my data? I think the answer is yes, I have following questions:

  1. Is there any ways for me to fix the collinearity, such as fine the samples pair which has collinearity. After that maybe I can drop some samples.
  2. Does this mean that my data situation is not suitable for mixed linear models? But if I really need to take into account the impact of spatial relationships, whatshould I do?
  3. I think the statsmodels use Maximum Likelihood Estimation to fix the mixed liner model and use linalg.py( of numpy) to solve a linear matrix equation ax = y. But the file (linalg.py) requests a must be square and of full-rank, i.e., all rows (or, equivalently, columns) must be linearly independent; if either is not true, use lstsq for the least-squares best "solution" of the system/equation.' So probably OLS is suitable for my data?

Activity

  1. kshedden commented on Aug 29, 2019

    @kshedden
    Contributor

    I would start with this:

    u, s, vt = np.linalg.svd(md.exog, 0)
    

    The elements of 's' are the singular values of the fixed effects design matrix. The more zeros (or extremely small values) there are in 's', the more collinearity you have. Non-convergence can happen for other reasons besides collinearity, so it is good to start with this check.

  2. KaninchenM commented on Sep 4, 2019

    @KaninchenM
    Author

    A good way to inspect collinearity. Thank you so much. But I still haven't found a good way to process my data. Hope I can find and update here later.

  3. kshedden commented on Sep 4, 2019

    @kshedden
    Contributor

    Dropping samples will only make things worse. It seems that your regression model is not identified. In general, that means that you should eliminate covariates until you get an identified regression. If you want to automate this process, you could use scipy.qr with pivot=True to find a maximal set of linearly independent columns of your design matrix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions