Published on :
Statistical INTERNAL_CREATION

Choice Model with Nested Logit - PROC BCHOICE Example

This code is also available in: Deutsch Español Français
Awaiting validation
This script analyzes transport mode preferences (Car, Plane, Transit) based on travel time and subjects' age. Data is first created internally then restructured to match the format required by discrete choice models (one row per alternative). The script then compares two Bayesian approaches: a nested logit model grouping ground transportation (Car, Transit) versus air travel, and a classical multinomial logit model.
Data Analysis

Type : INTERNAL_CREATION


The 'Travel' data is created via DATALINES. The 'Travel2' table is derived from 'Travel' to transform the data structure from wide format to long format.

1 Code Block
DATA STEP Data
Explanation :
Creation of the source table 'Travel' containing travel times for each mode, the subject's age, and the observed final choice.
Copied!
1DATA Travel;
2 INPUT AutoTime PlanTime TranTime Age Chosen $;
3 AgeCtr=Age-34;
4 DATALINES;
510.0 4.5 10.5 32 Plane
65.5 4.0 7.5 13 Auto
74.5 6.0 5.5 41 Transit
83.5 2.0 5.0 41 Transit
91.5 4.5 4.0 47 Auto
1010.5 3.0 10.5 24 Plane
117.0 3.0 9.0 27 Auto
129.0 3.5 9.0 21 Plane
134.0 5.0 5.5 23 Auto
1422.0 4.5 22.5 30 Plane
157.5 5.5 10.0 58 Plane
1611.5 3.5 11.5 36 Transit
173.5 4.5 4.5 43 Auto
1812.0 3.0 11.0 33 Plane
1918.0 5.5 20.0 30 Plane
2023.0 5.5 21.5 28 Plane
214.0 3.0 4.5 44 Plane
225.0 2.5 7.0 37 Transit
233.5 2.0 7.0 45 Auto
2412.5 3.5 15.5 35 Plane
251.5 4.0 2.0 22 Auto
26;
2 Code Block
DATA STEP Data
Explanation :
Data restructuring: conversion to long format where each row represents a choice alternative for a subject. Creation of the binary variable 'Choice' (1 if chosen, 0 otherwise).
Copied!
1DATA Travel2(keep=Subject Mode TravTime Age AgeCtr Choice);
2 array Times[3] AutoTime PlanTime TranTime;
3 array Allmodes[3] $ _temporary_ ('Auto' 'Plane' 'Transit');
4 SET Travel;
5 Subject = _n_;
6 DO i = 1 to 3;
7 Mode = Allmodes[i];
8 TravTime = Times[i];
9 Choice = (Chosen eq Mode);
10 OUTPUT;
11 END;
12RUN;
3 Code Block
PROC BCHOICE
Explanation :
Estimation of a Nested Logit model. The 'nest=(1 2 1)' option defines the hierarchical structure by grouping modes 1 (Car) and 3 (Transit) into a nest, separate from mode 2 (Plane).
Copied!
1PROC BCHOICE DATA=Travel2 seed=124 nmc=20000 nthin=2 dic;
2 class Mode Subject / param=ref order=DATA;
3 model Choice = Mode TravTime / choiceset=(Subject) type=nlogit nest=(1 2 1);
4RUN;
4 Code Block
PROC BCHOICE
Explanation :
Estimation of a standard Multinomial Logit model (without nesting structure) on the same data to allow comparison of information criteria (DIC).
Copied!
1PROC BCHOICE DATA=Travel2 seed=124 nmc=20000 nthin=2 dic;
2 class Mode Subject / param=ref order=DATA;
3 model Choice = Mode TravTime / choiceset=(Subject);
4RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : SAS SAMPLE LIBRARY