Context variable

12 views
Skip to first unread message

katrina gething

unread,
Apr 21, 2024, 6:49:18 AMApr 21
to Biogeme
Hello,

Absolute beginner here. I have data from a labelled dce: 12 choice tasks per participant, each choice task includes two choices made up of 5 attributes and there is a context variable (patient: chronic condition, serious condition, terminal condition), so that each participant sees 4 random choice tasks under each context variable.  

I have been looking for code to analyse this data. I have successfully run an MNL over the data, but I'm not sure how to include the context variables. 

Code I have used is below:

## Import libraries necessary for model estimation
import pandas as pd
import biogeme.database as db
import biogeme.biogeme as bio
from biogeme import models
from biogeme.expressions import Beta, Variable, log, exp


df = pd.read_csv('experiment clean3.txt', sep='\t')
database = db.Database('experiment clean3', df)

globals().update(database.variables)

## Exclude observations
#exclude = (pref1 == 99999)
#database.remove(exclude)

ASC1 = Beta('ASC Alt 1',0,None,None,0)

Acc1 = Beta('Access all failed good',0,None,None,0)
Acc2 = Beta('Access any phase good',0,None,None,0)
Rou1 = Beta('Route approved doctor',0,None,None,0)
Rou2 = Beta('Route any doctor',0,None,None,0)
Rou3 = Beta('Route pharmacist',0,None,None,0)
Int1 = Beta('Low intoxicate',0,None,None,0)
Fm1 = Beta('Liquid',0,None,None,0)
Fm2 = Beta('Plant material',0,None,None,0)
Ct1 = Beta('Patient pays',0,None,None,0)
Ct2 = Beta('Insurance',0,None,None,0)


V1 = (ASC1 +
      Acc1*(access1==1) + Acc2*(access1==2) +
      Rou1*(route1==1) + Rou2*(route1==2) + Rou3*(route1==3) +
      Int1*(intox1==1) +
      Fm1*(form1==1) + Fm2*(form1==2) +
      Ct1*(cost1==1) + Ct2*(cost1==2))
V2 = (
      Acc1*(access2==1) + Acc2*(access2==2) +
      Rou1*(route2==1) + Rou2*(route2==2) + Rou3*(route2==3) +
      Int1*(intox2==1) +
      Fm1*(form2==1) + Fm2*(form2==2) +
      Ct1*(cost2==1) + Ct2*(cost2==2))

#Associate utility functions with the numbering of alternatives
utility = {1: V1, 2: V2}

#### Definition of the model. This is the contribution of each observation to the log likelihood function.
availability = {1: 1, 2: 1}

## Definition of the model. This is the contribution of each observation to the log likelihood function.
prob = models.logit(utility,availability,pref1)

## Create the Biogeme object
biogemeObject = bio.BIOGEME(database, log(prob))
biogemeObject.modelName = '_MNL'

## Estimate the parameters
results = biogemeObject.estimate()

## Print the results
pandasResults = results.getEstimatedParameters(onlyRobust=False)
print(pandasResults)

Thank you in advance

Michel Bierlaire

unread,
Apr 21, 2024, 7:02:15 AMApr 21
to katrinag...@gmail.com, Michel Bierlaire, Biogeme
The context variables are used to implement interactions.

It means that some behavioral parameters vary with the context. For instance

Ct2_chronic = Beta('Insurance_chronic',0,None,None,0)
Ct2_serious = Beta('Insurance_serious',0,None,None,0)
Ct2_terminal = Beta('Insurance_terminal',0,None,None,0)

So that Ct2 = Ct2_chronic) * (chronic == 1) + Ct2_serious * (serious == 1) + Ct2_terminal * (terminal ==1) [I am just assuming one possible coding of the context variable]

Finally, you keep the
Ct2*(cost2==2)

Yoi can perform a similar transform for any parameter. Just make sure that it makes sense in your context.
> --
> You received this message because you are subscribed to the Google Groups "Biogeme" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to biogeme+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/biogeme/d925a368-8d6e-46fe-a84c-03aca0c8c996n%40googlegroups.com.

Michel Bierlaire
Transport and Mobility Laboratory
School of Architecture, Civil and Environmental Engineering
EPFL - Ecole Polytechnique Fédérale de Lausanne
http://transp-or.epfl.ch
http://people.epfl.ch/michel.bierlaire

Michel Bierlaire

unread,
Apr 21, 2024, 7:07:26 AMApr 21
to katrinag...@gmail.com, Michel Bierlaire, Biogeme
By the way, this can be useful:
https://youtu.be/35MREkzMLOw?si=f_O8VHUjLLEfTVaQ

> On 20 Apr 2024, at 04:32, katrina gething <katrinag...@gmail.com> wrote:
>
Reply all
Reply to author
Forward
0 new messages