pysal / mgwr

Multiscale Geographically Weighted Regression (MGWR)
https://mgwr.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
368 stars 126 forks source link

allow custom variable names in summary #130

Closed martinfleis closed 1 year ago

martinfleis commented 1 year ago

spreg allows passing name_x with a list of names of independent variables. mgwr does not, resulting in X0...Xn in the summary. With a larger number of variables, it gets tricky to link the two, especially when there's an intercept included (but not marked).

I am mirroring the behaviour of spreg here and adding optional name_x, that gets used in summary.

As far as I can tell, there are no tests for summary so nowhere to add tests for this.

Examples:

Current behaviour:

import libpysal
import numpy as np

from mgwr.gwr import GWR

data = libpysal.io.open(libpysal.examples.get_path('GData_utm.csv'))
coords = list(zip(data.by_col('X'), data.by_col('Y')))
y = np.array(data.by_col('PctBach')).reshape((-1,1))
rural = np.array(data.by_col('PctRural')).reshape((-1,1))
pov = np.array(data.by_col('PctPov')).reshape((-1,1))
african_amer = np.array(data.by_col('PctBlack')).reshape((-1,1))
X = np.hstack([rural, pov, african_amer])
model = GWR(coords, y, X, bw=90.000, fixed=False, kernel='bisquare')
results = model.fit()
results.summary()

===========================================================================
Model type                                                         Gaussian
Number of observations:                                                 159
Number of covariates:                                                     4

Global Regression Results
---------------------------------------------------------------------------
Residual sum of squares:                                           2639.559
Log-likelihood:                                                    -448.964
AIC:                                                                905.927
AICc:                                                               908.319
BIC:                                                               1853.879
R2:                                                                   0.485
Adj. R2:                                                              0.475

Variable                              Est.         SE  t(Est/SE)    p-value
------------------------------- ---------- ---------- ---------- ----------
X0                                  23.855      1.173     20.336      0.000
X1                                  -0.111      0.013     -8.650      0.000
X2                                  -0.346      0.071     -4.880      0.000
X3                                   0.058      0.029      1.998      0.046

Geographically Weighted Regression (GWR) Results
---------------------------------------------------------------------------
Spatial kernel:                                           Adaptive bisquare
Bandwidth used:                                                      90.000

Diagnostic information
---------------------------------------------------------------------------
Residual sum of squares:                                           2090.125
Effective number of parameters (trace(S)):                           14.925
Degree of freedom (n - trace(S)):                                   144.075
Sigma estimate:                                                       3.809
Log-likelihood:                                                    -430.409
AIC:                                                                892.669
AICc:                                                               896.463
BIC:                                                                941.541
R2:                                                                   0.592
Adjusted R2:                                                          0.550
Adj. alpha (95%):                                                     0.013
Adj. critical t value (95%):                                          2.501

Summary Statistics For GWR Parameter Estimates
---------------------------------------------------------------------------
Variable                   Mean        STD        Min     Median        Max
-------------------- ---------- ---------- ---------- ---------- ----------
X0                       23.068      4.185     16.844     22.653     29.626
X1                       -0.118      0.038     -0.190     -0.104     -0.071
X2                       -0.262      0.097     -0.535     -0.249     -0.065
X3                        0.045      0.059     -0.073      0.058      0.134
===========================================================================

New:

model = GWR(coords, y, X, bw=90.000, fixed=False, kernel='bisquare', name_x=["rural", "pov", "african_amer"])
results = model.fit()
results.summary()

===========================================================================
Model type                                                         Gaussian
Number of observations:                                                 159
Number of covariates:                                                     4

Global Regression Results
---------------------------------------------------------------------------
Residual sum of squares:                                           2639.559
Log-likelihood:                                                    -448.964
AIC:                                                                905.927
AICc:                                                               908.319
BIC:                                                               1853.879
R2:                                                                   0.485
Adj. R2:                                                              0.475

Variable                              Est.         SE  t(Est/SE)    p-value
------------------------------- ---------- ---------- ---------- ----------
Intercept                           23.855      1.173     20.336      0.000
rural                               -0.111      0.013     -8.650      0.000
pov                                 -0.346      0.071     -4.880      0.000
african_amer                         0.058      0.029      1.998      0.046

Geographically Weighted Regression (GWR) Results
---------------------------------------------------------------------------
Spatial kernel:                                           Adaptive bisquare
Bandwidth used:                                                      90.000

Diagnostic information
---------------------------------------------------------------------------
Residual sum of squares:                                           2090.125
Effective number of parameters (trace(S)):                           14.925
Degree of freedom (n - trace(S)):                                   144.075
Sigma estimate:                                                       3.809
Log-likelihood:                                                    -430.409
AIC:                                                                892.669
AICc:                                                               896.463
BIC:                                                                941.541
R2:                                                                   0.592
Adjusted R2:                                                          0.550
Adj. alpha (95%):                                                     0.013
Adj. critical t value (95%):                                          2.501

Summary Statistics For GWR Parameter Estimates
---------------------------------------------------------------------------
Variable                   Mean        STD        Min     Median        Max
-------------------- ---------- ---------- ---------- ---------- ----------
Intercept                23.068      4.185     16.844     22.653     29.626
rural                    -0.118      0.038     -0.190     -0.104     -0.071
pov                      -0.262      0.097     -0.535     -0.249     -0.065
african_amer              0.045      0.059     -0.073      0.058      0.134
===========================================================================
martinfleis commented 1 year ago

I would also eventually love to see mgwr accepting pandas objects and deriving the names from those (xref https://github.com/pysal/spreg/issues/127) but let's go step by step.

weikang9009 commented 1 year ago

I think this is great to have in MGWR, so I am going to merge this PR.

martinfleis commented 1 year ago

@weikang9009 thanks! Can we cut a release with this, pretty please? I'd like to include it in my teaching materials 😇.

martinfleis commented 1 year ago

Mmm, I suppose we should wait for #131 and new release infra. Need to nag @sjsrey to give @jGaboardi permissions :)

sjsrey commented 1 year ago

pysal/steering now should have admin rights on this repo.

jGaboardi commented 1 year ago

@sjsrey Thanks! Confirmed for myself.

martinfleis commented 11 months ago

@TaylorOshan, @Ziqi-Li any chance to cut a release with this? I'd like to include it in a class I'm teaching next Tuesday. If not that is fine as well but this is one of the tiny things that can make a difference when explaining what is going on.

Ziqi-Li commented 11 months ago

@TaylorOshan, @Ziqi-Li any chance to cut a release with this? I'd like to include it in a class I'm teaching next Tuesday. If not that is fine as well but this is one of the tiny things that can make a difference when explaining what is going on.

Sounds good to me.

@TaylorOshan do you want to do it yourself, or you want me to do it?

TaylorOshan commented 11 months ago

I always thought we had this already but maybe that’s in the GUI?

Feel free to go for it! @Ziqi-Li @martinfleis @jGaboardi

Ziqi-Li commented 11 months ago

I always thought we had this already but maybe that’s in the GUI?

Feel free to go for it! @Ziqi-Li @martinfleis @jGaboardi

Yeah, I also have that impression somehow... the GUI has it for sure.

martinfleis commented 11 months ago

Well, it wasn't here :). Where does the GUI live anyway?

@Ziqi-Li can help with the release somehow?

jGaboardi commented 11 months ago

Also, seems like we never got around to setting up a release action for mgwr.

Ziqi-Li commented 11 months ago

Well, it wasn't here :). Where does the GUI live anyway?

@Ziqi-Li can help with the release somehow?

The GUI code is here: https://github.com/Ziqi-Li/MGWR-GUI

Sure, I will work on the release today.

martinfleis commented 11 months ago

@Ziqi-Li Thanks!

@Ziqi-Li can help with the release somehow?

I meant to write "Can I help..." :D

martinfleis commented 11 months ago

This is the code implementing the same in the GUI

https://github.com/Ziqi-Li/MGWR-GUI/blob/ca8460ec178f233f95d386f521327f6b16c9148c/src/gui.py#L792-L801

Ziqi-Li commented 11 months ago

@Ziqi-Li Thanks!

@Ziqi-Li can help with the release somehow?

I meant to write "Can I help..." :D

:D. I just released the v2.2.0 on pypi @martinfleis @TaylorOshan