Important: Read this before posting to this forum

  1. This forum is for questions related to the use of Apollo. We will answer some general choice modelling questions too, where appropriate, and time permitting. We cannot answer questions about how to estimate choice models with other software packages.
  2. There is a very detailed manual for Apollo available at http://www.ApolloChoiceModelling.com/manual.html. This contains detailed descriptions of the various Apollo functions, and numerous examples are available at http://www.ApolloChoiceModelling.com/examples.html. In addition, help files are available for all functions, using e.g. ?apollo_mnl
  3. Before asking a question on the forum, users are kindly requested to follow these steps:
    1. Check that the same issue has not already been addressed in the forum - there is a search tool.
    2. Ensure that the correct syntax has been used. For any function, detailed instructions are available directly in Apollo, e.g. by using ?apollo_mnl for apollo_mnl
    3. Check the frequently asked questions section on the Apollo website, which discusses some common issues/failures. Please see http://www.apollochoicemodelling.com/faq.html
    4. Make sure that R is using the latest official release of Apollo.
  4. If the above steps do not resolve the issue, then users should follow these steps when posting a question:
    1. provide full details on the issue, including the entire code and output, including any error messages
    2. posts will not immediately appear on the forum, but will be checked by a moderator first. This may take a day or two at busy times. There is no need to submit the post multiple times.

Input validation code works on other PC, not mine

Ask questions about errors you encouunter. Please make sure to include full details about your model specifications, and ideally your model file.
klim
Posts: 4
Joined: 27 May 2021, 15:25

Re: Input validation code works on other PC, not mine

Post by klim »

Another thing for us who switch from Stata, importing an dta file will result in "Error in rle(id) : 'x' must be a vector of an atomic type". Try xlsx instead.
Last edited by klim on 10 Jun 2021, 19:23, edited 1 time in total.
LauraThoWal
Posts: 8
Joined: 19 Apr 2021, 17:41

Re: Input validation code works on other PC, not mine

Post by LauraThoWal »

Thanks David!

Storing as a dataframe rather than tibble sorted out the issue.

I am a little confused about explanators vs utilities in Apollo though. My understanding is that explanators are socio-demographic attributes of participants, and the utilities are characteristics of the choices/tasks. However, I'm unclear on how to get the estimates for explanators in the final model.

In addition, I'm having one other problem. We conducted multiple games (ie tasks) with different groups of people, and the different games had different treatments (ie reward systems). I want to incorporate GameID and Treatment as predictors in the model, so defined them in V with dummy variables as you explained in your reply above.

Code: Select all

V[['farm']] = b_GameID_2ASH*(GameID=="2ASH") + b_GameID_3ASH*(GameID=="3ASH") + ... etc
However when I run the model I get this error:
Error in apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, :
Parameter b_GameID_2ASH does not influence the log-likelihood of your model!
(and if I remove the GameID parameter I get the same error with the Treatment parameter). I think it is because I've included these parameters in all four utility functions.

If I define them as explanators instead, the model runs but s.e. etc are all NA. I have included model output below.

Code: Select all

Testing likelihood function...
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                  48663.00 48663.00 48663.00 48663.00
Times chosen                     23265.00 13094.00  5654.00  6650.00
Percentage chosen overall           47.81    26.91    11.62    13.67
Percentage chosen when available    47.81    26.91    11.62    13.67

Pre-processing likelihood function...
Preparing pre-processing report

Testing influence of parameters.......
Starting main estimation
Initial function value: -67461.24 
Initial gradient value:
       asc_farm       asc_scare        asc_kill    asc_conserve  b_Animal_count      b_Cons_lag 
       11099.25          928.25        -6511.75        -5515.75       -12027.50        -5515.75 
b_Kill_lag_mean 
       -6511.75 
initial  value 67461.242495 
iter   2 value 62894.525534
iter   3 value 62841.361744
iter   4 value 62828.621744
iter   5 value 59879.614667
iter   6 value 59773.873573
iter   7 value 59764.924476
iter   8 value 59764.556045
iter   9 value 59764.249285
iter   9 value 59764.249099
iter   9 value 59764.249099
final  value 59764.249099 
converged
Estimated parameters:
                   Estimate
asc_farm            0.49661
asc_scare          -0.07818
asc_kill           -0.24978
asc_conserve       -0.16865
b_Animal_count     -0.41843
b_Cons_lag         -0.16865
b_Kill_lag_mean    -0.24978

Computing covariance matrix using analytical gradient.
 0%....25%....50%....75%....100%
ERROR: Singular Hessian, cannot calculate s.e.
Hessian written to Apollo_synthesis_hessian.csv
WARNING: Some eigenvalues of the Hessian are positive, indicating convergence to a saddle
  point!
Computing score matrix...
Calculating LL(0) for applicable models...
Calculating LL of each model component...
> #### MODEL OUTPUTS   
> apollo_modelOutput(model)

Model name                       : Apollo_synthesis
Model description                : Analysis game synthesis with Apollo
Model run at                     : 2021-06-10 16:04:42
Estimation method                : bfgs
Model diagnosis                  : successful convergence 
Number of individuals            : 226
Number of rows in database       : 48663
Number of modelled outcomes      : 48663

Number of cores used             :  1 
Model without mixing

LL(start)                        : -67461.24
LL(0)                            : -67461.24
LL(final)                        : -59764.25
Rho-square (0)                   :  0.1141 
Adj.Rho-square (0)               :  0.114 
AIC                              :  119542.5 
BIC                              :  119604.1 


Estimated parameters             :  7
Time taken (hh:mm:ss)            :  00:00:8.39 
     pre-estimation              :  00:00:2.05 
     estimation                  :  00:00:2.2 
     post-estimation             :  00:00:4.14 
Iterations                       :  11  
Min abs eigenvalue of Hessian    :  3e-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.49661          NA          NA          NA            NA
asc_scare          -0.07818          NA          NA          NA            NA
asc_kill           -0.24978          NA          NA          NA            NA
asc_conserve       -0.16865          NA          NA          NA            NA
b_Animal_count     -0.41843          NA          NA          NA            NA
b_Cons_lag         -0.16865          NA          NA          NA            NA
b_Kill_lag_mean    -0.24978          NA          NA          NA            NA
 
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Input validation code works on other PC, not mine

Post by dpalma »

Hi Laura,

Yes, including the same explanatory variable with the same value and coefficient in each utility will lead to the "Parameter b does not influence the log-likelihood of your model!", as it is impossible to identify.

I am not sure I understand what you mean by including a variable as an "explanator" or "inside V". All explanatory variables in an MNL model should go into the V, otherwise they are ignored by the model. Could you share code showing what you mean in each case?

Cheers
David
LauraThoWal
Posts: 8
Joined: 19 Apr 2021, 17:41

Re: Input validation code works on other PC, not mine

Post by LauraThoWal »

Hi David,

Thanks for the quick reply!

For some broader context, I want to know what variables influence the decision to farm, scare, kill, or conserve (my 4 V utilities). This includes personal attributes like gender and experimental conditions like treatment. These variables would of course be constant across the 4 options/V utilities, but I still need them in the final model.

Treatment as explanator: (haven't included GameID to save space, but it's treated the same as Treatments)

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  ="Apollo_synthesis",
  modelDescr ="Analysis game synthesis with Apollo",
  indivID    ="HHID1"
)

#### LOAD DATA AND APPLY ANY TRANSFORMATIONS 
database<-read_csv(here("Synthesis 3 data.csv"))
database <- as.data.frame(database)

#### ANALYSIS OF CHOICES   
choiceAnalysis_settings <- list(
  alternatives = c(farm=0, scare=1, kill=2, conserve=3),
  avail        = 1,
  choiceVar    = database$Choice,
  explanators  = database[,c("Gabon", "Age", "Female", "Education", "Farmer", 
                             "Baseline", "Subsidy", "Deterrents", "Agglomeration",
                             "CropDam", "meanequity", "meantrust", "meanfam", "Round")]
)

apollo_choiceAnalysis(choiceAnalysis_settings, apollo_control, database)

output=apollo_choiceAnalysis(choiceAnalysis_settings,apollo_control,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_Animal_count = 0,
              b_Cons_lag     = 0,
              b_Kill_lag_mean    = 0)

apollo_beta_fixed = c() # no parameters fixed at starting value

apollo_fixed = c()

#### 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
  V = list()
  V[['farm']] = asc_farm 
  V[['scare']] = asc_scare
  V[['kill']] = asc_kill + b_Kill_lag_mean + b_Animal_count  
  V[['conserve']] = asc_conserve + b_Cons_lag + b_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)


#### MODEL OUTPUTS   
apollo_modelOutput(model)


GameID/Treatments as V utilities

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  ="Apollo_synthesis",
  modelDescr ="Analysis game synthesis with Apollo",
  indivID    ="HHID1"
)


#### LOAD DATA AND APPLY ANY TRANSFORMATIONS 
database<-read_csv(here("Synthesis 3 data.csv"))
database <- as.data.frame(database)

#### ANALYSIS OF CHOICES   
choiceAnalysis_settings <- list(
  alternatives = c(farm=0, scare=1, kill=2, conserve=3),
  avail        = 1,
  choiceVar    = database$Choice,
  explanators  = database[,c("Gabon", "Age", "Female", "Education", "Farmer", 
                             "CropDam", "meanequity", "meantrust", "meanfam", "Round")]
)

apollo_choiceAnalysis(choiceAnalysis_settings, apollo_control, database)

output=apollo_choiceAnalysis(choiceAnalysis_settings,apollo_control,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_Animal_count = 0,
              b_Round    = 0,
              b_Cons_lag     = 0,
              b_Kill_lag_mean    = 0,
              b_GameID_2ASH = 0, 
              b_GameID_3ASH = 0, 
              b_GameID_4ASH = 0, 
              b_GameID_1BON = 0, 
              b_GameID_2BON = 0, 
              b_GameID_3BON = 0, 
              b_GameID_4BON = 0, 
              b_GameID_5BON = 0, 
              b_GameID_6BON = 0, 
              b_GameID_7BON = 0, 
              b_GameID_8BON = 0, 
              b_GameID_9BON = 0, 
              b_GameID_1BOU = 0, 
              b_GameID_1CAS = 0, 
              b_GameID_2CAS = 0, 
              b_GameID_1EDA = 0, 
              b_GameID_1HOY = 0, 
              b_GameID_2HOY = 0, 
              b_GameID_JUUN01 = 0, 
              b_GameID_1LOL = 0, 
              b_GameID_2LOL = 0, 
              b_GameID_1LOP = 0, 
              b_GameID_6LOP = 0, 
              b_GameID_11LOP = 0, 
              b_GameID_12LOP = 0, 
              b_GameID_13LOP = 0, 
              b_GameID_14LOP = 0, 
              b_GameID_15LOP = 0, 
              b_GameID_2MAL = 0, 
              b_GameID_3MAL = 0, 
              b_GameID_4MAL = 0, 
              b_GameID_5MAL = 0, 
              b_GameID_6MAL = 0, 
              b_GameID_7MAL = 0, 
              b_GameID_8MAL = 0, 
              b_GameID_9MAL = 0, 
              b_GameID_1MAN = 0, 
              b_GameID_2MAN = 0, 
              b_GameID_1MIK = 0, 
              b_GameID_2MIK = 0, 
              b_GameID_1MOU = 0, 
              b_GameID_7ORK = 0, 
              b_GameID_8ORK = 0, 
              b_GameID_9ORK = 0, 
              b_GameID_10ORK = 0, 
              b_GameID_11ORK = 0, 
              b_GameID_1OTO = 0, 
              b_GameID_2OTO = 0, 
              b_GameID_5OTO = 0, 
              b_GameID_1ROU = 0, 
              b_GameID_1SAN = 0, 
              b_GameID_2SAN = 0, 
              b_GameID_1SHA = 0, 
              b_GameID_1WAG = 0, 
              b_GameID_1WES = 0, 
              b_GameID_2WES = 0, 
              b_GameID_3WES = 0, 
              b_GameID_1WOU = 0, 
              b_GameID_2WOU = 0, 
              b_GameID_1YOU = 0, 
              b_Treatments_Subsidy = 0, 
              b_Treatments_Deterrents = 0, 
              b_Treatments_Agglomeration = 0,
              b_GameID_1ASH = 0,
              b_Treatments_Baseline = 0)

apollo_beta_fixed = c() # no parameters fixed at starting value

apollo_fixed = c("b_GameID_1ASH", "b_Treatments_Baseline")

#### 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
  V = list()
  V[['farm']] = asc_farm + b_Round + b_GameID_2ASH*(GameID=="2ASH") + b_GameID_3ASH*(GameID=="3ASH") + b_GameID_4ASH*(GameID=="4ASH") + b_GameID_1BON*(GameID=="1BON") + b_GameID_2BON*(GameID=="2BON") + b_GameID_3BON*(GameID=="3BON") + b_GameID_4BON*(GameID=="4BON") + b_GameID_5BON*(GameID=="5BON") + b_GameID_6BON*(GameID=="6BON") + b_GameID_7BON*(GameID=="7BON") + b_GameID_8BON*(GameID=="8BON") + b_GameID_9BON*(GameID=="9BON") + b_GameID_1BOU*(GameID=="1BOU") + b_GameID_1CAS*(GameID=="1CAS") + b_GameID_2CAS*(GameID=="2CAS") + b_GameID_1EDA*(GameID=="1EDA") + b_GameID_1HOY*(GameID=="1HOY") + b_GameID_2HOY*(GameID=="2HOY") + b_GameID_JUUN01*(GameID=="JUUN01") + b_GameID_1LOL*(GameID=="1LOL") + b_GameID_2LOL*(GameID=="2LOL") + b_GameID_1LOP*(GameID=="1LOP") + b_GameID_6LOP*(GameID=="6LOP") + b_GameID_11LOP*(GameID=="11LOP") + b_GameID_12LOP*(GameID=="12LOP") + b_GameID_13LOP*(GameID=="13LOP") + b_GameID_14LOP*(GameID=="14LOP") + b_GameID_15LOP*(GameID=="15LOP") + b_GameID_2MAL*(GameID=="2MAL") + b_GameID_3MAL*(GameID=="3MAL") + b_GameID_4MAL*(GameID=="4MAL") + b_GameID_5MAL*(GameID=="5MAL") + b_GameID_6MAL*(GameID=="6MAL") + b_GameID_7MAL*(GameID=="7MAL") + b_GameID_8MAL*(GameID=="8MAL") + b_GameID_9MAL*(GameID=="9MAL") + b_GameID_1MAN*(GameID=="1MAN") + b_GameID_2MAN*(GameID=="2MAN") + b_GameID_1MIK*(GameID=="1MIK") + b_GameID_2MIK*(GameID=="2MIK") + b_GameID_1MOU*(GameID=="1MOU") + b_GameID_7ORK*(GameID=="7ORK") + b_GameID_8ORK*(GameID=="8ORK") + b_GameID_9ORK*(GameID=="9ORK") + b_GameID_10ORK*(GameID=="10ORK") + b_GameID_11ORK*(GameID=="11ORK") + b_GameID_1OTO*(GameID=="1OTO") + b_GameID_2OTO*(GameID=="2OTO") + b_GameID_5OTO*(GameID=="5OTO") + b_GameID_1ROU*(GameID=="1ROU") + b_GameID_1SAN*(GameID=="1SAN") + b_GameID_2SAN*(GameID=="2SAN") + b_GameID_1SHA*(GameID=="1SHA") + b_GameID_1WAG*(GameID=="1WAG") + b_GameID_1WES*(GameID=="1WES") + b_GameID_2WES*(GameID=="2WES") + b_GameID_3WES*(GameID=="3WES") + b_GameID_1WOU*(GameID=="1WOU") + b_GameID_2WOU*(GameID=="2WOU") + b_GameID_1YOU*(GameID=="1YOU") + b_Treatments_Subsidy*(Treatments=="Subsidy")  + b_Treatments_Deterrents*(Treatments=="Deterrents") + b_Treatments_Agglomeration*(Treatments=="Agglomeration") 
  V[['scare']] = asc_scare + b_Round + b_GameID_2ASH*(GameID=="2ASH") + b_GameID_3ASH*(GameID=="3ASH") + b_GameID_4ASH*(GameID=="4ASH") + b_GameID_1BON*(GameID=="1BON") + b_GameID_2BON*(GameID=="2BON") + b_GameID_3BON*(GameID=="3BON") + b_GameID_4BON*(GameID=="4BON") + b_GameID_5BON*(GameID=="5BON") + b_GameID_6BON*(GameID=="6BON") + b_GameID_7BON*(GameID=="7BON") + b_GameID_8BON*(GameID=="8BON") + b_GameID_9BON*(GameID=="9BON") + b_GameID_1BOU*(GameID=="1BOU") + b_GameID_1CAS*(GameID=="1CAS") + b_GameID_2CAS*(GameID=="2CAS") + b_GameID_1EDA*(GameID=="1EDA") + b_GameID_1HOY*(GameID=="1HOY") + b_GameID_2HOY*(GameID=="2HOY") + b_GameID_JUUN01*(GameID=="JUUN01") + b_GameID_1LOL*(GameID=="1LOL") + b_GameID_2LOL*(GameID=="2LOL") + b_GameID_1LOP*(GameID=="1LOP") + b_GameID_6LOP*(GameID=="6LOP") + b_GameID_11LOP*(GameID=="11LOP") + b_GameID_12LOP*(GameID=="12LOP") + b_GameID_13LOP*(GameID=="13LOP") + b_GameID_14LOP*(GameID=="14LOP") + b_GameID_15LOP*(GameID=="15LOP") + b_GameID_2MAL*(GameID=="2MAL") + b_GameID_3MAL*(GameID=="3MAL") + b_GameID_4MAL*(GameID=="4MAL") + b_GameID_5MAL*(GameID=="5MAL") + b_GameID_6MAL*(GameID=="6MAL") + b_GameID_7MAL*(GameID=="7MAL") + b_GameID_8MAL*(GameID=="8MAL") + b_GameID_9MAL*(GameID=="9MAL") + b_GameID_1MAN*(GameID=="1MAN") + b_GameID_2MAN*(GameID=="2MAN") + b_GameID_1MIK*(GameID=="1MIK") + b_GameID_2MIK*(GameID=="2MIK") + b_GameID_1MOU*(GameID=="1MOU") + b_GameID_7ORK*(GameID=="7ORK") + b_GameID_8ORK*(GameID=="8ORK") + b_GameID_9ORK*(GameID=="9ORK") + b_GameID_10ORK*(GameID=="10ORK") + b_GameID_11ORK*(GameID=="11ORK") + b_GameID_1OTO*(GameID=="1OTO") + b_GameID_2OTO*(GameID=="2OTO") + b_GameID_5OTO*(GameID=="5OTO") + b_GameID_1ROU*(GameID=="1ROU") + b_GameID_1SAN*(GameID=="1SAN") + b_GameID_2SAN*(GameID=="2SAN") + b_GameID_1SHA*(GameID=="1SHA") + b_GameID_1WAG*(GameID=="1WAG") + b_GameID_1WES*(GameID=="1WES") + b_GameID_2WES*(GameID=="2WES") + b_GameID_3WES*(GameID=="3WES") + b_GameID_1WOU*(GameID=="1WOU") + b_GameID_2WOU*(GameID=="2WOU") + b_GameID_1YOU*(GameID=="1YOU") + b_Treatments_Subsidy*(Treatments=="Subsidy")  + b_Treatments_Deterrents*(Treatments=="Deterrents") + b_Treatments_Agglomeration*(Treatments=="Agglomeration") 
  V[['kill']] = asc_kill + b_Kill_lag_mean + b_GameID_2ASH*(GameID=="2ASH") + b_GameID_3ASH*(GameID=="3ASH") + b_GameID_4ASH*(GameID=="4ASH") + b_GameID_1BON*(GameID=="1BON") + b_GameID_2BON*(GameID=="2BON") + b_GameID_3BON*(GameID=="3BON") + b_GameID_4BON*(GameID=="4BON") + b_GameID_5BON*(GameID=="5BON") + b_GameID_6BON*(GameID=="6BON") + b_GameID_7BON*(GameID=="7BON") + b_GameID_8BON*(GameID=="8BON") + b_GameID_9BON*(GameID=="9BON") + b_GameID_1BOU*(GameID=="1BOU") + b_GameID_1CAS*(GameID=="1CAS") + b_GameID_2CAS*(GameID=="2CAS") + b_GameID_1EDA*(GameID=="1EDA") + b_GameID_1HOY*(GameID=="1HOY") + b_GameID_2HOY*(GameID=="2HOY") + b_GameID_JUUN01*(GameID=="JUUN01") + b_GameID_1LOL*(GameID=="1LOL") + b_GameID_2LOL*(GameID=="2LOL") + b_GameID_1LOP*(GameID=="1LOP") + b_GameID_6LOP*(GameID=="6LOP") + b_GameID_11LOP*(GameID=="11LOP") + b_GameID_12LOP*(GameID=="12LOP") + b_GameID_13LOP*(GameID=="13LOP") + b_GameID_14LOP*(GameID=="14LOP") + b_GameID_15LOP*(GameID=="15LOP") + b_GameID_2MAL*(GameID=="2MAL") + b_GameID_3MAL*(GameID=="3MAL") + b_GameID_4MAL*(GameID=="4MAL") + b_GameID_5MAL*(GameID=="5MAL") + b_GameID_6MAL*(GameID=="6MAL") + b_GameID_7MAL*(GameID=="7MAL") + b_GameID_8MAL*(GameID=="8MAL") + b_GameID_9MAL*(GameID=="9MAL") + b_GameID_1MAN*(GameID=="1MAN") + b_GameID_2MAN*(GameID=="2MAN") + b_GameID_1MIK*(GameID=="1MIK") + b_GameID_2MIK*(GameID=="2MIK") + b_GameID_1MOU*(GameID=="1MOU") + b_GameID_7ORK*(GameID=="7ORK") + b_GameID_8ORK*(GameID=="8ORK") + b_GameID_9ORK*(GameID=="9ORK") + b_GameID_10ORK*(GameID=="10ORK") + b_GameID_11ORK*(GameID=="11ORK") + b_GameID_1OTO*(GameID=="1OTO") + b_GameID_2OTO*(GameID=="2OTO") + b_GameID_5OTO*(GameID=="5OTO") + b_GameID_1ROU*(GameID=="1ROU") + b_GameID_1SAN*(GameID=="1SAN") + b_GameID_2SAN*(GameID=="2SAN") + b_GameID_1SHA*(GameID=="1SHA") + b_GameID_1WAG*(GameID=="1WAG") + b_GameID_1WES*(GameID=="1WES") + b_GameID_2WES*(GameID=="2WES") + b_GameID_3WES*(GameID=="3WES") + b_GameID_1WOU*(GameID=="1WOU") + b_GameID_2WOU*(GameID=="2WOU") + b_GameID_1YOU*(GameID=="1YOU") + b_Treatments_Subsidy*(Treatments=="Subsidy")  + b_Treatments_Deterrents*(Treatments=="Deterrents") + b_Treatments_Agglomeration*(Treatments=="Agglomeration") 
  V[['conserve']] = asc_conserve + b_Cons_lag + b_Animal_count + b_GameID_2ASH*(GameID=="2ASH") + b_GameID_3ASH*(GameID=="3ASH") + b_GameID_4ASH*(GameID=="4ASH") + b_GameID_1BON*(GameID=="1BON") + b_GameID_2BON*(GameID=="2BON") + b_GameID_3BON*(GameID=="3BON") + b_GameID_4BON*(GameID=="4BON") + b_GameID_5BON*(GameID=="5BON") + b_GameID_6BON*(GameID=="6BON") + b_GameID_7BON*(GameID=="7BON") + b_GameID_8BON*(GameID=="8BON") + b_GameID_9BON*(GameID=="9BON") + b_GameID_1BOU*(GameID=="1BOU") + b_GameID_1CAS*(GameID=="1CAS") + b_GameID_2CAS*(GameID=="2CAS") + b_GameID_1EDA*(GameID=="1EDA") + b_GameID_1HOY*(GameID=="1HOY") + b_GameID_2HOY*(GameID=="2HOY") + b_GameID_JUUN01*(GameID=="JUUN01") + b_GameID_1LOL*(GameID=="1LOL") + b_GameID_2LOL*(GameID=="2LOL") + b_GameID_1LOP*(GameID=="1LOP") + b_GameID_6LOP*(GameID=="6LOP") + b_GameID_11LOP*(GameID=="11LOP") + b_GameID_12LOP*(GameID=="12LOP") + b_GameID_13LOP*(GameID=="13LOP") + b_GameID_14LOP*(GameID=="14LOP") + b_GameID_15LOP*(GameID=="15LOP") + b_GameID_2MAL*(GameID=="2MAL") + b_GameID_3MAL*(GameID=="3MAL") + b_GameID_4MAL*(GameID=="4MAL") + b_GameID_5MAL*(GameID=="5MAL") + b_GameID_6MAL*(GameID=="6MAL") + b_GameID_7MAL*(GameID=="7MAL") + b_GameID_8MAL*(GameID=="8MAL") + b_GameID_9MAL*(GameID=="9MAL") + b_GameID_1MAN*(GameID=="1MAN") + b_GameID_2MAN*(GameID=="2MAN") + b_GameID_1MIK*(GameID=="1MIK") + b_GameID_2MIK*(GameID=="2MIK") + b_GameID_1MOU*(GameID=="1MOU") + b_GameID_7ORK*(GameID=="7ORK") + b_GameID_8ORK*(GameID=="8ORK") + b_GameID_9ORK*(GameID=="9ORK") + b_GameID_10ORK*(GameID=="10ORK") + b_GameID_11ORK*(GameID=="11ORK") + b_GameID_1OTO*(GameID=="1OTO") + b_GameID_2OTO*(GameID=="2OTO") + b_GameID_5OTO*(GameID=="5OTO") + b_GameID_1ROU*(GameID=="1ROU") + b_GameID_1SAN*(GameID=="1SAN") + b_GameID_2SAN*(GameID=="2SAN") + b_GameID_1SHA*(GameID=="1SHA") + b_GameID_1WAG*(GameID=="1WAG") + b_GameID_1WES*(GameID=="1WES") + b_GameID_2WES*(GameID=="2WES") + b_GameID_3WES*(GameID=="3WES") + b_GameID_1WOU*(GameID=="1WOU") + b_GameID_2WOU*(GameID=="2WOU") + b_GameID_1YOU*(GameID=="1YOU") + b_Treatments_Subsidy*(Treatments=="Subsidy")  + b_Treatments_Deterrents*(Treatments=="Deterrents") + b_Treatments_Agglomeration*(Treatments=="Agglomeration") 
  
  
  ### 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)


#### MODEL OUTPUTS   
apollo_modelOutput(model)
dpalma
Posts: 190
Joined: 24 Apr 2020, 17:54

Re: Input validation code works on other PC, not mine

Post by dpalma »

Hi Laura,

When using the same explanatory variables for all alternatives, what you usually do is fixing the utility of one alternative to zero, and include all explanatory variable in all other utilities, but using different coefficients. So imagine you want to explain the decision to work full time, part time, or not at all based on people's number of years of education (variable educ). You would write the utility functions as follow:

Code: Select all

V[["noWork"]]   = 0
V[["partTime"]] = asc_partTime + bEduc_partTime*educ
V[["fullTime"]] = asc_fullTime + bEduc_fullTime*educ
If after estimation you get that bEduc_fullTime > bEduc_partTime, it means that people with higher education are more likely to work, and for more hours.

I tried to adapt your code to follow this framework. I didn't set one alternative's utility to zero, but instead fixed all its parameters to zero (farming). I didn't include all explanatory variables, as it is only an example. I hope it helps you get the idea.

I also removed the "GameID" effects, as I believe they are only there to control for possible biases in each choice scenario, and they do not contain information about the actual attributes in each scenario. If they are only controls for possible biases, then I would start by estimating models that ignore their effect, and only include them later on when you are confident your model is running as expected. On the other hand, if "GameID" does contain relevant information about each scenario's attributes, then I would include them right away.

Below is the 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  ="sameAttribAllAlts",
  modelDescr ="Analysis game synthesis with Apollo",
  indivID    ="HHID1"
)


#### LOAD DATA AND APPLY ANY TRANSFORMATIONS 
#database<-read_csv(here("Synthesis 3 data.csv"))
database <- read.csv("Synthesis 2 data.csv")
database <- database[is.finite(database$Kill_lag) & is.finite(database$Cons_lag),] # remove obs with NA
database <- as.data.frame(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_Female_farm    = 0, 
              b_Female_scare   = 0, 
              b_Female_kill    = 0, 
              b_Female_conserve= 0, 
              
              b_Education_farm    = 0, 
              b_Education_scare   = 0, 
              b_Education_kill    = 0, 
              b_Education_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_mean  = 0)

apollo_beta_fixed = c() # no parameters fixed at starting value

apollo_fixed = c('asc_farm', 'b_Age_farm', 'b_Female_farm', 'b_Education_farm',
                 'b_Treatments_Subsidy_farm', 'b_Treatments_Deterrents_farm', 
                 'b_Treatments_Agglomeration_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
  TS <- (Treatments=="Subsidy"      )
  TD <- (Treatments=="Deterrents"   )
  TA <- (Treatments=="Agglomeration")
  V = list()
  V[['farm']]     = asc_farm     + b_Age_farm    *Age + b_Female_farm    *Female + b_Education_farm    *Education + b_Treatments_Subsidy_farm    *TS  + b_Treatments_Deterrents_farm    *TD + b_Treatments_Agglomeration_farm    *TA
  V[['scare']]    = asc_scare    + b_Age_scare   *Age + b_Female_scare   *Female + b_Education_scare   *Education + b_Treatments_Subsidy_scare   *TS  + b_Treatments_Deterrents_scare   *TD + b_Treatments_Agglomeration_scare   *TA
  V[['kill']]     = asc_kill     + b_Age_kill    *Age + b_Female_kill    *Female + b_Education_kill    *Education + b_Treatments_Subsidy_kill    *TS  + b_Treatments_Deterrents_kill    *TD + b_Treatments_Agglomeration_kill    *TA + b_Kill_lag_mean*Kill_lag
  V[['conserve']] = asc_conserve + b_Age_conserve*Age + b_Female_conserve*Female + b_Education_conserve*Education + 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)

Cheers
David
LauraThoWal
Posts: 8
Joined: 19 Apr 2021, 17:41

Re: Input validation code works on other PC, not mine

Post by LauraThoWal »

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)
stephanehess
Site Admin
Posts: 977
Joined: 24 Apr 2020, 16:29

Re: Input validation code works on other PC, not mine

Post by stephanehess »

Hi Laura

there are clearly some issues here if you look at the standard errors for the constants.

Can I suggest that you gradually build up your model and see where it goes wrong. So what happens with ascs only, for example?

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply