pysal / spreg

Spatial econometric regression in Python
https://pysal.org/spreg/
Other
67 stars 23 forks source link

random effect spatial error model encounters "LinAlgError: Singular matrix" error #87

Open shuai-zhou opened 2 years ago

shuai-zhou commented 2 years ago

A similar issue can be found here, but I did not understand what @gboeing meant by adding noise to dummy variables. Anyway, I got the "Singular matrix" error when implementing random effect spatial error model using spreg.Panel_RE_Error. The test data can be downloaded here, it is a longitudinal data in long format. And my code looks like the following:

# Load libraries
import numpy as np
import pandas as pd
import geopandas as gpd
import libpysal
from pysal.lib import weights
from pysal.model import spreg

# Read data
zipfile = './data/model_test.zip'
gdf = gpd.read_file(zipfile)

# Construct weight
w = weights.Queen.from_dataframe(gdf.iloc[0:254, :])
w.transform = 'r'

# Prepare variables
y = gdf[['y']]
x = gdf[['colle', 'labor', 'year_1980', 'year_1990']]

# Model fitting. This step throws singular matrix error
re_error = spreg.Panel_RE_Error(y.to_numpy(),
                                x.to_numpy(),
                                w,
                                name_y=list(y.columns),
                                name_x=list(x.columns))
print(re_error.summary)

What caused the singular matrix issue in this context and how can I fix it? Thanks.

ljwolf commented 2 years ago

This is usually caused by perfect collinearity in your x matrix.... I'll try to have a look this week.

shuai-zhou commented 2 years ago

@ljwolf Thanks, I am looking forward to it.