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