gpt4 book ai didi

python - Pysal:如何使用 pysal 回归模型进行交叉验证?

转载 作者:行者123 更新时间:2023-12-02 06:34:42 24 4
gpt4 key购买 nike

我正在使用 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 =====================================

最佳答案

我也遇到了同样的问题,并尝试使用以下方法解决。我不完全确定这是否是正确/正确的方法。我也很想知道是否有更好的方法来解决这个问题。

    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 后,您可以使用其他方法测试其性能。

关于python - Pysal:如何使用 pysal 回归模型进行交叉验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56649634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com