Ah, thank you! I really appreciate you taking the time to offer your advice.
I expanded the code to include the other parameters such as "Trust" and "Equity". I get a warning message with the model results ("Some eigenvalues of Hessian are positive, indicating potential problems!") and NaN's. From the website, it seems like this could be because I have too many parameters? Or because the scale of most parameters is ~ -2:3, while age goes up to the 80's?
Model output:
Model name : Synthesis 2 socio-demos
Model description : Analysis game synthesis with Apollo3
Model run at : 2021-06-11 16:15:25
Estimation method : bfgs
Model diagnosis : successful convergence
Number of individuals : 343
Number of rows in database : 74583
Number of modelled outcomes : 74583
Number of cores used : 1
Model without mixing
LL(start) : -103394
LL(0) : -103394
LL(final) : -79391.72
Rho-square (0) : 0.2321
Adj.Rho-square (0) : 0.2318
AIC : 158861.5
BIC : 159221
Estimated parameters : 39
Time taken (hh:mm:ss) : 00:06:11.62
pre-estimation : 00:00:45.36
estimation : 00:01:8.57
post-estimation : 00:04:17.69
Iterations : 48
Min abs eigenvalue of Hessian : 1e-06
Some eigenvalues of Hessian are positive, indicating potential problems!
Estimates:
Estimate s.e. t.rat.(0) Rob.s.e. Rob.t.rat.(0)
asc_farm 0.000000 NA NA NA NA
asc_scare -0.798089 2927.471317 -2.7262e-04 NaN NaN
asc_kill -1.214872 977.596951 -0.001243 NaN NaN
asc_conserve -2.053032 NaN NaN 26.445847 -0.077632
b_Age_farm 0.000000 NA NA NA NA
b_Age_scare -0.003678 6.6644e-04 -5.518487 0.003196 -1.150768
b_Age_kill -0.001337 8.8515e-04 -1.510639 0.004311 -0.310161
b_Age_conserve -0.001868 8.3681e-04 -2.232626 0.003612 -0.517289
b_Trust_farm 0.000000 NA NA NA NA
b_Trust_scare 0.001645 0.007326 0.224508 0.029385 0.055972
b_Trust_kill 0.044588 0.010816 4.122471 0.037690 1.183018
b_Trust_conserve -0.030129 0.009336 -3.227289 0.037431 -0.804918
b_Equity_farm 0.000000 NA NA NA NA
b_Equity_scare -0.011824 0.008766 -1.348741 0.040355 -0.292995
b_Equity_kill -0.011405 0.012179 -0.936481 0.046081 -0.247503
b_Equity_conserve -0.032193 0.011277 -2.854669 0.048071 -0.669689
b_Familiarity_farm 0.000000 NA NA NA NA
b_Familiarity_scare 0.045733 0.016108 2.839158 0.060236 0.759229
b_Familiarity_kill -0.235252 0.020426 -11.517390 0.076715 -3.066572
b_Familiarity_conserve 0.130958 0.021058 6.218751 0.114985 1.138907
b_Female_farm 0.000000 NA NA NA NA
b_Female_scare 0.167327 0.021315 7.850103 0.088403 1.892773
b_Female_kill -0.246329 0.032115 -7.670155 0.114404 -2.153152
b_Female_conserve -0.070286 0.027130 -2.590730 0.110018 -0.638860
b_Farmer_farm 0.000000 NA NA NA NA
b_Farmer_scare 0.056709 0.020160 2.812954 0.086272 0.657324
b_Farmer_kill 0.218066 0.027836 7.834022 0.111817 1.950203
b_Farmer_conserve -0.103725 0.026362 -3.934710 0.123398 -0.840574
b_Education_farm 0.000000 NA NA NA NA
b_Education_scare 0.024441 0.002696 9.066551 0.011805 2.070384
b_Education_kill 0.060662 0.003702 16.384741 0.013540 4.480230
b_Education_conserve 0.002126 0.003963 0.536553 0.014672 0.144923
b_Treatments_Baseline_farm 0.000000 NA NA NA NA
b_Treatments_Baseline_scare -0.541594 2927.471313 -1.8500e-04 NaN NaN
b_Treatments_Baseline_kill -0.468782 977.596947 -4.7952e-04 1.040096 -0.450710
b_Treatments_Baseline_conserve -1.525000 NaN NaN 61.468082 -0.024810
b_Treatments_Subsidy_farm 0.000000 NA NA NA NA
b_Treatments_Subsidy_scare -0.658473 2927.471313 -2.2493e-04 NaN NaN
b_Treatments_Subsidy_kill -0.452128 977.596947 -4.6249e-04 NaN NaN
b_Treatments_Subsidy_conserve -0.002891 NaN NaN NaN NaN
b_Treatments_Deterrents_farm 0.000000 NA NA NA NA
b_Treatments_Deterrents_scare 1.021462 2927.471313 3.4892e-04 104.885250 0.009739
b_Treatments_Deterrents_kill 0.099315 977.596947 1.0159e-04 NaN NaN
b_Treatments_Deterrents_conserve -0.893262 NaN NaN NaN NaN
b_Treatments_Agglomeration_farm 0.000000 NA NA NA NA
b_Treatments_Agglomeration_scare -0.619484 2927.471313 -2.1161e-04 NaN NaN
b_Treatments_Agglomeration_kill -0.393277 977.596947 -4.0229e-04 NaN NaN
b_Treatments_Agglomeration_conserve 0.368120 NaN NaN NaN NaN
b_Animal_count 0.005493 5.1233e-04 10.720900 0.001824 3.011669
b_Cons_lag 0.341196 0.006351 53.724014 0.021815 15.640671
b_Kill_lag 0.317892 0.008849 35.924305 0.032790 9.694877
WARNING: Availability not provided (or some elements are NA). Full availability assumed.
Overview of choices for MNL model component :
farm scare kill conserve
Times available 74583.00 74583.00 74583.00 74583.0
Times chosen 34991.00 20142.00 7964.00 11486.0
Percentage chosen overall 46.92 27.01 10.68 15.4
Percentage chosen when available 46.92 27.01 10.68 15.4
Model code:
Code: Select all
#### LOAD LIBRARY AND DEFINE CORE SETTINGS
### Clear memory
rm(list = ls())
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName ="Synthesis 2 socio-demos",
modelDescr ="Analysis game synthesis with Apollo3",
indivID ="HHID1"
)
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS
database<-read_csv(here("Synthesis 5 data.csv"))
database <- as.data.frame(database)
database <- na.omit(database)
#### DEFINE MODEL PARAMETERS
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_farm = 0,
asc_scare = 0,
asc_kill = 0,
asc_conserve = 0,
b_Age_farm = 0,
b_Age_scare = 0,
b_Age_kill = 0,
b_Age_conserve= 0,
b_Trust_farm = 0,
b_Trust_scare = 0,
b_Trust_kill = 0,
b_Trust_conserve= 0,
b_Equity_farm = 0,
b_Equity_scare = 0,
b_Equity_kill = 0,
b_Equity_conserve= 0,
b_Familiarity_farm = 0,
b_Familiarity_scare = 0,
b_Familiarity_kill = 0,
b_Familiarity_conserve= 0,
b_Female_farm = 0,
b_Female_scare = 0,
b_Female_kill = 0,
b_Female_conserve= 0,
b_Farmer_farm = 0,
b_Farmer_scare = 0,
b_Farmer_kill = 0,
b_Farmer_conserve= 0,
b_Education_farm = 0,
b_Education_scare = 0,
b_Education_kill = 0,
b_Education_conserve= 0,
b_Treatments_Baseline_farm = 0,
b_Treatments_Baseline_scare = 0,
b_Treatments_Baseline_kill = 0,
b_Treatments_Baseline_conserve = 0,
b_Treatments_Subsidy_farm = 0,
b_Treatments_Subsidy_scare = 0,
b_Treatments_Subsidy_kill = 0,
b_Treatments_Subsidy_conserve = 0,
b_Treatments_Deterrents_farm = 0,
b_Treatments_Deterrents_scare = 0,
b_Treatments_Deterrents_kill = 0,
b_Treatments_Deterrents_conserve = 0,
b_Treatments_Agglomeration_farm = 0,
b_Treatments_Agglomeration_scare = 0,
b_Treatments_Agglomeration_kill = 0,
b_Treatments_Agglomeration_conserve = 0,
b_Animal_count = 0,
b_Cons_lag = 0,
b_Kill_lag = 0)
apollo_beta_fixed = c() # no parameters fixed at starting value
apollo_fixed = c('asc_farm', 'b_Age_farm', 'b_Farmer_farm', 'b_Female_farm', 'b_Education_farm',
'b_Treatments_Subsidy_farm', 'b_Treatments_Deterrents_farm',
'b_Treatments_Agglomeration_farm', 'b_Treatments_Baseline_farm',
'b_Familiarity_farm', 'b_Equity_farm', 'b_Trust_farm')
#### 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
TB <- (Treatments=="Baseline" )
TS <- (Treatments=="Subsidy" )
TD <- (Treatments=="Deterrents" )
TA <- (Treatments=="Agglomeration")
V = list()
V[['farm']] = asc_farm + b_Farmer_farm *Farmer + b_Equity_farm *Equity + b_Trust_farm *Trust + b_Familiarity_farm *Familiarity + b_Age_farm *Age + b_Female_farm *Female + b_Education_farm *Education + b_Treatments_Baseline_farm *TB + b_Treatments_Subsidy_farm *TS + b_Treatments_Deterrents_farm *TD + b_Treatments_Agglomeration_farm *TA
V[['scare']] = asc_scare + b_Farmer_scare *Farmer + b_Equity_scare *Equity + b_Trust_scare *Trust + b_Familiarity_scare *Familiarity + b_Age_scare *Age + b_Female_scare *Female + b_Education_scare *Education + b_Treatments_Baseline_scare *TB + b_Treatments_Subsidy_scare *TS + b_Treatments_Deterrents_scare *TD + b_Treatments_Agglomeration_scare *TA
V[['kill']] = asc_kill + b_Farmer_kill *Farmer + b_Equity_kill *Equity + b_Trust_kill *Trust + b_Familiarity_kill *Familiarity + b_Age_kill *Age + b_Female_kill *Female + b_Education_kill *Education + b_Treatments_Baseline_kill *TB + b_Treatments_Subsidy_kill *TS + b_Treatments_Deterrents_kill *TD + b_Treatments_Agglomeration_kill *TA + b_Kill_lag*Kill_lag
V[['conserve']] = asc_conserve + b_Farmer_conserve*Farmer + b_Equity_conserve *Equity + b_Trust_conserve*Trust + b_Familiarity_conserve *Familiarity + b_Age_conserve*Age + b_Female_conserve*Female + b_Education_conserve*Education + b_Treatments_Baseline_conserve*TB + b_Treatments_Subsidy_conserve*TS + b_Treatments_Deterrents_conserve*TD + b_Treatments_Agglomeration_conserve*TA + b_Cons_lag*Cons_lag + b_Animal_count*Animal_count
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(farm=0, scare=1, kill=2, conserve=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, list(writeIter=FALSE))
#### MODEL OUTPUTS
apollo_modelOutput(model)
apollo_saveOutput(model)
Additionally, I would like to incorporate some interactions between parameters for two of the utilities (e.g., between "Trust" and "Cons_lag", or "Farmer" and "Subsidy"), but I get NAs for s.e. etc.
Relevant excerpt here, but full code below:
Code: Select all
### Create alternative specific constants and coefficients using interactions with socio-demographics
Fam_LagCons_conserve = Familiarity + b_Fam_LagCons *Cons_lag
Trust_LagCons_conserve = Trust + b_Trust_LagCons *Cons_lag
Fam_LagKill_kill = Familiarity + b_Fam_LagKill *Kill_lag
Trust_LagKill_kill = Trust + b_Trust_LagKill *Kill_lag
Subsidy_Farmer_conserve = Farmer + b_Farmer_Subsidy *TS
V[['conserve']] = asc_conserve + [...] Fam_LagCons_conserve * b_Cons_lag + Trust_LagCons_conserve * b_Cons_lag + Subsidy_Farmer_conserve * Farmer
Full Code:
Code: Select all
#### LOAD LIBRARY AND DEFINE CORE SETTINGS
### Clear memory
rm(list = ls())
### Initialise code
apollo_initialise()
### Set core controls
apollo_control = list(
modelName ="Synthesis 2 socio-demos",
modelDescr ="Analysis game synthesis with Apollo3",
indivID ="HHID1"
)
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS
database<-read_csv(here("Synthesis 5 data.csv"))
database <- as.data.frame(database)
database <- na.omit(database)
#### DEFINE MODEL PARAMETERS
### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_farm = 0,
asc_scare = 0,
asc_kill = 0,
asc_conserve = 0,
b_Age_farm = 0,
b_Age_scare = 0,
b_Age_kill = 0,
b_Age_conserve= 0,
b_Trust_farm = 0,
b_Trust_scare = 0,
b_Trust_kill = 0,
b_Trust_conserve= 0,
b_Equity_farm = 0,
b_Equity_scare = 0,
b_Equity_kill = 0,
b_Equity_conserve= 0,
b_Familiarity_farm = 0,
b_Familiarity_scare = 0,
b_Familiarity_kill = 0,
b_Familiarity_conserve= 0,
b_Female_farm = 0,
b_Female_scare = 0,
b_Female_kill = 0,
b_Female_conserve= 0,
b_Farmer_farm = 0,
b_Farmer_scare = 0,
b_Farmer_kill = 0,
b_Farmer_conserve= 0,
b_Education_farm = 0,
b_Education_scare = 0,
b_Education_kill = 0,
b_Education_conserve= 0,
b_Treatments_Baseline_farm = 0,
b_Treatments_Baseline_scare = 0,
b_Treatments_Baseline_kill = 0,
b_Treatments_Baseline_conserve = 0,
b_Treatments_Subsidy_farm = 0,
b_Treatments_Subsidy_scare = 0,
b_Treatments_Subsidy_kill = 0,
b_Treatments_Subsidy_conserve = 0,
b_Treatments_Deterrents_farm = 0,
b_Treatments_Deterrents_scare = 0,
b_Treatments_Deterrents_kill = 0,
b_Treatments_Deterrents_conserve = 0,
b_Treatments_Agglomeration_farm = 0,
b_Treatments_Agglomeration_scare = 0,
b_Treatments_Agglomeration_kill = 0,
b_Treatments_Agglomeration_conserve = 0,
b_Fam_LagCons = 0,
b_Trust_LagCons = 0,
b_Fam_LagKill = 0,
b_Trust_LagKill = 0,
b_Farmer_Subsidy= 0,
b_Animal_count = 0,
b_Cons_lag = 0,
b_Kill_lag = 0)
apollo_beta_fixed = c() # no parameters fixed at starting value
apollo_fixed = c('asc_farm', 'b_Age_farm', 'b_Farmer_farm', 'b_Female_farm', 'b_Education_farm',
'b_Treatments_Subsidy_farm', 'b_Treatments_Deterrents_farm',
'b_Treatments_Agglomeration_farm', 'b_Treatments_Baseline_farm',
'b_Familiarity_farm', 'b_Equity_farm', 'b_Trust_farm')
#### 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
TB <- (Treatments=="Baseline" )
TS <- (Treatments=="Subsidy" )
TD <- (Treatments=="Deterrents" )
TA <- (Treatments=="Agglomeration")
### Create alternative specific constants and coefficients using interactions with socio-demographics
Fam_LagCons_conserve = Familiarity + b_Fam_LagCons *Cons_lag
Trust_LagCons_conserve = Trust + b_Trust_LagCons *Cons_lag
Fam_LagKill_kill = Familiarity + b_Fam_LagKill *Kill_lag
Trust_LagKill_kill = Trust + b_Trust_LagKill *Kill_lag
Subsidy_Farmer_conserve = Farmer + b_Farmer_Subsidy *TS
V = list()
V[['farm']] = asc_farm + b_Farmer_farm *Farmer + b_Equity_farm *Equity + b_Trust_farm *Trust + b_Familiarity_farm *Familiarity + b_Age_farm *Age + b_Female_farm *Female + b_Education_farm *Education + b_Treatments_Baseline_farm *TB + b_Treatments_Subsidy_farm *TS + b_Treatments_Deterrents_farm *TD + b_Treatments_Agglomeration_farm *TA
V[['scare']] = asc_scare + b_Farmer_scare *Farmer + b_Equity_scare *Equity + b_Trust_scare *Trust + b_Familiarity_scare *Familiarity + b_Age_scare *Age + b_Female_scare *Female + b_Education_scare *Education + b_Treatments_Baseline_scare *TB + b_Treatments_Subsidy_scare *TS + b_Treatments_Deterrents_scare *TD + b_Treatments_Agglomeration_scare *TA
V[['kill']] = asc_kill + b_Farmer_kill *Farmer + b_Equity_kill *Equity + b_Trust_kill *Trust + b_Familiarity_kill *Familiarity + b_Age_kill *Age + b_Female_kill *Female + b_Education_kill *Education + b_Treatments_Baseline_kill *TB + b_Treatments_Subsidy_kill *TS + b_Treatments_Deterrents_kill *TD + b_Treatments_Agglomeration_kill *TA + b_Kill_lag*Kill_lag + Fam_LagKill_kill * b_Kill_lag + Trust_LagKill_kill * b_Kill_lag
V[['conserve']] = asc_conserve + b_Farmer_conserve*Farmer + b_Equity_conserve *Equity + b_Trust_conserve*Trust + b_Familiarity_conserve *Familiarity + b_Age_conserve*Age + b_Female_conserve*Female + b_Education_conserve*Education + b_Treatments_Baseline_conserve*TB + b_Treatments_Subsidy_conserve*TS + b_Treatments_Deterrents_conserve*TD + b_Treatments_Agglomeration_conserve*TA + b_Cons_lag*Cons_lag + b_Animal_count*Animal_count + Fam_LagCons_conserve * b_Cons_lag + Trust_LagCons_conserve * b_Cons_lag + Subsidy_Farmer_conserve * Farmer
### Define settings for MNL model component
mnl_settings = list(
alternatives = c(farm=0, scare=1, kill=2, conserve=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, list(writeIter=FALSE))
#### MODEL OUTPUTS
apollo_modelOutput(model)
apollo_saveOutput(model)