Page 1 of 1

Error in Bayesian estimation

Posted: 10 May 2020, 11:28
by rzanoli
Hello

I have been trying to use the features of Bayesian estimation in my model but I got an error message.

Therefore, I tried to run the Apollo_example_26.r file and incurred in the same error. All goes well until when trying to have the model estimated I get the message:

Estimate Model? (Y/N):
Error in colMeans(model$F) :
'x' must be an array of at least two dimensions

What is the problem?

Any help appreciated. I am using Apollo v. 0.1.0.

Thanks

Raffaele Zanoli

Below, the partial model output from Apollo_example_26.r:

Diagnostic checks passed. Please review before proceeding
-----------------------------------------------------------

Number of Individuals: 500
Number of Observations: 7000
Prior variance: 2
Target Acceptance (Fixed): 0.3
Target Acceptance (Normal): 0.3
Degrees of Freedom: 5
Avg. Number of Observations per Individual: 14
Initial Log-Likelihood: -19242.47502

-----------------------------------------------------------

Fixed Parameters Start
asc_bus_shift_female 0
asc_air_shift_female 0
asc_rail_shift_female 0
b_tt_shift_business 0
b_cost_shift_business 0
cost_income_elast 0

-----------------------------------------------------------

Random Parameters Start Dist.
asc_bus 0 N
asc_air 0 N
asc_rail 0 N
b_tt_car -3 LN-
b_tt_bus -3 LN-
b_tt_air -3 LN-
b_tt_rail -3 LN-
b_access -3 LN-
b_cost -3 LN-
b_wifi 0 CN+
b_food 0 CN+

-----------------------------------------------------------




Estimate Model? (Y/N):
Error in colMeans(model$F) :
'x' must be an array of at least two dimensions
> # ################################################################# #
> #### MODEL OUTPUTS ####
> # ################################################################# #
>
> # ----------------------------------------------------------------- #
> #---- FORMATTED OUTPUT (TO SCREEN) ----
> # ----------------------------------------------------------------- #
>
> apollo_modelOutput(model)
Error in apollo_modelOutput(model) : object 'model' not found

Re: Problem with Bayesian estimator

Posted: 10 May 2020, 12:18
by stephanehess
Hi Raffaele

is this simply a case of you trying to run the whole file in one go? Try running it only to the estimate line, then it will prompt you to confirm whether you want to proceed with estimation - see the Estimate Model? (Y/N) prompt in your output. If you were running the whole file in one go, it would have taken the first character in the next line as the response to this prompt.

Stephane

Error in Bayesian estimation

Posted: 17 May 2020, 21:00
by jfhawkin
I am playing with Bayesian estimation. I tried the example given on the Apollo website and receive the error:

Code: Select all

Error in colMeans(model$F) : 
  'x' must be an array of at least two dimensions

Re: Error in Bayesian estimation

Posted: 18 May 2020, 14:12
by stephanehess
Hi Jason

this is a common issue that happens to people when using Bayesian estimation through RSGHB. The issue is that you ran the whole file in one go. RSGHB, which is used by Apollo for Bayesian estimation, asks you to confirm before continuing the estimation. As you ran the whole file in one go, it took the next line as the answer. See the bit below.

Code: Select all

Estimate Model? (Y/N):
Error in colMeans(model$F) :
This is a useful feature to make sure that all your inputs are correct but can cause problems if you batch run files. You can turn that input request off by setting nodiagnostics=TRUE in apollo_HB

Best wishes

Stephane

Error in colMeans(model$F) when attempting HB estimation

Posted: 12 Aug 2020, 09:01
by DavideContu
Dear Stephane,

Trust you are well and many, many thanks for having made Apollo available, as well as this forum.

I am trying to estimate a latent class model with continues random parameters using Bayesian estimation.

Whilst the diagnostic test is passed,

Code: Select all

Diagnostic checks passed. Please review before proceeding
-----------------------------------------------------------
                                                         
                      Number of Individuals:          765
                     Number of Observations:         6120
                             Prior variance:            2
                  Target Acceptance (Fixed):          0.3
                 Target Acceptance (Normal):          0.3
                         Degrees of Freedom:            5
 Avg. Number of Observations per Individual:            8
                     Initial Log-Likelihood: -7930.051947

-----------------------------------------------------------

 Fixed Parameters Start
          delta_b     0
          delta_c     0
          delta_d     0
          delta_e     0
          delta_f     0
          delta_g     0
          delta_h     0

-----------------------------------------------------------

 Random Parameters Start Dist.
           asc_3_a   0.0     N
              di_a   0.5     N
             emi_a   0.5     N
              wa_a   0.5     N
              ho_a   0.5     N
              la_a   0.5     N
              bi_a   0.5     N
              ga_a   1.0     N

-----------------------------------------------------------

the following error is displayed:

Estimate Model? (Y/N):
Error in colMeans(model$F) :
'x' must be an array of at least two dimensions



Any suggestions are very welcome.

Thank you
Davide

PS: The code used:

Code: Select all

# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS                       ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)

set.seed(377)

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelName  ="Apollo_LC_MMNL8 HB",
  modelDescr ="LC RPL SAI HB",
  HB         = TRUE, 
  indivID    ="ID"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #

database = read.csv("italy_choice_f2.csv",header=TRUE)

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation


#a: full compensatory,asc,sai
#b: irrational
#c: only asc
#d: only sai
#e: asc and sai
#f: betas only
#g: betas and asc
#h: betas and sai

apollo_beta = c(asc_3_a               = 0,
                di_a                  = 0.5,
                emi_a                 = 0.5,
                wa_a                  = 0.5,
                ho_a                  = 0.5,
                la_a                  = 0.5,
                bi_a                  = 0.5,
                ga_a                  = 1,
                asc_3_b               = 0,
                di_b                  = 0,
                emi_b                 = 0,
                wa_b                  = 0,
                ho_b                  = 0,
                la_b                  = 0,
                bi_b                  = 0,
                ga_b                  = 0,
                delta_a               = 0,
                delta_b               = 0,
                delta_c               = 0,
                delta_d               = 0,
                delta_e               = 0,
                delta_f               = 0,
                delta_g               = 0,
                delta_h               = 0)

### 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("ga_b","asc_3_b","di_b","emi_b","wa_b","ho_b","la_b","bi_b","delta_a")

# ################################################################# #
#### HB settings                                                 ####
# ################################################################# #


apollo_HB = list(
  hbDist         = c(asc_3_a               = "N",
                     di_a                  = "N",
                     emi_a                 = "N",
                     wa_a                  = "N",
                     ho_a                  = "N",
                     la_a                  = "N",
                     bi_a                  = "N",
                     ga_a                  = "N",
                     asc_3_b               = "F",
                     di_b                  = "F",
                     emi_b                 = "F",
                     wa_b                  = "F",
                     ho_b                  = "F",
                     la_b                  = "F",
                     bi_b                  = "F",
                     ga_b                  = "F",
                     delta_a               = "F",
                     delta_b               = "F",
                     delta_c               = "F",
                     delta_d               = "F",
                     delta_e               = "F",
                     delta_f               = "F",
                     delta_g               = "F",
                     delta_h               = "F"
                     
                     ),
  gNCREP          = 100000, # burn-in iterations
  gNEREP          = 50000, # post burn-in iterations
  gINFOSKIP       = 500)

# ################################################################# #
#### GROUP AND VALIDATE INPUTS                                   ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
# ################################################################# #

# ################################################################# #
#### DEFINE RANDOM COMPONENTS                                    ####
# ################################################################# #

### Set parameters for generating draws
#apollo_draws = list(
#interDrawsType="mlhs",           
#interNDraws=100,                   
#interUnifDraws=c(),                
# interNormDraws=c("draws_ga","draws_asc3","draws_di","draws_wa","draws_emi","draws_la","draws_ho","draws_bi"),   
# intraDrawsType="mlhs",
# intraNDraws=0,        
# intraUnifDraws=c(),   
# intraNormDraws=c()    
#)

### Create random parameters
#apollo_randCoeff = function(apollo_beta, apollo_inputs){
# randcoeff = list()
  
 
  
# randcoeff[["asc_3_a"]] =asc_3_a_mu+asc_3_a_si*draws_asc3
# randcoeff[["di_a"]]=di_a_mu+di_a_si*draws_di
# randcoeff[["emi_a"]]=emi_a_mu+emi_a_si*draws_emi
# randcoeff[["wa_a"]]=wa_a_mu+wa_a_si*draws_wa
# randcoeff[["ho_a"]]=ho_a_mu+ho_a_si*draws_ho
# randcoeff[["la_a"]]=la_a_mu+la_a_si*draws_la
# randcoeff[["bi_a"]]=bi_a_mu+bi_a_si*draws_bi
# randcoeff[["ga_a"]]=ga_a_mu+ga_a_si*draws_ga
  


  
# return(randcoeff)
#}

# ################################################################# #
#### DEFINE LATENT CLASS COMPONENTS                              ####
# ################################################################# #

apollo_lcPars = function(apollo_beta, apollo_inputs){
  lcpars = list()
  
  lcpars[["asc_3"]]   = list(asc_3_a,asc_3_b,asc_3_a,asc_3_b,asc_3_a,asc_3_b,asc_3_a,asc_3_b )
  lcpars[["di"]]      = list(di_a,di_b,di_b,di_b,di_b,di_a,di_a,di_a)
  lcpars[["emi"]]     = list(emi_a,emi_b,emi_b,emi_b,emi_b,emi_a,emi_a,emi_a  )
  lcpars[["wa"]]      = list(wa_a,wa_b,wa_b,wa_b,wa_b,wa_a,wa_a,wa_a )
  lcpars[["ho"]]      = list(ho_a,ho_b,ho_b,ho_b,ho_b,ho_a ,ho_a,ho_a)
  lcpars[["la"]]      = list(la_a,la_b,la_b,la_b,la_b,la_a,la_a,la_a)
  lcpars[["bi"]]      = list(bi_a,bi_b,bi_b,bi_b,bi_b,bi_a,bi_a,bi_a  )
  lcpars[["ga"]]      = list(ga_a,ga_b,ga_b,ga_a,ga_a,ga_b,ga_b,ga_a )
  
  
  V=list()
  V[["class_a"]] = delta_a
  V[["class_b"]] = delta_b
  V[["class_c"]] = delta_c
  V[["class_d"]] = delta_d
  V[["class_e"]] = delta_e
  V[["class_f"]] = delta_f
  V[["class_g"]] = delta_g
  V[["class_h"]] = delta_h
  
  mnl_settings = list(
    alternatives = c(class_a=1, class_b=2, class_c=3, class_d=4, class_e=5, class_f=6, class_g=7, class_h=8), 
    avail        = 1, 
    choiceVar    = NA, 
    V            = V
  )
  lcpars[["pi_values"]] = apollo_mnl(mnl_settings, functionality="raw")
  
  lcpars[["pi_values"]] = apollo_firstRow(lcpars[["pi_values"]], apollo_inputs)
  
  return(lcpars)
}

# ################################################################# #
#### 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 settings for MNL model component that are generic across classes
  mnl_settings = list(
    alternatives = c(alt1=1, alt2=2,alt3=3),
    avail        = list(alt1=1, alt2=1,alt3=1),
    choiceVar    = choice
  )
  
  ### Loop over classes
  s=1
  while(s<=8){
    
    ### Compute class-specific utilities
    V=list()
    V[['alt1']]  = (di[[s]]*di_1 + emi[[s]]*emi_1 + wa[[s]]*wa_1 + ho[[s]]*ho_1+ la[[s]]*la_1+bi[[s]]*bill_m_1+ga[[s]]*s_1)
    V[['alt2']]  = (di[[s]]*di_2 + emi[[s]]*emi_2 + wa[[s]]*wa_2 + ho[[s]]*ho_2+ la[[s]]*la_2+bi[[s]]*bill_m_2+ga[[s]]*s_2)
    V[['alt3']]  = (asc_3[[s]]+di[[s]]*di_3 + emi[[s]]*emi_3 + wa[[s]]*wa_3 + ho[[s]]*ho_3+ la[[s]]*la_3+bi[[s]]*bill_m_3+ga[[s]]*s_3)
    
    
    

    mnl_settings$V = V
    mnl_settings$componentName = paste0("Class_",s)
    
    ### Compute within-class choice probabilities using MNL model
    P[[paste0("Class_",s)]] = apollo_mnl(mnl_settings, functionality)
    
    ### Take product across observation for same individual
    #P[[paste0("Class_",s)]] = apollo_panelProd(P[[paste0("Class_",s)]], apollo_inputs ,functionality)
    
    ### Average across inter-individual draws within classes
    # P[[paste0("Class_",s)]] = apollo_avgInterDraws(P[[paste0("Class_",s)]], apollo_inputs, functionality)
    
    s=s+1
  }
  
  ### Compute latent class model probabilities
  lc_settings   = list(inClassProb = P, classProb=pi_values)
  P[["model"]] = apollo_lc(lc_settings, apollo_inputs, functionality)
  
  ### Average across inter-individual draws in class allocation probabilities
  #P[["model"]] = apollo_avgInterDraws(P[["model"]], apollo_inputs, functionality)
  
  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}


# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# #

### Estimate model
model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)

### Show output in screen
apollo_modelOutput(model)

### Save output to file(s)
apollo_saveOutput(model)

#Lind=apollo_probabilities(model$estimate, apollo_inputs, functionality="estimate")



Re: Error in Bayesian estimation

Posted: 12 Aug 2020, 20:48
by stephanehess
Davide

other users posted the same issue already and this was replied to in the forum. I've merged your post into the same topic.

Stephane

Re: Error in Bayesian estimation

Posted: 13 Aug 2020, 08:52
by DavideContu
Indeed, thanks so much-I just added:

nodiagnostics=TRUE.