Coder Social home page Coder Social logo

Comments (7)

TiagoOlivoto avatar TiagoOlivoto commented on July 18, 2024

Dear @drschowdary , thanks for reporting this issue. This probably occurs because the correlation matrix between predictors (cor_mat) is not positive definite. Could you please provide a reproducible example? See how to use reprex to do that (https://reprex.tidyverse.org/)

Here, I show a similar error, forcing an exactly singular matrix (determinant = 0)

library(metan)
#> Registered S3 method overwritten by 'GGally':
#>   method from   
#>   +.gg   ggplot2
#> |=========================================================|
#> | Multi-Environment Trial Analysis (metan) v1.16.0        |
#> | Author: Tiago Olivoto                                   |
#> | Type 'citation('metan')' to know how to cite metan      |
#> | Type 'vignette('metan_start')' for a short tutorial     |
#> | Visit 'https://bit.ly/pkgmetan' for a complete tutorial |
#> |=========================================================|
mat <- 
  data_ge2 %>% 
  mutate(EH2 = EH) %>% # force a perfectly correlated predictor
  select_numeric_cols() %>% 
  corr_coef()

pcoeff <- path_coeff_mat(mat$cor, resp = EP)
#> Error in solve.default(cor.x, cor.y): Lapack routine dgesv: system is exactly singular: U[15,15] = 0

Created on 2021-12-27 by the reprex package (v2.0.1)

from metan.

TiagoOlivoto avatar TiagoOlivoto commented on July 18, 2024

Since this clearly is not a bug but an issue related to the data, I'm closing this issue. Feel free to re-open it if necessary.

from metan.

drschowdary avatar drschowdary commented on July 18, 2024

from metan.

TiagoOlivoto avatar TiagoOlivoto commented on July 18, 2024

@drschowdary, path analysis is done in GENES probably because GENES uses a different method of matrix inversion, which returns an approximate inverse of a matrix even when it is singular (see an example in metan at https://tiagoolivoto.github.io/metan/reference/solve_svd.html). I strongly suggest you check the multicollinearity of the matrix of predictor traits (use metan::colindiag()), because your path coefficients might be highly biased.

from metan.

drschowdary avatar drschowdary commented on July 18, 2024

from metan.

TiagoOlivoto avatar TiagoOlivoto commented on July 18, 2024

@drschowdary, I just changed the method of matrix inversion so that a pseudo-inverse is computed even with a singular matrix. A warning will alert you about this problem. To get this new feature, install the development version of metan with

devtools::install_github("TiagoOlivoto/metan")

After installing the development version, you'll be able to run the following examples. There are two main ways of dealing with multicollinearity (see our paper for more info). The first (and that I'd suggest) is removing the traits that are being the main cause of the problem. The second is including a correction factor (k) in the diagonal of the correlation matrix.

library(metan)
#> Registered S3 method overwritten by 'GGally':
#>   method from   
#>   +.gg   ggplot2
#> |=========================================================|
#> | Multi-Environment Trial Analysis (metan) v1.16.0        |
#> | Author: Tiago Olivoto                                   |
#> | Type 'citation('metan')' to know how to cite metan      |
#> | Type 'vignette('metan_start')' for a short tutorial     |
#> | Visit 'https://bit.ly/pkgmetan' for a complete tutorial |
#> |=========================================================|

df_colinear <- 
  data_ge2 %>% 
  select(PH, EP, KW, NKE) %>% 
  mutate(PH2 = PH)

# now it runs with a warning
pc1 <- path_coeff(df_colinear, resp = KW)
#> Warning: System is computationally singular. Check the collinearity of
#> predictors with `metan::colindiag()`.
#> Severe multicollinearity. 
#> Condition Number: 50796137447998448
#> Consider using a correction factor with 'correction' argument.
#> Consider identifying collinear traits with `non_collinear_vars()`
plot(pc1)

# include a correction factor, say, 0.05
pc2 <- path_coeff(df_colinear, resp = KW, correction = 0.05)
#> Weak multicollinearity. 
#> Condition Number: 56.782
#> You will probably have path coefficients close to being unbiased.
plot(pc2)

# identifying colinear traits
diagnosis <- colindiag(df_colinear)
print(diagnosis)
#> Severe multicollinearity in the matrix! Pay attention on the variables listed bellow
#> CN = 9413786837639034
#> Matrix determinant: 0 
#> Largest correlation: PH x PH2 = 1 
#> Smallest correlation: EP x NKE = 0.233 
#> Number of VIFs > 10: 0 
#> Number of correlations with r >= |0.8|: 1 
#> Variables with largest weight in the last eigenvalues: 
#> PH2 > PH > KW > NKE > EP

# removing the collinear trait PH2
# option 1
pc3 <- 
  df_colinear %>% 
  remove_cols(PH2) %>% 
  path_coeff(resp = KW)
#> Weak multicollinearity. 
#> Condition Number: 6.119
#> You will probably have path coefficients close to being unbiased.
plot(pc3)

# option 2
pc4 <- path_coeff(df_colinear, resp = KW, pred = -PH2)
#> Weak multicollinearity. 
#> Condition Number: 6.119
#> You will probably have path coefficients close to being unbiased.
plot(pc4)

Created on 2021-12-30 by the reprex package (v2.0.1)

from metan.

drschowdary avatar drschowdary commented on July 18, 2024

from metan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.