-
Notifications
You must be signed in to change notification settings - Fork 0
/
MNL main variables only.R
82 lines (53 loc) · 2.45 KB
/
MNL main variables only.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list (
modelName = "MNL model",
modelDescr = "MNL model",
indivID = "ID", ## ID of participants
mixing = FALSE ## mixed logit or random distribution parameters
)
apollo_control$panelData = FALSE ## define if there are panel data (TRUE if panel data)
#### LOAD DATA
database = read.csv("Data.csv",header=TRUE,sep=";")
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(c=0, asc_opt_out=0,
b_Price=0, b_Origin=0, b_Fiber=0, b_Wash=0, b_Dry=0, b_Env=0, b_Labor=0)
### Vector with names (in quotes) of parameters to be kept fixed at their starting value in apollo_beta, use apollo_beta_fixed = c() if none
apollo_fixed = c("asc_opt_out")
#### GROUP AND VALIDATE INPUTS
apollo_inputs = apollo_validateInputs()
#### DEFINE MODEL AND LIKELIHOOD FUNCTION
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
### Attach inputs and detach after function exit
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))
### Create list of probabilities P
P = list()
### List of utilities: these must use the same names as in mnl_settings, order is irrelevant
V = list()
V[['alt1']] = c + b_Price*Price1 + b_Origin*Origin1 + b_Fiber*Fiber1 +
b_Wash*Wash1 + b_Dry*Dry1 + b_Env*log(Env1) + b_Labor*log(Labor1)
V[['alt2']] = c + b_Price*Price2 + b_Origin*Origin2 + b_Fiber*Fiber2 +
b_Wash*Wash2 + b_Dry*Dry2 + b_Env*log(Env2) + b_Labor*log(Labor2)
V[['opt_out']] = asc_opt_out
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(alt1=1, alt2=2, opt_out=3),
avail = 1,
choiceVar = Choice,
V = V
)
### Compute probabilities using MNL model
P[['model']] = apollo_mnl(mnl_settings, functionality)
### Prepare and return outputs of function
P = apollo_prepareProb(P, apollo_inputs, functionality)
return(P)
}
#### MODEL ESTIMATION
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)
#### MODEL OUTPUTS
apollo_modelOutput(model,modelOutput_settings=list(printPVal=TRUE))
apollo_saveOutput(model)