Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error : torch._C._LinAlgError #119

Open
agnes-duhamet opened this issue Apr 24, 2023 · 3 comments
Open

Error : torch._C._LinAlgError #119

agnes-duhamet opened this issue Apr 24, 2023 · 3 comments

Comments

@agnes-duhamet
Copy link

Hi,
I would like to model marine fish species distribution in function of distance to the coast, depth, and marine habitat taking into account spatial autocorrelation and species interaction. I think that this package sjSDM could be appropriate to model this but when I try to run sjSDM model I have this error : Error : torch._C._LinAlgError: linalg.inv: The diagonal element 3 is zero, the inversion could not be completed because the input matrix is singular.

I wrote :
model <- sjSDM(Y = Occ, env = linear(data = env_var, formula = ~dist_land+depth), spatial = linear(data = SP, formula = ~longitude_start_DD:latitude_start_DD), se = TRUE, family=binomial("probit"), sampling = 100L)

Here are the data:
sjsdm.zip

Would you know where the error comes from and how I can avoid it?

Thanks in advance,

Agnès

@MaximilianPi
Copy link
Member

Hi @agnes-duhamet ,

You can avoid the errors by scaling your variables:

library(sjSDM)
library(tidyverse)
model <- sjSDM(Y = Occ, env = linear(data = env_var %>% scale, formula = ~dist_land+depth), 
               spatial = linear(data = SP %>% scale, formula = ~0+longitude_start_DD:latitude_start_DD), se = TRUE, family=binomial("probit"), sampling = 100L)
summary(model)
summary(model)
Family:  binomial 

LogLik:  -310.8243 
Regularization loss:  0 

Species-species correlation matrix: 

	sp1	 1.0000									
	sp2	 0.6450	 1.0000								
	sp3	 0.4290	 0.4650	 1.0000							
	sp4	 0.5850	 0.5350	 0.4190	 1.0000						
	sp5	 0.1690	 0.2080	-0.0300	 0.2470	 1.0000					
	sp6	 0.5190	 0.5700	 0.2250	 0.6510	 0.2880	 1.0000				
	sp7	 0.7190	 0.8190	 0.5270	 0.7510	 0.2250	 0.7490	 1.0000			
	sp8	 0.6960	 0.7040	 0.4380	 0.7620	 0.2300	 0.7480	 0.8900	 1.0000		
	sp9	 0.6600	 0.7360	 0.5410	 0.7440	 0.1870	 0.7130	 0.9100	 0.8490	 1.0000	
	sp10	 0.4940	 0.6260	 0.3080	 0.5730	 0.3270	 0.5730	 0.6700	 0.6010	 0.6180	 1.0000



Spatial: 
                                           sp1        sp2        sp3       sp4        sp5         sp6       sp7       sp8       sp9      sp10
longitude_start_DD:latitude_start_DD 0.3700787 0.08187833 0.03864665 0.4418196 0.07880618 0.001866536 0.9838676 0.8267187 0.4871799 0.5944506



                                     Estimate Std.Err Z value Pr(>|z|)    
Conger_conger (Intercept)              -1.469   0.233   -6.29  3.2e-10 ***
Conger_conger dist_land                -0.649   0.742   -0.87  0.38175    
Conger_conger depth                     1.091   0.608    1.79  0.07267 .  
Muraena_helena (Intercept)             -1.192   0.357   -3.34  0.00083 ***
Muraena_helena dist_land               -0.515   0.805   -0.64  0.52199    
Muraena_helena depth                    2.194   1.267    1.73  0.08349 .  
Synodus_saurus (Intercept)             -2.413   0.491   -4.91  8.9e-07 ***
Synodus_saurus dist_land               -0.511   1.725   -0.30  0.76690    
Synodus_saurus depth                   -0.182   1.255   -0.14  0.88493    
Belone_belone (Intercept)              -1.532   0.299   -5.12  3.0e-07 ***
Belone_belone dist_land                 0.237   0.308    0.77  0.44176    
Belone_belone depth                    -0.929   0.909   -1.02  0.30672    
Cololabis_saira (Intercept)            -0.994   0.181   -5.50  3.8e-08 ***
Cololabis_saira dist_land               0.799   0.444    1.80  0.07204 .  
Cololabis_saira depth                  -0.587   0.451   -1.30  0.19343    
Aidablennius_sphynx (Intercept)        -1.918   0.546   -3.51  0.00045 ***
Aidablennius_sphynx dist_land          -0.732   2.042   -0.36  0.72000    
Aidablennius_sphynx depth              -0.364   1.550   -0.23  0.81423    
Lipophrys_trigloides (Intercept)       -1.209   0.518   -2.33  0.01971 *  
Lipophrys_trigloides dist_land         -1.373   1.230   -1.12  0.26426    
Lipophrys_trigloides depth             -1.257   1.358   -0.93  0.35486    
Parablennius_gattorugine (Intercept)   -1.593   0.393   -4.05  5.1e-05 ***
Parablennius_gattorugine dist_land     -0.792   0.850   -0.93  0.35154    
Parablennius_gattorugine depth         -0.691   1.208   -0.57  0.56747    
Parablennius_incognitus (Intercept)    -1.522   0.779   -1.95  0.05087 .  
Parablennius_incognitus dist_land      -2.650   3.564   -0.74  0.45716    
Parablennius_incognitus depth          -0.203   1.888   -0.11  0.91445    
Parablennius_pilicornis (Intercept)    -2.598   0.559   -4.65  3.3e-06 ***
Parablennius_pilicornis dist_land      -0.616   2.317   -0.27  0.79046    
Parablennius_pilicornis depth          -0.139   1.358   -0.10  0.91832    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

@agnes-duhamet
Copy link
Author

Thanks a lot.
Now I try to add the qualitative variable of marine habitat (called habitat_principal). When I add this variable I have the same error : Error : torch._C._LinAlgError: linalg.inv: The diagonal element 4 is zero, the inversion could not be completed because the input matrix is singular.
Is it possible to add qualitative variable in s-jSDM model ?
sjsdm_marine_habitat.zip

Agnès

@MaximilianPi
Copy link
Member

Hi @agnes-duhamet,

No it is possible, the problem is that you have empty levels in your habitat_principal variable:

library(sjSDM)
library(tidyverse)
env_var_scaled = env_var %>% 
  mutate(dist_land = scale(dist_land), depth = scale(depth), habitat_principal = droplevels(habitat_principal)) 
model <- sjSDM(Y = Occ, env = linear(data = env_var_scaled, formula = ~dist_land+depth+habitat_principal), 
               spatial = linear(data = SP %>% scale, formula = ~0+longitude_start_DD:latitude_start_DD), se = TRUE, family=binomial("probit"), sampling = 100L)
summary(model)

Family:  binomial 

LogLik:  -306.3565 
Regularization loss:  0 

Species-species correlation matrix: 

	sp1	 1.0000									
	sp2	 0.7840	 1.0000								
	sp3	 0.5270	 0.5870	 1.0000							
	sp4	 0.7020	 0.6600	 0.4720	 1.0000						
	sp5	 0.1280	 0.2010	-0.1490	 0.2150	 1.0000					
	sp6	 0.6960	 0.6510	 0.3350	 0.8300	 0.2960	 1.0000				
	sp7	 0.8490	 0.8720	 0.6590	 0.8510	 0.1560	 0.8080	 1.0000			
	sp8	 0.8330	 0.7750	 0.5380	 0.8620	 0.1680	 0.8450	 0.9330	 1.0000		
	sp9	 0.8120	 0.8120	 0.6850	 0.8510	 0.1060	 0.7850	 0.9590	 0.9160	 1.0000	
	sp10	 0.6040	 0.7540	 0.3140	 0.6530	 0.4010	 0.6870	 0.7190	 0.6540	 0.6540	 1.0000



Spatial: 
                                           sp1     sp2       sp3       sp4       sp5        sp6     sp7      sp8       sp9      sp10
longitude_start_DD:latitude_start_DD 0.6936179 0.30476 0.2282074 0.8361171 0.5391262 0.08481123 1.42469 1.167874 0.3563441 0.5948695



                                                                                      Estimate Std.Err Z value Pr(>|z|)  
Conger_conger (Intercept)                                                               -1.349   1.592   -0.85    0.397  
Conger_conger dist_land                                                                 -0.324   1.080   -0.30    0.764  
Conger_conger depth                                                                      1.098   1.164    0.94    0.345  
Conger_conger habitat_principalBiocenose de l herbier a Posidonia oceanica              -0.763   1.647   -0.46    0.643  
Conger_conger habitat_principalBiocenose des algues infralittorales                     -1.518   2.024   -0.75    0.453  
Conger_conger habitat_principalFonds meubles circalittoraux                             -1.743   1.763   -0.99    0.323  
Conger_conger habitat_principalZone bathyale                                             3.032   7.339    0.41    0.680  
Muraena_helena (Intercept)                                                              -1.746   2.490   -0.70    0.483  
Muraena_helena dist_land                                                                -0.673   1.052   -0.64    0.522  
Muraena_helena depth                                                                     2.999   1.983    1.51    0.130  
Muraena_helena habitat_principalBiocenose de l herbier a Posidonia oceanica              0.135   2.515    0.05    0.957  
Muraena_helena habitat_principalBiocenose des algues infralittorales                    -3.399   5.103   -0.67    0.505  
Muraena_helena habitat_principalFonds meubles circalittoraux                            -0.286   2.626   -0.11    0.913  
Muraena_helena habitat_principalZone bathyale                                            0.600  22.189    0.03    0.978  
Synodus_saurus (Intercept)                                                              -2.446   2.477   -0.99    0.323  
Synodus_saurus dist_land                                                                -0.596   2.930   -0.20    0.839  
Synodus_saurus depth                                                                    -0.189   5.089   -0.04    0.970  
Synodus_saurus habitat_principalBiocenose de l herbier a Posidonia oceanica             -0.835   2.335   -0.36    0.721  
Synodus_saurus habitat_principalBiocenose des algues infralittorales                    -3.378   5.170   -0.65    0.513  
Synodus_saurus habitat_principalFonds meubles circalittoraux                            -2.022   2.564   -0.79    0.430  
Synodus_saurus habitat_principalZone bathyale                                           -1.872  22.526   -0.08    0.934  
Belone_belone (Intercept)                                                               -1.585   1.691   -0.94    0.348  
Belone_belone dist_land                                                                  0.372   0.508    0.73    0.464  
Belone_belone depth                                                                     -1.272   1.365   -0.93    0.351  
Belone_belone habitat_principalBiocenose de l herbier a Posidonia oceanica              -0.668   1.733   -0.39    0.700  
Belone_belone habitat_principalBiocenose des algues infralittorales                     -1.912   2.141   -0.89    0.372  
Belone_belone habitat_principalFonds meubles circalittoraux                             -0.905   1.799   -0.50    0.615  
Belone_belone habitat_principalZone bathyale                                            -0.929  12.665   -0.07    0.942  
Cololabis_saira (Intercept)                                                             -0.760   1.657   -0.46    0.646  
Cololabis_saira dist_land                                                                1.396   0.649    2.15    0.032 *
Cololabis_saira depth                                                                   -0.733   0.719   -1.02    0.308  
Cololabis_saira habitat_principalBiocenose de l herbier a Posidonia oceanica            -0.292   1.691   -0.17    0.863  
Cololabis_saira habitat_principalBiocenose des algues infralittorales                   -4.949   3.568   -1.39    0.165  
Cololabis_saira habitat_principalFonds meubles circalittoraux                           -1.988   1.830   -1.09    0.277  
Cololabis_saira habitat_principalZone bathyale                                          -2.830   6.518   -0.43    0.664  
Aidablennius_sphynx (Intercept)                                                         -1.412   3.227   -0.44    0.662  
Aidablennius_sphynx dist_land                                                           -1.157   3.287   -0.35    0.725  
Aidablennius_sphynx depth                                                               -0.605  11.645   -0.05    0.959  
Aidablennius_sphynx habitat_principalBiocenose de l herbier a Posidonia oceanica        -1.529   2.019   -0.76    0.449  
Aidablennius_sphynx habitat_principalBiocenose des algues infralittorales               -3.353   5.574   -0.60    0.547  
Aidablennius_sphynx habitat_principalFonds meubles circalittoraux                       -1.969   2.122   -0.93    0.353  
Aidablennius_sphynx habitat_principalZone bathyale                                      -1.374  54.279   -0.03    0.980  
Lipophrys_trigloides (Intercept)                                                        -1.101   5.763   -0.19    0.849  
Lipophrys_trigloides dist_land                                                          -2.051   1.741   -1.18    0.239  
Lipophrys_trigloides depth                                                              -1.663  14.775   -0.11    0.910  
Lipophrys_trigloides habitat_principalBiocenose de l herbier a Posidonia oceanica       -0.484   4.778   -0.10    0.919  
Lipophrys_trigloides habitat_principalBiocenose des algues infralittorales              -0.274   5.058   -0.05    0.957  
Lipophrys_trigloides habitat_principalFonds meubles circalittoraux                      -1.451   4.866   -0.30    0.766  
Lipophrys_trigloides habitat_principalZone bathyale                                     -1.860  70.143   -0.03    0.979  
Parablennius_gattorugine (Intercept)                                                    -1.288   2.940   -0.44    0.661  
Parablennius_gattorugine dist_land                                                      -1.015   1.483   -0.68    0.494  
Parablennius_gattorugine depth                                                          -0.999   6.260   -0.16    0.873  
Parablennius_gattorugine habitat_principalBiocenose de l herbier a Posidonia oceanica   -0.920   2.664   -0.35    0.730  
Parablennius_gattorugine habitat_principalBiocenose des algues infralittorales          -0.189   3.060   -0.06    0.951  
Parablennius_gattorugine habitat_principalFonds meubles circalittoraux                  -2.078   2.744   -0.76    0.449  
Parablennius_gattorugine habitat_principalZone bathyale                                 -1.889  30.431   -0.06    0.950  
Parablennius_incognitus (Intercept)                                                     -1.030  22.707   -0.05    0.964  
Parablennius_incognitus dist_land                                                       -2.986   6.828   -0.44    0.662  
Parablennius_incognitus depth                                                           -0.731  98.867   -0.01    0.994  
Parablennius_incognitus habitat_principalBiocenose de l herbier a Posidonia oceanica    -1.212   3.089   -0.39    0.695  
Parablennius_incognitus habitat_principalBiocenose des algues infralittorales            2.030   3.446    0.59    0.556  
Parablennius_incognitus habitat_principalFonds meubles circalittoraux                   -2.297   3.203   -0.72    0.473  
Parablennius_incognitus habitat_principalZone bathyale                                  -1.419 463.930    0.00    0.998  
Parablennius_pilicornis (Intercept)                                                     -2.542   2.229   -1.14    0.254  
Parablennius_pilicornis dist_land                                                       -0.372   1.822   -0.20    0.838  
Parablennius_pilicornis depth                                                           -0.361   3.243   -0.11    0.911  
Parablennius_pilicornis habitat_principalBiocenose de l herbier a Posidonia oceanica    -1.530   2.230   -0.69    0.493  
Parablennius_pilicornis habitat_principalBiocenose des algues infralittorales            0.533   2.510    0.21    0.832  
Parablennius_pilicornis habitat_principalFonds meubles circalittoraux                   -2.445   2.410   -1.01    0.310  
Parablennius_pilicornis habitat_principalZone bathyale                                  -1.953  16.290   -0.12    0.905  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants