Page 1 of 1

Error code with choiceAnalysis_settings

Posted: 19 Apr 2021, 17:49
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")]
  )

Re: Error code with choiceAnalysis_settings

Posted: 21 Apr 2021, 14:23
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

Re: Error code with choiceAnalysis_settings

Posted: 22 Apr 2021, 16:20
by LauraThoWal
Well I feel foolish, thank you so much!