python - Extract coefficients from spreg.OLS results -
i'm trying recreate r spgwr notebook using pysal. using r, local coefficients can directly extracted dataframe so: (csv available here)
r
library(spgwr) df <- read.csv("file.csv") attach(df) # calibrate bandwidth bw <- gwr.sel(endog ~ x1+x2+x3, data=df, coords=cbind(x,y), adapt=t) # fit model gwr.model = gwr(endog ~ x1+x2+x3, data=df, coords=cbind(x,y), adapt=bw, hatmatrix=true, se.fit=true) # build results dataframe results<-as.data.frame(gwr.model$sdf) # results contains coefficients , standard errors: head(results) # columns: sum.w x.intercept, x1, x2, x3 … pred.se_edf x y # attach coefficients original dataframe df$coef_x1<-results$x1 df$coef_x2<-results$x2 df$coef_x3<-results$x3 is there simple way can calculate these coefficients using results ps.spreg.ols?
python
import pandas pd import pysal ps df = pd.read_csv("file.csv") # build spatial weights # give different weights r, that's ok # repackage variables convenience yxs = df.loc[:, ['endog', 'x1', 'x2', 'x3']] spatial_weights = ps.knnw_from_array( df.loc[yxs.index, ['x', 'y']].values ) # row-standarise weights spatial_weights.transform = 'r' fit = ps.spreg.ols( df.endog.values[:, none], df[['x1', 'x2', 'x3']].values, w=spatial_weights, spat_diag=true, ) print(fit.summary) regression ---------- summary of output: ordinary least squares ----------------------------------------- data set : unknown weights matrix : unknown dependent variable : dep_var number of observations: 625 mean dependent var : 345.8406 number of variables : 4 s.d. dependent var : 19.5388 degrees of freedom : 621 r-squared : 0.5163 adjusted r-squared : 0.5139 sum squared residual: 115234.766 f-statistic : 220.9246 sigma-square : 185.563 prob(f-statistic) : 1.68e-97 s.e. of regression : 13.622 log likelihood : -2517.141 sigma-square ml : 184.376 akaike info criterion : 5042.283 s.e of regression ml: 13.5785 schwarz criterion : 5060.034 ------------------------------------------------------------------------------------ variable coefficient std.error t-statistic probability ------------------------------------------------------------------------------------ constant 361.8912240 3.0709296 117.8441935 0.0000000 var_1 -13.0082465 1.8701682 -6.9556560 0.0000000 var_2 -1.1944903 0.1067895 -11.1854632 0.0000000 var_3 23.3549680 2.1474242 10.8758055 0.0000000 ------------------------------------------------------------------------------------ regression diagnostics multicollinearity condition number 13.948 test on normality of errors test df value prob jarque-bera 2 2160.476 0.0000 diagnostics heteroskedasticity random coefficients test df value prob breusch-pagan test 3 75.484 0.0000 koenker-bassett test 3 13.942 0.0030 diagnostics spatial dependence test mi/df value prob lagrange multiplier (lag) 1 43.543 0.0000 robust lm (lag) 1 1.819 0.1775 lagrange multiplier (error) 1 46.989 0.0000 robust lm (error) 1 5.264 0.0218 lagrange multiplier (sarma) 2 48.808 0.0000 ================================ end of report ===================================== i'm not sure how use data contained in fit calculate local coefficients, however.
Comments
Post a Comment