Page 1 of 1

Error in apollo_validate when using MDCEV model

Posted: 18 Feb 2021, 15:50
by LydiaYang
Hello,

I encountered an error from Apollo when using MDCEV model when Testing likelihood function.
The error goes like this:
"Error in apollo_validate(mdcev_settings, modelType, functionality, apollo_inputs) :
Expenditure for some observations for model component "model" is either less or more than budget!"

The budget in my data is set as "salary", where salary = saving + inv + expenditure, such that there is no outside good and expenditure is set as "other" that will always be chosen.
The dataset is based on household finance data and there is no real data of salary, "expenditure" is also calculated via total annual household expenditure: expenditure = total_expenditure/12.

I was wondering is it possible that the issue is actually caused by my "fake data"? Or is there any other reason that might lead to this error?

Thank you for your time in advance!

The original code is as follows:

Code: Select all

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta = c(alpha_base         = 0,
                gamma_saving       = 1,
                gamma_investment   = 1,
                gamma_other        = 1,
                delta_saving       = 0,
                delta_investment   = 0,
                delta_other        = 0,
                sigma              = 1)

### 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("delta_saving", "sigma")

# ################################################################# #
#### 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()
  
  ### Define individual alternatives
  alternatives = c("saving", 
                   "investment",
                   "other")
  
  ### Define availabilities
  avail = list(saving     = 1,
               investment = 1,
               other      = 1)
  
  ### Define continuous consumption for individual alternatives
  continuousChoice = list(saving      = saving, 
                          investment  = inv,
                          other       = expenditure)
  
  ### Define utilities for individual alternatives
  V = list()
  V[["saving" ]]    = delta_saving 
  V[["investment"]] = delta_investment    
  V[["other"]]      = delta_other
  
  ### Define alpha parameters
  alpha = list(saving     = 1 /(1 + exp(-alpha_base)), 
               investment = 1 /(1 + exp(-alpha_base)), 
               other      = 1 /(1 + exp(-alpha_base)))
  
  ### Define gamma parameters
  gamma = list(saving      = gamma_saving, 
               investment  = gamma_investment,
               other       = gamma_other)
  
  ### Define costs for individual alternatives
  cost = list(saving      = 1, 
              investment  = 1,
              other       = 1)
  
  ### Define budget
  budget = salary
  
  ### Define settings for MDCEV model
  mdcev_settings <- list(alternatives      = alternatives,
                         avail             = avail,
                         continuousChoice  = continuousChoice,
                         V                 = V,
                         alpha             = alpha,
                         gamma             = gamma, 
                         sigma             = sigma, 
                         cost              = cost,
                         budget            = budget)
  
  ### Compute probabilities using MDCEV model
  P[["model"]] = apollo_mdcev(mdcev_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)
}

Re: Error in apollo_validate when using MDCEV model

Posted: 22 Feb 2021, 12:46
by dpalma
Hi Lydia,

Could you please post some tabulation of your data, particularly of variables saving, inv, expenditure, and salary. Something like the output of:

Code: Select all

summary(database[,c('saving', 'inv', 'expenditure', 'salary')]
summary( rowSums(database[,c('saving', 'inv', 'expenditure')) - database$salary )
Thanks
David

Re: Error in apollo_validate when using MDCEV model

Posted: 22 Feb 2021, 13:08
by LydiaYang
Hi David,

Thank you for your reply, the summary statistics are as follows:

Code: Select all

> summary(database[,c('saving', 'inv', 'expenditure', 'salary')])
     saving              inv           expenditure         salary       
 Min.   :   0.000   Min.   :   0.00   Min.   :  30.7   Min.   :   30.7  
 1st Qu.:   0.000   1st Qu.:   0.00   1st Qu.: 997.6   1st Qu.: 1092.3  
 Median :   2.083   Median :   0.00   Median :1787.5   Median : 1992.1  
 Mean   : 275.518   Mean   :  40.79   Mean   :2234.5   Mean   : 2550.8  
 3rd Qu.: 208.333   3rd Qu.:   0.00   3rd Qu.:2963.7   3rd Qu.: 3422.2  
 Max.   :2083.333   Max.   :8333.33   Max.   :6605.1   Max.   :17021.8
 
 summary( rowSums(database[,c('saving', 'inv', 'expenditure')]) - database$salary )
      Min.    1st Qu.     Median       Mean    3rd Qu.       Max. 
-8.545e-04  0.000e+00  0.000e+00 -3.958e-07  0.000e+00  7.324e-04
Besides, I also tried to simulate a set of new saving and investment data based on real monthly income of household data, by

1. Randomly generate saving_dummy and inv_dummy, where 1 represents the individual holds saving/investment, 0 not.
2. Generate saving_rate and inv_rate based on a uniform distribution U(0.01~0.08).
3. Generate "saving" = income * saving_rate * saving_dummy and "investment" = income * inv_rate * inv_dummy
4. Generate "other (expenditure)" = income - saving - investment.

which basically indicates that saving + investment + other = monthly income.
I have tried MDCEV with outside good model where "other (expenditure)" is taken as the outside good, but I have encoutered the same issue.
In Apollo example 11 and 12 that are based on ApollotimeUsedata, I thought all the alternatives (plus outside good) should equal to the total budget of time. So I was wondering where I could possiblely go wrong?

Thank you for your time! I am looking forward to your insight.

Best,
Lydia

Re: Error in apollo_validate when using MDCEV model

Posted: 22 Feb 2021, 16:52
by dpalma
Hi Lydia,

Apollo is quite strict in expenditure being equal to the budget. When calculating the budget and each alternative consumption, there are often rounding errors leading to expenditure being similar but not exactly equal to the budget. Your case seems to be one of these cases.

I would recommend using the expenditure as the budget, to avoid any further issues. In other words:

Code: Select all

### Define settings for MDCEV model
  mdcev_settings <- list(alternatives      = alternatives,
                         avail             = avail,
                         continuousChoice  = continuousChoice,
                         V                 = V,
                         alpha             = alpha,
                         gamma             = gamma, 
                         sigma             = sigma, 
                         cost              = cost,
                         budget            = saving + inv + expenditure)
Where expenditure is the amount consumed of alternative "other".

The next version of Apollo will be a bit more lenient with this constraint.

Best
David

Re: Error in apollo_validate when using MDCEV model

Posted: 22 Feb 2021, 17:40
by LydiaYang
Hi David,

Received with many thanks! I try to rewrite the code as:

Code: Select all

### Define budget
  budget = saving + investment + other
  
  ### Define settings for MDCEV model
  mdcev_settings <- list(alternatives      = alternatives,
                         avail             = avail,
                         continuousChoice  = continuousChoice,
                         V                  = V,
                         alpha            = alpha,
                         gamma         = gamma, 
                         sigma           = sigma, 
                         cost              = cost,
                         budget          = saving + investment + other)
while another error showed up:
"Testing influence of parameters Error in apollo_prepareProb(P, apollo_inputs, functionality) :
Need to multiply observations for the same individual! (see ?apollo_panelProd)


I only have one observation for the same individual, so maybe I should change the setting somewhere?

Thank you again for your time.

Best,
Lydia

Re: Error in apollo_validate when using MDCEV model

Posted: 23 Feb 2021, 12:26
by dpalma
Hi Lydia,

The problem seems to be with importing data from a dta (stata) file. You are using the haven package to import the data, but this package has some incompatibilities with apollo. You can solve the issue by transforming the data into csv first (you can do that in stata or even in R), and then read the data from the csv file in Apollo.

As a general rule, I recommend always loading the data for Apollo from a CSV file, as importing from other formats (stata, spss, excel, etc.) usually leads to problems in R.

Best wishes
David

Re: Error in apollo_validate when using MDCEV model

Posted: 23 Feb 2021, 15:21
by LydiaYang
Hi David,

Thank you for the kind suggestion.

Also for my case where one individual has only one observation, this line of code has been left out.

Code: Select all

### Take product across observation for same individual
  P = apollo_panelProd(P, apollo_inputs, functionality)
Now the model works perfectly, really appreciate your help!

Best,
Lydia