or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

diagnostics.mdindex.mdml-models.mdols-models.mdpanel-models.mdprobit-models.mdregime-models.mdspatial-error-models.mdsur-models.mdtsls-models.mdutilities.md

diagnostics.mddocs/

0

# Diagnostic Functions

1

2

Comprehensive diagnostic testing for spatial autocorrelation, heteroskedasticity, normality, multicollinearity, and model specification in spatial regression contexts.

3

4

## Capabilities

5

6

### Spatial Diagnostic Tests

7

8

```python { .api }

9

class LMtests:

10

def __init__(self, ols, w, tests=["all"]):

11

"""

12

Lagrange Multiplier tests for spatial dependence.

13

14

Parameters:

15

- ols: OLS regression object

16

- w: Spatial weights object

17

- tests: list of strings specifying which tests to run

18

* 'all': runs all tests (default)

19

* 'lme': LM error test

20

* 'rlme': Robust LM error test

21

* 'lml': LM lag test

22

* 'rlml': Robust LM lag test

23

* 'sarma': LM SARMA test

24

* 'lmwx': LM test for WX

25

* 'rlmwx': Robust LM WX test

26

* 'lmspdurbin': Joint test for SDM

27

* 'rlmdurlag': Robust LM Lag - SDM

28

29

Attributes:

30

- lme: tuple (statistic, p-value) for LM error test

31

- lml: tuple (statistic, p-value) for LM lag test

32

- rlme: tuple (statistic, p-value) for Robust LM error test

33

- rlml: tuple (statistic, p-value) for Robust LM lag test

34

- sarma: tuple (statistic, p-value) for SARMA test

35

- lmwx: tuple (statistic, p-value) for LM WX test

36

- rlmwx: tuple (statistic, p-value) for Robust LM WX test

37

- lmspdurbin: tuple (statistic, p-value) for Joint SDM test

38

- rlmdurlag: tuple (statistic, p-value) for Robust LM Lag - SDM test

39

"""

40

41

class MoranRes:

42

def __init__(self, ols, w, z=False):

43

"""

44

Moran's I test for spatial autocorrelation in residuals.

45

46

Parameters:

47

- ols: OLS regression object

48

- w: Spatial weights object

49

- z: boolean, if True computes eI, vI and zI attributes

50

51

Attributes:

52

- I: float, Moran's I statistic

53

- eI: float, Moran's I expectation (if z=True)

54

- vI: float, Moran's I variance (if z=True)

55

- zI: float, Moran's I standardized value (if z=True)

56

"""

57

58

class AKtest:

59

def __init__(self, iv, w, case='nosp'):

60

"""

61

Anselin-Kelejian test for spatial dependence in IV residuals.

62

63

Parameters:

64

- iv: TSLS regression object

65

- w: Spatial weights object

66

- case: string, 'nosp' for no spatial endogenous regressors, 'gen' for general case

67

68

Attributes:

69

- mi: float, Moran's I statistic for IV residuals

70

- ak: float, Square of corrected Moran's I

71

- p: float, P-value of the test

72

"""

73

```

74

75

### General Diagnostic Tests

76

77

```python { .api }

78

def jarque_bera(reg):

79

"""Jarque-Bera test for normality of residuals."""

80

81

def breusch_pagan(reg):

82

"""Breusch-Pagan test for heteroskedasticity."""

83

84

def white(reg):

85

"""White test for heteroskedasticity."""

86

87

def koenker_bassett(reg):

88

"""Koenker-Bassett test for heteroskedasticity."""

89

90

def vif(reg):

91

"""Variance Inflation Factor for multicollinearity detection."""

92

93

def f_stat(reg, z_stat=False):

94

"""F-statistic or Wald statistic for overall model significance."""

95

96

def t_stat(reg, z_stat=False):

97

"""t-statistics or z-statistics for individual coefficients."""

98

```

99

100

## Usage Examples

101

102

### Spatial Diagnostic Testing

103

104

```python

105

import numpy as np

106

import spreg

107

from libpysal import weights

108

109

# OLS model for testing

110

n = 100

111

x = np.random.randn(n, 2)

112

y = np.random.randn(n, 1)

113

w = weights.KNN.from_array(np.random.randn(n, 2), k=5)

114

115

ols_model = spreg.OLS(y, x, nonspat_diag=False, spat_diag=False)

116

117

# Manual spatial diagnostic testing

118

lm_tests = spreg.LMtests(ols_model, w)

119

120

print("LM Error test:", lm_tests.lme)

121

print("LM Lag test:", lm_tests.lml)

122

print("Robust LM Error:", lm_tests.rlme)

123

print("Robust LM Lag:", lm_tests.rlml)

124

print("SARMA test:", lm_tests.sarma)

125

126

# Decision rule for spatial dependence

127

if lm_tests.lme[1] < 0.05 and lm_tests.lml[1] < 0.05:

128

if lm_tests.rlme[1] < lm_tests.rlml[1]:

129

print("Spatial error model recommended")

130

else:

131

print("Spatial lag model recommended")

132

133

# Moran's I test on residuals

134

moran_test = spreg.MoranRes(ols_model, w, z=True)

135

print(f"Moran's I: {moran_test.I}, Z-score: {moran_test.zI}")

136

137

# For TSLS models, use AKtest

138

tsls_model = spreg.TSLS(y, x[:,:1], x[:,1:2], x[:,1:2]) # Simple IV example

139

ak_test = spreg.AKtest(tsls_model, w)

140

print(f"AK test statistic: {ak_test.ak}, p-value: {ak_test.p}")

141

```