UnboundLocalError: local variable 'wls_results' referenced before assignment (glm.py, 386)

Bug #422216 reported by David Warde-Farley
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
statsmodels
Confirmed
Undecided
Unassigned

Bug Description

It says that binomial family models can take 2D responses (presumably for multinomial regression?). Neither appears to work; am I doing something wrong?

Exactly what it says above.

In [141]: Y = zeros((700, 3))

In [142]: X = randn(700, 10)

In [143]: Y[arange(700),random_integers(3,size=700)-1] = 1

In [144]: a = sm.GLM(Y, X, family=sm.family.Binomial())

In [145]: a.fit()
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)

/Users/dwf/<ipython console> in <module>()

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/scikits.statsmodels-0.1.0b1-py2.5.egg/scikits/statsmodels/glm.pyc in fit(self, maxiter, method, tol, data_weights, scale)
    384 self.iteration += 1
    385 self.mu = mu
--> 386 glm_results = GLMResults(self, wls_results.params,
    387 wls_results.normalized_cov_params, self.scale)
    388 glm_results.bse = np.sqrt(np.diag(wls_results.cov_params(\

UnboundLocalError: local variable 'wls_results' referenced before assignment

In [146]: a = sm.GLM(Y[:,:-1], X, family=sm.family.Binomial())

In [147]: a.fit()
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)

/Users/dwf/<ipython console> in <module>()

/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/scikits.statsmodels-0.1.0b1-py2.5.egg/scikits/statsmodels/glm.pyc in fit(self, maxiter, method, tol, data_weights, scale)
    384 self.iteration += 1
    385 self.mu = mu
--> 386 glm_results = GLMResults(self, wls_results.params,
    387 wls_results.normalized_cov_params, self.scale)
    388 glm_results.bse = np.sqrt(np.diag(wls_results.cov_params(\

UnboundLocalError: local variable 'wls_results' referenced before assignment

Tags: glm
Revision history for this message
Skipper Seabold (jsseabold) wrote :

I'm afraid the description is imprecise. Instead of 2d data it should be 2d with shape[1] = 2. This is for binomial data that has a different number of trials for each observation and so the response data is (success, failure) and the argument data_weights then needs to be used in fit with data_weights set to success+failure. There should be an example of this in the examples directory.

Unfortunately, there is not support for multinomial data yet. There was some discussion about extending this, and someone even offered some code, but I just haven't had the time to extend it. I think it would be pretty straightforward to implement though.

Skipper

Changed in statsmodels:
status: New → Confirmed
Revision history for this message
David Warde-Farley (david-warde-farley) wrote :

Aha. Well, I imagine the UnboundLocal is still a bug. :)

Revision history for this message
joep (josef-pktd) wrote :

UnboundLocal is still a bug, we are not supposed to have a code path that get's us there

The while loop needs to be made more robust. If we get to this point, without raising any earlier errors (still missing input checks), then the while loop has to be executed at least once for the wls_results to be available.

what does self.family.deviance(self.endog, mu) return before the while loop is entered in this case?

Also maxiter needs to be at least 2 for the while loop to execute.

This is from reading the code, I did not run yet this example to see where the actual problem occurs, but we need to include valid input checks for cases like this which fail at some random point.

joep (josef-pktd)
tags: added: glm
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.