-
Notifications
You must be signed in to change notification settings - Fork 0
/
MNL with background variables and interaction.R
100 lines (71 loc) · 3.64 KB
/
MNL with background variables and interaction.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
### Load Apollo library
library(apollo)
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list (
modelName = "MNL Combined5",
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,
b_Gender=0, b_Residence=0, b_Clothes=0, b_JeansFreq=0,
b_EnvLabor=0, b_GenderPrice=0, b_EducationEnv=0,
b_ResidenceEnv=0, b_ResidencePrice=0, b_ConcernSwtLabor=0,
b_SkLabor=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) +
b_Gender*Gender + b_Residence*Residence + b_Clothes*Clothes + b_JeansFreq*JeansFreq +
Price1*(Gender*b_GenderPrice + Residence*b_ResidencePrice) +
log(Env1)*(Education*b_EducationEnv + Residence*b_ResidenceEnv) +
log(Labor1)*(log(Env1)*b_EnvLabor + ConcernSwt*b_ConcernSwtLabor + Skepticism*b_SkLabor)
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) +
b_Gender*Gender + b_Residence*Residence + b_Clothes*Clothes + b_JeansFreq*JeansFreq +
Price2*(Gender*b_GenderPrice + Residence*b_ResidencePrice) +
log(Env2)*(Education*b_EducationEnv + Residence*b_ResidenceEnv) +
log(Labor2)*(log(Env2)*b_EnvLabor + ConcernSwt*b_ConcernSwtLabor + Skepticism*b_SkLabor)
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)
### Take product across observation for same individual
#P = apollo_panelProd(P, apollo_inputs, 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)
### Write the results to Excel file
df = read.csv("MNL Combined5_estimates.csv",header=TRUE,sep=",")
library(writexl)
write_xlsx(df,"MNL Combined5_estimates.xlsx")