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.

Error code with choiceAnalysis_settings

Ask questions about data format and processing of data, including the use of pre-estimation functions in Apollo. If your question relates to a specific error you are getting, please provide some of the output.
Post Reply
LauraThoWal
Posts: 8
Joined: 19 Apr 2021, 17:41

Error code with choiceAnalysis_settings

Post by LauraThoWal »

I'm analysing RP data from two case studies, but am getting an error when it comes to "choiceAnalysis_settings".

Code: Select all

#### ANALYSIS OF CHOICES   
choiceAnalysis_settings <- list(
  alternatives = c(farm=0, scare=1, kill=2, conserve=3),
  avail        = 1,
  choiceVar    = moddata3$Choice,
  explanators  = moddata3[,c("Gabon" + "Age" + "Female" + "Education" + "Farmer" + 
                             "CropDam" + "meanequity" + "meantrust" + "meanfam")]
  )
keeps giving:
Error in "Gabon" + "Age" : non-numeric argument to binary operator


The variables are all numeric, I've checked multiple times. I've also tried different (also numeric) variables, and still get the same error. Any insight would be appreciated!


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

#### LOAD DATA AND APPLY ANY TRANSFORMATIONS 
mydata<-read_csv(here("dataset_2pt1.csv"))

## modify dataset
moddata <- mydata %>% 
  select(Treatments, Country, GameID, Round, Age, Education, Trust_comm_1, Trust_comm_2, 
         Equity_1, Equity_2, Rel1, Rel2, Rel3, Kill, Cons, HHID,
         CC1, CC2, CC3, CC4, CC5, CC6, CC7, CC8, CC9,
         decision_time, Animal_count, Exp_crop_damage, Prime_Occ, Gender, Goal_games) %>% 
  mutate(Farmer = ifelse(Prime_Occ %in% 1, 1, 0)) %>% 
  filter(Treatments!="P") %>% 
  mutate(CropDam = ifelse(Country %in% "Scotland" & Exp_crop_damage %in% 0-30, "1",
                          ifelse(Country %in% "Scotland" & Exp_crop_damage %in% 31-60, "2",
                                 ifelse(Country %in% "Gabon" & Exp_crop_damage %in% 1, "1",
                                        ifelse(Country %in% "Gabon" & Exp_crop_damage %in% 2, "2",
                                               ifelse(Country %in% "Gabon" & Exp_crop_damage %in% 3, "3", "3")))))) %>% 
  mutate(Goal_games1 = ifelse(Goal_games %in% 1, "Win",
                          ifelse(Goal_games %in% 2, "Benefit group",
                                 ifelse(Goal_games %in% 3, "Real life",
                                        ifelse(Goal_games %in% 4, "Fun", "Other"))))) %>% 
  mutate(Treatments = ifelse(Treatments=="T2", "Baseline", 
                             ifelse(Treatments=="T4", "Deterrents", 
                                    ifelse(Treatments=="T3", "Subsidy", 
                                           ifelse(Treatments=="T5", "Agglomeration", "")))), 
         Treatments=factor(Treatments, levels=c("Baseline",
                                                "Deterrents",
                                                "Subsidy", 
                                                "Agglomeration"))) %>% 
  na.omit() %>% 
  mutate(GameID=factor(GameID),
         Country=factor(Country),
         Gender=factor(Gender),
         Goal_games=factor(Goal_games),
         CropDam=as.numeric(CropDam),
         meantrust = rowMeans(select(., Trust_comm_1, Trust_comm_2)),
         meanequity = rowMeans(select(., Equity_1, Equity_2)),
         meanfam = rowMeans(select(., Rel1, Rel2, Rel3)))   %>% 
  group_by(Treatments, GameID, Round) %>% 
  mutate(Kill_mean = mean(Kill),
         Kill_lag=lag(Kill_mean, 4, na.rm=TRUE),
         Cons_mean =mean(Cons),
         Cons_lag=lag(Cons, 4, na.rm=TRUE)) %>%
  ungroup() %>%
  mutate(Kill_lag=lag(Kill_mean, 4, na.rm=TRUE),
         Cons_lag=lag(Cons, 4, na.rm=TRUE))
  

### combine the cell choice columns into a single column with separate rows for cell
moddata2 <- pivot_longer(moddata, CC1:CC9, names_to = "CC", values_to = "Choice")

moddata3 <- moddata2 %>% 
  select(Treatments, Country, GameID, Round, Age, Education, Kill, Cons, HHID, Farmer,
         CC, Cons_mean, Cons_lag, Kill_lag, Kill_mean, meanequity, meanfam, meantrust, Choice,
         decision_time, Animal_count, CropDam, Prime_Occ, Gender, Goal_games) %>% 
  mutate(Gabon = ifelse(Country=="Gabon", "1", "0")) %>%
  mutate(Female = ifelse(Gender=="Female", "1", "0")) %>%
  mutate(CC=factor(CC),
         Gabon=as.numeric(Gabon),
         Female=as.numeric(Female),
         HHID=factor(HHID))

#### ANALYSIS OF CHOICES   
choiceAnalysis_settings <- list(
  alternatives = c(farm=0, scare=1, kill=2, conserve=3),
  avail        = 1,
  choiceVar    = moddata3$Choice,
  explanators  = moddata3[,c("Gabon" + "Age" + "Female" + "Education" + "Farmer" + 
                             "CropDam" + "meanequity" + "meantrust" + "meanfam")]
  )
stephanehess
Site Admin
Posts: 977
Joined: 24 Apr 2020, 16:29

Re: Error code with choiceAnalysis_settings

Post by stephanehess »

Hi

the reason you are getting the error is because you are adding string variables together.

I assume what you're wanting to do here is to use the 9 variables as explanators in the choiceAnalysis. In that case, they should be separated by commas, not +

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
LauraThoWal
Posts: 8
Joined: 19 Apr 2021, 17:41

Re: Error code with choiceAnalysis_settings

Post by LauraThoWal »

Well I feel foolish, thank you so much!
Post Reply