r-spatial / spdep

Spatial Dependence: Weighting Schemes and Statistics
https://r-spatial.github.io/spdep/
122 stars 26 forks source link

SPDEP et CARBayesST #129

Closed gadouaka closed 11 months ago

gadouaka commented 1 year ago

Apres avoir exécuté le modèle suivant: model4 <- CARBayesST::ST.CARar(formula = formula1, family = "poisson", data = DISTRICT, W=W, AR=1, burnin = 20000, n.sample = 220000, thin = 10) le message d'errueur suivant s'affiche: Setting up the model. Error: W has some areas with no neighbours (one of the row sums equals zero). Que puis-je faire?

rsbivand commented 1 year ago

In general, no CAR model should be fit when there are subgraphs in the graph of neighbours. Singleton observations (no-neighbour observations) are also subgraphs with a single member. To check a neighbour object, try:

any(card(nb) == 0)
n.comp.nb(nb)$nc > 1

If either of these holds, subgraphs are present (see also https://doi.org/10.1016/j.sste.2018.04.002). You would then need to revisit the construction of the neighbour object, either changing how it is constructed, or possibly omitting observations in subgraphs. See also https://r-spatial.org/book/14-Areal.html for more on neighbour objects. SAR models and tests based on SAR assumptions are perhaps more forgiving of subgraphs, but are not properly studied. Tests in spdep try to adjust n to account for the absence of information from no-neighbour observations.

gadouaka commented 1 year ago

Bonsoir, apres avoir suivi les étapes suivantes pour la matrice du poids: library(sp) library(sf) Carte <- st_read("C:/Users/USER/Documents/Dossier R/shapefile/carte.shp") Carte |> st_geometry() |> st_centroid(of_largest_polygon = TRUE) -> coordo coordo |> dnearneigh(0, 6.5) -> nb nb Neighbour list object: Number of regions: 35 Number of nonzero links: 1050 Percentage nonzero weights: 85.71429 Average number of links: 30 j ai utilise la méthode de voisinage par distance pour corriger le probleme des voisins sans lien(OK, pas de probleme) Ensuite, try(nb |> nb2listw(style="B") -> list.nb1) W1 <- nb2mat(nb, style = "B" model3 <- CARBayesST::ST.CARar(formula = formula, family = "poisson", data = DISTRICT, W=W1, AR=1, burnin = 20000, n.sample = 220000, thin = 10) En executant le modele ci-haut, le message d'erreur suivant s'affiche: Warning message: In mat2listw(W) : style is M (missing); style should be set to a valid value Et pourtant, j ai bel et bien precisé le type"B". Dans ce cas, comment je peux faire pour resooudre definitivement ce probleme du type de ponderation que je me confronte?

gadouaka commented 1 year ago

Bonsoir, apres avoir suivi les étapes suivantes pour la matrice du poids: library(sp) library(sf) Carte <- st_read(« C:/Users/USER/Documents/Dossier R/shapefile/carte.shp ») Carte |> st_geometry() |> st_centroid(of_largest_polygon = TRUE) -> coordo coordo |> dnearneigh(0, 6.5)

-> nb nb

Objet de liste de voisins : Nombre de régions :

35 Nombre de liens non nuls: 1050 Pourcentage poids non nul: 85.71429 Nombre moyen de liens: 30

j ai utilise la méthode de voisinage par distance pour corriger le probleme des voisins sans lien(OK, pas de probleme) Ensuite, try(nb |> nb2listw(style="B ») -> list.nb1)

W1 <- nb2mat(nb, style = « B »

modèle3 <- CARBayesST::ST. CARar(formula = formula, family = « poisson », data = DISTRICT, W=W1, AR=1, burnin = 20000, n.sample = 220000, thin = 10) En executant le modele ci-haut, le message d’erreur suivant s’affiche: Warning message: In mat2listw(W) :

style is M (missing); style doit être défini sur une valeur valide Et pourtant, j ai bel et bien precisé le type"B ». Dans ce cas, comment je peux faire pour resooudre definitivement ce probleme du type de ponderation que je me confronte?

rsbivand commented 1 year ago

This is related to https://github.com/r-spatial/spdep/commit/fdc789d2ea6859f2bdb331ace6db20a7a3897186, in CRAN versions from current 1.2-8. If mat2listw() does not declare a style= argument, it issues a warning. https://github.com/duncanplee/CARBayesST/blob/897a3124598b08b7a2e7836029742d414b9c322b/R/poisson.CARar1.R#L161 (and similarly in the other variants) use mat2listw() to cary out the tests I mentioned above in https://github.com/r-spatial/spdep/issues/129#issuecomment-1612601045.

Since they only need the neighbours component of the matrix converted to a spatial neighbour list object, they could modify their code (also in CARBayes) to add an arbitrary style such a "B".

The warning as such does not affect your results, it is a warning, not an error.

An "M" style is entered as a missing style, which complicates the subsetting of listw objects, hence the warning. This change was made in response to several issues in spatialreg.

I've raised an issue in CARBayesST: https://github.com/duncanplee/CARBayesST/issues/9.

Please also note that CARBayesST would have worked with your original neighour object containing islands as the code in for example https://github.com/duncanplee/CARBayesST/blob/897a3124598b08b7a2e7836029742d414b9c322b/R/poisson.CARar1.R#L161-L176 shows - it adjusts tau.shape if rho=1 and n.islands > 0; your problem was trying to view the spatial weights object list.nb. You could view your neighbour object by saying for example print(list.nb, zero.policy = TRUE). I would argue that dnearneigh() giving on average 30 neighbours for 35 observations is far too dense. Please do look at the alternatives described in https://r-spatial.org/book/14-Areal.html.

gadouaka commented 11 months ago

Bonjour, j'ai soucis avec les parametres de mon modele suivant: modele1 <- CARBayesST::ST.CARlinear(Rubela~Tmax+Precipit + Q_poor + Densite, family = "poisson", data = DISTRICT, W=W,burnin = 1000, n.sample = 21000, thin = 10) A chque fois que j'execute ce modele, les valeurs de parametres changent lors d'une execution à l autre. comment pourrais je faire pour stabiliser les valeurs des parametres? Merci d'avance pour votre orientation et soutien

gadouaka commented 11 months ago

voici l'affichage de mes resultats modele1 <- CARBayesST::ST.CARlinear(Rubela~Tmax+Precipit + Q_poor + Densite, family = "poisson", data = DISTRICT, W=W,burnin = 1000, n.sample = 21000, thin = 10) Setting up the model. Generating 2000 post burnin and thinned (if requested) samples. |============================================================================================| 100% Summarising results. Finished in 12 seconds. Warning message: In mat2listw(W) : style is M (missing); style should be set to a valid value

modele1 ##

#################

Model fitted

################# Likelihood model - Poisson (log link function) Latent structure model - Spatially autocorrelated linear time trends Regression equation - Rubela ~ Tmax + Precipit + Q_poor + Densite

############

Results

############ Posterior quantities for selected parameters and DIC

           Mean    2.5%   97.5% n.effective Geweke.diag

(Intercept) 2.9909 -9.1822 15.9671 40.9 -2.4 Tmax 0.0222 -0.3818 0.3892 38.5 2.4 Precipit -0.0187 -0.0348 -0.0021 133.5 2.3 Q_poor -0.0535 -0.1025 -0.0111 19.0 0.3 Densite 0.0024 -0.0330 0.0432 29.4 -0.1 alpha 2.4047 1.6383 3.2108 205.5 -2.3 tau2.int 5.0801 1.5105 14.7044 196.7 -4.7 tau2.slo 23.1092 7.2815 71.3182 346.8 0.2 rho.int 0.1979 0.0058 0.6887 457.1 -2.6 rho.slo 0.1294 0.0025 0.6027 324.9 0.4

DIC = 848.0238 p.d = 52.15453 LMPL = -583.1736