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

allow custom variable names in summary #130

Merged
merged 1 commit into from
Nov 2, 2023

Conversation

martinfleis
Copy link
Member

@martinfleis martinfleis commented Oct 13, 2023

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
Copy link
Member Author

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

@weikang9009
Copy link
Member

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

@weikang9009 weikang9009 merged commit 85e30c6 into pysal:master Nov 2, 2023
6 checks passed
@martinfleis martinfleis deleted the summary_names branch November 2, 2023 13:49
@martinfleis
Copy link
Member Author

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

@martinfleis
Copy link
Member Author

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

@sjsrey
Copy link
Member

sjsrey commented Nov 2, 2023

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

@jGaboardi
Copy link
Member

@sjsrey Thanks! Confirmed for myself.

@martinfleis
Copy link
Member Author

@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
Copy link
Member

Ziqi-Li commented Nov 28, 2023

@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
Copy link
Collaborator

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
Copy link
Member

Ziqi-Li commented Nov 28, 2023

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
Copy link
Member Author

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

@Ziqi-Li can help with the release somehow?

@jGaboardi
Copy link
Member

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

@Ziqi-Li
Copy link
Member

Ziqi-Li commented Nov 28, 2023

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
Copy link
Member Author

@Ziqi-Li Thanks!

@Ziqi-Li can help with the release somehow?

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

@martinfleis
Copy link
Member Author

@Ziqi-Li
Copy link
Member

Ziqi-Li commented Nov 28, 2023

@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

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

Successfully merging this pull request may close these issues.

6 participants