Conditional Logistic Regression in Stan?

1,572 views
Skip to first unread message

Noah

unread,
Jun 22, 2013, 2:59:21 PM6/22/13
to stan-...@googlegroups.com
Hello,

A few questions related to a conditional logistic regression. 

1) Is there a way in Stan to model a conditional logistic regression?  (I didn't see it in the manual)

2) With a CLR, the posterior is a probability estimate of the outcome for each member of a group, with the within-group probabilities summing to 1.  With many draws from the posterior, I will effectively have many draws of possible probabilities. (I have N draws of probabilities for K members of a race.)  How would I then simulate outcomes?  What is the probability of member i winning? 

3) I have probabilities of winning for each race member (from another model.)  It would be interesting to treat those as priors for a model in Stan.  However, I'm used to thinking about priors on the coefficients as drawn from some distribution, not actual point estimates of the outcomes.  How would I set that up in Stan?

Thanks in advance!

Ben Goodrich

unread,
Jun 22, 2013, 3:31:21 PM6/22/13
to stan-...@googlegroups.com
On Saturday, June 22, 2013 2:59:21 PM UTC-4, Noah wrote:
A few questions related to a conditional logistic regression. 

1) Is there a way in Stan to model a conditional logistic regression?  (I didn't see it in the manual)

I think Stan can do pretty much any regression, but the conditional logistic log-likelihood isn't programmed into Stan yet, so the log-likelihood would have to be evaluated using Stan code and manipulation of the lp__ symbol.
 
2) With a CLR, the posterior is a probability estimate of the outcome for each member of a group, with the within-group probabilities summing to 1.  With many draws from the posterior, I will effectively have many draws of possible probabilities. (I have N draws of probabilities for K members of a race.)  How would I then simulate outcomes?  What is the probability of member i winning?

There is a good explanation of the probabilities and the likelihood here

http://www.stata.com/support/faqs/statistics/within-group-collinearity-and-clogit/

To implement this in Stan, you will need to take logs to get the categorical log-likelihood and you probably want to use some of the log_sum_exp() tricks to prevent numerical overflow. In any event, once you get the posterior probabilities, you can just draw from categorical_rng() for each group.

3) I have probabilities of winning for each race member (from another model.)  It would be interesting to treat those as priors for a model in Stan.  However, I'm used to thinking about priors on the coefficients as drawn from some distribution, not actual point estimates of the outcomes.  How would I set that up in Stan?

Does your model have predictors? If so, it is going to be pretty tedious to analytically derive the distribution of the coefficients conditional on the probability of winning. In Stan, you proceed numerically by adjusting lp__ by the log absolute Jacobian determinant of the transformation from probabilities to coefficients, but this is difficult at best.

Ben

Peter Mahoney

unread,
Sep 2, 2015, 10:49:41 PM9/2/15
to Stan users mailing list
Hi Folks!

Has anyone successfully implemented conditional logistic regression in Stan since this last posting?  I have implemented the methods employed in the attached paper using clogit{survival} in R for a variety of resource selection models, but I would love to give it a go in Stan.  Before trying to brute force my way through the process (as an ecologist and not a statistician), I figured I'd check to see if anyone had example code they would be willing to share...so as not to reinvent the wheel so to speak.  Thanks for any help!

Cheers,
Peter
Ecology_86_2005_1320.pdf

Ben Goodrich

unread,
Sep 2, 2015, 10:55:24 PM9/2/15
to Stan users mailing list
Not that I know of.

Peter Mahoney

unread,
Sep 4, 2015, 11:00:56 AM9/4/15
to Stan users mailing list
Thanks Ben, that's what I had figured, but felt it was worth asking.  I'll give it a shot then...
Cheers,
P

Ben Goodrich

unread,
Sep 4, 2015, 11:33:41 AM9/4/15
to Stan users mailing list
On Friday, September 4, 2015 at 11:00:56 AM UTC-4, Peter Mahoney wrote:
Thanks Ben, that's what I had figured, but felt it was worth asking.  I'll give it a shot then...

The three-term recursion for the normalizing factor is the hard part of a clogit model, but it can be done in a .stan program if you first declare a user-defined function and then define it. See section 17.9 of the current Stan user manual.

Ben

Peter Mahoney

unread,
Sep 8, 2015, 1:48:24 PM9/8/15
to Stan users mailing list
Thanks Ben.  After considering this further, I remembered my old routines capitalize on existing stratified Cox proportional hazard code in the R survival package.  As I understand it, the (partial) likelihoods are the same between stratified Cox and conditional logistic, at least under scenarios where I would need clogit (e.g., single event within a strata for Cox and a single case with multiple controls across many groups).  I'm wondering if it will be easier (and still appropriate) to modify the examples of Cox regression by allowing the baseline hazard to vary by some stratification/grouping variable rather than build the full conditional logistic likelihood?  I appreciate your time, patience, and advice...thanks!

P

Ben Goodrich

unread,
Sep 8, 2015, 2:07:26 PM9/8/15
to Stan users mailing list
Perhaps. That is what the clogit() function in the survival package does.

genenk

unread,
Nov 7, 2015, 11:22:35 AM11/7/15
to Stan users mailing list
Peter, could you share which way you went? I am also interested in running a conditional logistic regression in Stan without reinventing the wheel.

Kruschke's Doing Bayesian Data Analysis has an example implementation in JAGS (Jags-Ynom-XmetMulti-McondLogistic1.R from the archive of programmes at the very bottom of https://sites.google.com/site/doingbayesiandataanalysis/software-installation). However, I don't know whether that is useful as a starting point to translate into Stan.

Bob Carpenter

unread,
Nov 8, 2015, 3:12:50 AM11/8/15
to stan-...@googlegroups.com
That model would be trivial to translate directly to Stan.

We wouldn't recommend the fat priors Krushcke's using in
his examples, though. Especially given that he's standardized
all the predictors and it's a logisitc regression.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ben Goodrich

unread,
Nov 8, 2015, 9:46:55 AM11/8/15
to Stan users mailing list
On Sunday, November 8, 2015 at 3:12:50 AM UTC-5, Bob Carpenter wrote:
That model would be trivial to translate directly to Stan.

True. Although that conditional logit model is not the conditional logit model that goes by the name clogit in Stata and in the survival R package, which is what the older posts by Noah and Peter on this thread were referring to.  We need genenk to clarify which model is of interest.

Ben

Peter Mahoney

unread,
Nov 9, 2015, 2:07:44 PM11/9/15
to Stan users mailing list
Genenk, the code from Kruschke's book wasn't available when I started looking for a solution so, as Ben pointed out, I started to take an approach akin to what clogit() implements in R.  I haven't gotten very far as other priorities continue to displace my need for a clogit implementation in Stan, but I've been considering ways of specifying a stratified version of the example Cox code (Leuk and LeukFr) provided through Stan's examples repository on github: https://github.com/stan-dev/example-models/wiki/BUGS-Examples-Sorted-Alphabetically.

Although Kruschke's code looks straightforward, when/if I come up with a solution for a stratified Cox, I'll be sure to post some example code.  Sorry I don't have more for you.

P

Ben Goodrich

unread,
Nov 9, 2015, 2:16:20 PM11/9/15
to Stan users mailing list
On Monday, November 9, 2015 at 2:07:44 PM UTC-5, Peter Mahoney wrote:
Although Kruschke's code looks straightforward, when/if I come up with a solution for a stratified Cox, I'll be sure to post some example code.  Sorry I don't have more for you.

I believe what Kruschke calls "conditional logit" is what economists would call "nested logit". I don't see any resemblance between it and what economists call a "clogit" model.

Ben
 

David Muller

unread,
Nov 10, 2015, 3:15:00 PM11/10/15
to Stan users mailing list
I've put up a naively implemented example conditional logistic model (using the same likelihood as clogit in Stata and R's survival package) here:


It uses the recursion trick to evaluate the denominator of the likelihood, so should scale reasonably well to situations with more than one event per group/stratum. It's also pretty much untested... I've only checked one model against survival::clogit in R and the estimates look comparable (see the R script in the repository). In any case, hopefully this is helpful as a starting point.

David

Bob Carpenter

unread,
Nov 10, 2015, 4:06:53 PM11/10/15
to stan-...@googlegroups.com
Thanks for sharing that.

And sorry for the clunky syntax requiring forward declarations
of recursive functions. I should fix that so it's not necessary.

- Bob

David Muller

unread,
Nov 10, 2015, 6:38:02 PM11/10/15
to Stan users mailing list
No problem Bob.

The forward declaration requirement isn't so bad. One thing that will be really nice for these kinds of models, where the likelihood is defined at the group level rather than the observation level, is the R-style indexing. Being able to index directly using a logical vector would save a bunch of looping and make it harder for me to make silly errors in extracting sub-vectors/matrices. That said, I'm pretty new to Stan so I've probably not gone about doing this in the best way.

David

Bob Carpenter

unread,
Nov 10, 2015, 6:47:51 PM11/10/15
to stan-...@googlegroups.com

> On Nov 10, 2015, at 6:38 PM, David Muller <davi...@gmail.com> wrote:
>
> No problem Bob.
>
> The forward declaration requirement isn't so bad. One thing that will be really nice for these kinds of models, where the likelihood is defined at the group level rather than the observation level, is the R-style indexing. Being able to index directly using a logical vector would save a bunch of looping and make it harder for me to make silly errors in extracting sub-vectors/matrices. That said, I'm pretty new to Stan so I've probably not gone about doing this in the best way.

You're in luck there --- R-like indexing should be in Stan
with the next release.

- Bob

David Muller

unread,
Nov 10, 2015, 6:52:26 PM11/10/15
to Stan users mailing list
This is brilliant news. Thanks so much to you and the team for continuing to improve Stan.

David

Ben Goodrich

unread,
Nov 11, 2015, 10:56:51 AM11/11/15
to Stan users mailing list
On Tuesday, November 10, 2015 at 3:15:00 PM UTC-5, David Muller wrote:
I've put up a naively implemented example conditional logistic model (using the same likelihood as clogit in Stata and R's survival package) here:


Let me echo Bob's thanks for putting this together. I would love to see someone layer on a likelihood contribution (such as negative binomial) for the number of successes per group as a function of group-level characteristics. Even IRT-like models could be done this way with a part that models how many times a question will be answered correctly, and then conditional on that, a part that models which students are the successful ones for each question.

Ben

Peter Mahoney

unread,
Nov 11, 2015, 11:16:48 AM11/11/15
to Stan users mailing list
From someone who will hopefully be utilizing the product of your efforts...thanks David!!  I'll run some simulations in the coming weeks to confirm things are working, at least for the constraints acting on my system (and step selection functions in general). 

Thanks again,
Peter

David Muller

unread,
Nov 11, 2015, 5:14:07 PM11/11/15
to Stan users mailing list

On Wednesday, 11 November 2015 16:16:48 UTC, Peter Mahoney wrote:
From someone who will hopefully be utilizing the product of your efforts...thanks David!!  I'll run some simulations in the coming weeks to confirm things are working, at least for the constraints acting on my system (and step selection functions in general). 

Thanks again,
Peter

No problem Peter, I hope it's helpful. Let me know if anything isn't working as you would expect.
 
On Wednesday, 11 November 2015 15:56:51 UTC, Ben Goodrich wrote:
On Tuesday, November 10, 2015 at 3:15:00 PM UTC-5, David Muller wrote:
I've put up a naively implemented example conditional logistic model (using the same likelihood as clogit in Stata and R's survival package) here:

Let me echo Bob's thanks for putting this together. I would love to see someone layer on a likelihood contribution (such as negative binomial) for the number of successes per group as a function of group-level characteristics. Even IRT-like models could be done this way with a part that models how many times a question will be answered correctly, and then conditional on that, a part that models which students are the successful ones for each question.

Ben

That's an interesting idea. It should be straightforward to add  a negative binomial or binomial regression on the number of successes at the group level. It's a bit outside any application that I would use at the moment, so I can't really justify spending the time to do so, but perhaps someone else who is interested in that kind of model will jump in.

David

genenk

unread,
Nov 17, 2015, 2:48:38 PM11/17/15
to Stan users mailing list
Stata's clogit and asclogit seem to correspond to sections 17.4.4 (A Conditional Fixed Effects Estimator) and 18.2.3 (The Conditional Logit Model), respectively, of Greene's Econometric Analysis 7ed and both fall under the Logit Model in chapter 3 of Train's Discrete Choice 2ed, which includes all logit models in which IIA holds (meaning homoscedastic and uncorrelated errors, if remember the conditions correctly). Greene explains the different names while Train lumps these models together, which I find more useful because the difference comes from your data and what interactions you are trying to infer, rather than from the mathematics. 

As Ben guessed, I am interested in nested / mixed logit models as these terms are used in economics. These models relax the IIA assumption but it will probably be more appropriate to discuss them in another thread . I thought Kruschke's terminology is the Bayesian way to refer to nested logit but that doesn't seem to be the case. Actually now I think that his "conditional logistic regression" is in fact the sequential logit from e.g. here http://data.princeton.edu/wws509/stata/c6s4.html and here compared with the nested logit http://onlinelibrary.wiley.com/doi/10.1111/j.1468-5876.2008.00458.x/pdf

I won't hijack this thread further but also wanted to post to say thanks for the incoming R-style indexing!

Jeff Alstott

unread,
Mar 2, 2016, 10:04:39 PM3/2/16
to Stan users mailing list
Has anyone, such as Peter, had further developments on this? I appreciate the code, David, and have been trying it out. Unfortunately, the current code runs very slowly for me. If no one else has advanced on this code I will try my hand at speeding it up, but I'm new to Stan and so it will be a long climb.

Thanks!
Jeff

Peter Mahoney

unread,
Oct 10, 2016, 3:02:15 PM10/10/16
to Stan users mailing list
Hi David,

As an update, I've had a chance (finally) to implement your code and everything seems to be working great!  The estimates are comparable between survival::clogit and stan_clogit across multiple datasets.  However, my need is limited to a single 'case' per stratum so I can't speak to the accuracy beyond this constraint.

As Jeff pointed out, the code as implemented didn't scale well for large datasets.  I've attached a very simple modification (lines 112-113) that improves speed by an order of magnitude or more when dealing with much larger datasets than in the 'infert' example.  Please NOTE that the data needs to be sorted by stratum/group for this produce accurate results!  I'm going to continue to tailor it towards my specific needs, but I thought this might be helpful for Jeff and others that are hoping for faster model fitting/convergence.

Once again, I really appreciate all the help!
Peter
clogit2.stan
stan_clogit2.R
Reply all
Reply to author
Forward
0 new messages