我正在使用pysal
进行空间回归。这就是我在做什么。我首先将训练和验证集分开
import pysal as ps
m_train = ps.model.spreg.GM_Lag(ytrain, xtrain, w=w_train, spat_diag=True)
如何使用m_train
的系数在验证集上测试模型?
print(m_train.summary)
REGRESSION
----------
SUMMARY OF OUTPUT: SPATIAL TWO STAGE LEAST SQUARES
--------------------------------------------------
Data set : unknown
Weights matrix : unknown
Dependent Variable : dep_var Number of Observations: 138
Mean dependent var : 0.2200 Number of Variables : 12
S.D. dependent var : 2.2411 Degrees of Freedom : 126
Pseudo R-squared : 0.6609
Spatial Pseudo R-squared: omitted due to rho outside the boundary (-1, 1).
------------------------------------------------------------------------------------
Variable Coefficient Std.Error z-Statistic Probability
------------------------------------------------------------------------------------
CONSTANT -0.1456487 0.1166442 -1.2486579 0.2117902
var_1 0.0326638 0.1420966 0.2298707 0.8181922
var_2 -0.0252494 0.1520906 -0.1660155 0.8681447
var_3 -0.0899973 0.1598643 -0.5629610 0.5734615
var_4 0.0720296 0.1980007 0.3637845 0.7160190
var_5 -0.1850660 0.1932144 -0.9578273 0.3381499
var_6 0.3282914 0.1482416 2.2145705 0.0267896
var_7 0.0769414 0.1665854 0.4618738 0.6441718
var_8 0.0285464 0.1570794 0.1817320 0.8557930
var_9 0.0427946 0.1613301 0.2652607 0.7908086
var_10 -0.1375104 0.1636461 -0.8402910 0.4007452
W_dep_var 1.0880815 0.0960350 11.3300537 0.0000000
------------------------------------------------------------------------------------
Instrumented: W_dep_var
Instruments: W_var_1, W_var_10, W_var_2, W_var_3, W_var_4, W_var_5, W_var_6,
W_var_7, W_var_8, W_var_9
Warning: *** WARNING: Estimate for spatial lag coefficient is outside the boundary (-1, 1). ***
DIAGNOSTICS FOR SPATIAL DEPENDENCE
TEST MI/DF VALUE PROB
Anselin-Kelejian Test 1 9.856 0.0017
================================ END OF REPORT =====================================
答案 0 :(得分:0)
我也遇到了相同的问题,并尝试使用以下方法解决。我不完全确定这是否正确/正确。我也非常想知道是否有更好的方法来解决这个问题。
x = xtest.values
betas = m_train.betas # beta coefficients of the spatial lag model
betasX = betas[:-1]
betaRho = betas[-1]
wtArr, ids = wt.full() # convert pysal weight matrix into an array
In = np.zeros((wtArr.shape[0], wtArr.shape[0]), float)
np.fill_diagonal(In, 1)
rho = betasRho[0]
inter = np.subtract(In, rho*wtArr)
BRho = np.linalg.inv(inter)
xNew = np.c_[np.ones(wtArr.shape[0]), x]
xNew = np.asmatrix(xNew)
predY = BRho.dot(xNew).dot(betasX)
获得预测的y后,可以使用其他方法测试其性能。