Skip to content

Commit

Permalink
remove splot completely
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfleis committed Jul 31, 2024
1 parent 450c45d commit 5c0d857
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
14 changes: 6 additions & 8 deletions autocorrelation/exercise_answers.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Measure Join Counts statistic on your new variable.
```py
jc = esda.Join_Counts(
glasgow["better_half"],
contiguity.to_W(),
contiguity,
)
```

Expand Down Expand Up @@ -110,7 +110,7 @@ ax.axhline(0, c="black", alpha=0.5)
Calculate Moran's $I$

```py
mi = esda.Moran(glasgow['rank_std'], contiguity_r.to_W())
mi = esda.Moran(glasgow['rank_std'], contiguity_r)
```

```py
Expand All @@ -124,27 +124,25 @@ mi.p_sim
Calculate LISA statistics for the areas

```py
lisa = esda.Moran_Local(glasgow['rank_std'], contiguity_r.to_W())
lisa = esda.Moran_Local(glasgow['rank_std'], contiguity_r)
```

Make a map of significant clusters at the 5%

```py
from splot.esda import lisa_cluster

_ = lisa_cluster(lisa, glasgow)
lisa.explore(glasgow)
```

Create cluster maps for significance levels 1%

```py
_ = lisa_cluster(lisa, glasgow, p=.01)
lisa.explore(glasgow, crit_value=.01)
```

Create cluster maps for significance levels 10%;

```py
_ = lisa_cluster(lisa, glasgow, p=.1)
lisa.explore(glasgow, crit_value=.1)
```

Can you create both interactive and static versions of those maps?
Expand Down
9 changes: 7 additions & 2 deletions autocorrelation/hands_on.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,15 @@ global Moran's $I$, you select 5% as the threshold for statistical significance.
You can quickly map these results using the `explore()` method.

```{python}
lisa.explore(elections, crit_value=0.05) # <1>
lisa.explore(
elections, # <1>
crit_value=0.05, # <2>
prefer_canvas=True,
tiles="CartoDB Positron",
)
```
1. `lisa` does not save geometry so you need to pass the original GeoDataFrame as a source of geometric information.

2. You can specify a custom threshold of significance.

::: {.callout-tip collapse="true"}
# Plotting LISA manually
Expand Down
1 change: 0 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ dependencies:
- rasterio >=1.3
- scikit-learn
- seaborn >=0.13
- splot >=1.1
- spglm >=1.1
- spreg >=1.4
- statsmodels >=0.14
Expand Down
12 changes: 5 additions & 7 deletions regression/hands_on.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import pandas as pd
import seaborn as sns
import statsmodels.formula.api as sm
from libpysal import graph
from splot.esda import lisa_cluster
```

## Data
Expand Down Expand Up @@ -222,18 +221,17 @@ plt.axhline(0, c="black", alpha=0.5);
That looks like a pretty strong relationship. Use the local version of Moran's statistic to find out the clusters.

```{python}
lisa = esda.Moran_Local(elections_data['residual'], contiguity_r.to_W()) # <1>
lisa = esda.Moran_Local(elections_data['residual'], contiguity_r) # <1>
```
1. Use `Moran_Local` function from `esda` and remember that it needs `W`, not `Graph`.
1. Use `Moran_Local` function from `esda`.

Let's use our handy `lisa_cluster` function from `splot` to visualise the results.
Let's visualise the results.

```{python}
#| fig-cap: LISA clusters
_ = lisa_cluster(lisa, elections_data)
lisa.explore(elections_data, prefer_canvas=True, tiles="CartoDB Positron")
```

The outcome of LISA shows large clusters of both overpredicted (high-high) and underpredicted (low-low) areas. The underpredicted are mostly in central Bohemia around Prague and in the mountains near the borders, where the ski resorts are. Putting aside the central areas for a bit, the explanation of underprediction in mountains is relatively straightforward. The education data are linked to the residents of each municipality. The people who voted in a municipality do not necessarily need to match with residents. It is known that more affluent population groups, who are more likely to go to a ski resort, voted overwhelmingly for Pavel. And since the elections were in winter, a lot of them likely voted in ski resorts, affecting the outcome of the model.
The outcome of LISA shows large clusters of both overpredicted (High-High) and underpredicted (Low-Low) areas. The underpredicted are mostly in central Bohemia around Prague and in the mountains near the borders, where the ski resorts are. Putting aside the central areas for a bit, the explanation of underprediction in mountains is relatively straightforward. The education data are linked to the residents of each municipality. The people who voted in a municipality do not necessarily need to match with residents. It is known that more affluent population groups, who are more likely to go to a ski resort, voted overwhelmingly for Pavel. And since the elections were in winter, a lot of them likely voted in ski resorts, affecting the outcome of the model.

The overpredicted areas, on the other hand, are known for higher levels of deprivation, which may have played a role in the results. What is clear, is that geography plays a huge role in the modelling of the elections.

Expand Down

0 comments on commit 5c0d857

Please sign in to comment.