OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

IndexError: invalid index to scalar variable in a spatial regression model

  • Thread starter Thread starter Freya
  • Start date Start date
F

Freya

Guest
I am doing a spatial lag model in order to find which variables are related to the dependent variable.

Code:
import libpysal as lps
# read the .dbf file from shapefile data
f = lps.io.open('data/lambeth_data2.dbf','r')
# Read into an array y
y = np.array(f.by_col['Mental hea'])
y.shape = (len(y),1)
# value for independent variables into a one dimensional array X. 

X= []
X.append(f.by_col['Greenspace']) 
X.append(f.by_col['Imd score']) 
X.append(f.by_col['Population']) 
X.append(f.by_col['NUMPOINTS'])

X = np.array(X).T

w_queen2=Queen.from_shapefile('data/lambeth_data2.shp')

from spreg import ML_Lag
spat_lag2 = ML_Lag(y,X,w_queen2,name_y='Mental hea', 
                  name_x=['Greenspace','Imd score', 'Population','NUMPOINTS'],
                  name_w='w_queen', name_ds='lambeth_data')
print(spat_lag2.summary)

There are four independent variables in X.

But there is an error after running:

Code:
IndexError                                Traceback (most recent call last)
Input In [145], in <module>
      1 from spreg import ML_Lag
----> 2 spat_lag2 = ML_Lag(y,X,w_queen2,name_y='Mental hea', 
      3                   name_x=['Greenspace','Imd score', 'Population','NUMPOINTS'],
      4                   name_w='w_queen', name_ds='lambeth_data')
      5 print(spat_lag2.summary)

File ~/opt/anaconda3/envs/msda2021/lib/python3.8/site-packages/spreg/ml_lag.py:557, in ML_Lag.__init__(self, y, x, w, method, epsilon, vm, name_y, name_x, name_w, name_ds)
    555 set_warn(self, warn)
    556 method = method.upper()
--> 557 BaseML_Lag.__init__(
    558     self, y=y, x=x_constant, w=w, method=method, epsilon=epsilon)
    559 # increase by 1 to have correct aic and sc, include rho in count
    560 self.k += 1

File ~/opt/anaconda3/envs/msda2021/lib/python3.8/site-packages/spreg/ml_lag.py:235, in BaseML_Lag.__init__(self, y, x, w, method, epsilon)
    232     self = None
    233     return
--> 235 self.rho = res.x[0][0]
    237 # compute full log-likelihood, including constants
    238 ln2pi = np.log(2.0 * np.pi)

IndexError: invalid index to scalar variable.

PS. Before X and y are passed to ML_Lag,

X:

Code:
array([[  0.51,  23.57, 168.22,   2.  ],
       [  5.05,  32.47, 142.03,   0.  ],
       [  1.19,  22.23, 152.32,   2.  ],
       ...
       ...
       [  1.21,  19.84,  93.39,   4.  ]])

y:

Code:
array([[17.79],
       [24.97],
      ...
      ...
       [15.96]])

I don't know how to fix it, please help me. Thank you!
<p>I am doing a spatial lag model in order to find which variables are related to the dependent variable.</p>
<pre><code>import libpysal as lps
# read the .dbf file from shapefile data
f = lps.io.open('data/lambeth_data2.dbf','r')
# Read into an array y
y = np.array(f.by_col['Mental hea'])
y.shape = (len(y),1)
# value for independent variables into a one dimensional array X.

X= []
X.append(f.by_col['Greenspace'])
X.append(f.by_col['Imd score'])
X.append(f.by_col['Population'])
X.append(f.by_col['NUMPOINTS'])

X = np.array(X).T

w_queen2=Queen.from_shapefile('data/lambeth_data2.shp')

from spreg import ML_Lag
spat_lag2 = ML_Lag(y,X,w_queen2,name_y='Mental hea',
name_x=['Greenspace','Imd score', 'Population','NUMPOINTS'],
name_w='w_queen', name_ds='lambeth_data')
print(spat_lag2.summary)
</code></pre>
<p>There are four independent variables in X.</p>
<p>But there is an error after running:</p>
<pre><code>IndexError Traceback (most recent call last)
Input In [145], in <module>
1 from spreg import ML_Lag
----> 2 spat_lag2 = ML_Lag(y,X,w_queen2,name_y='Mental hea',
3 name_x=['Greenspace','Imd score', 'Population','NUMPOINTS'],
4 name_w='w_queen', name_ds='lambeth_data')
5 print(spat_lag2.summary)

File ~/opt/anaconda3/envs/msda2021/lib/python3.8/site-packages/spreg/ml_lag.py:557, in ML_Lag.__init__(self, y, x, w, method, epsilon, vm, name_y, name_x, name_w, name_ds)
555 set_warn(self, warn)
556 method = method.upper()
--> 557 BaseML_Lag.__init__(
558 self, y=y, x=x_constant, w=w, method=method, epsilon=epsilon)
559 # increase by 1 to have correct aic and sc, include rho in count
560 self.k += 1

File ~/opt/anaconda3/envs/msda2021/lib/python3.8/site-packages/spreg/ml_lag.py:235, in BaseML_Lag.__init__(self, y, x, w, method, epsilon)
232 self = None
233 return
--> 235 self.rho = res.x[0][0]
237 # compute full log-likelihood, including constants
238 ln2pi = np.log(2.0 * np.pi)

IndexError: invalid index to scalar variable.
</code></pre>
<p>PS.
Before X and y are passed to ML_Lag,</p>
<p>X:</p>
<pre><code>array([[ 0.51, 23.57, 168.22, 2. ],
[ 5.05, 32.47, 142.03, 0. ],
[ 1.19, 22.23, 152.32, 2. ],
...
...
[ 1.21, 19.84, 93.39, 4. ]])
</code></pre>
<p>y:</p>
<pre><code>array([[17.79],
[24.97],
...
...
[15.96]])
</code></pre>
<p>I don't know how to fix it, please help me. Thank you!</p>
 

Latest posts

Top