Page 1 of 1

Constraints do not work

Posted: 05 Oct 2022, 17:53
by intllawKuma
Hi,

I ran the following code using my own survey data.
I got the negative alpha -0.355207 even though I set the constraints c("alpha >= 0","alpha < 1").
It would be appreciated if you could tell me how to limit the sign of alpha to positive.

Code: Select all


library(apollo)
apollo_initialise()
apollo_control <- list(
  modelName       = "MNL",
  modelDescr      = "MNL model",
  indivID         = "ID",
  outputDirectory = "output"
)
database <- read.csv("dataNest/dataset1r1.csv")

apollo_beta <- c(asc = 0,
                 b_1 = 0,
                 b_2 = 0,
                 alpha = 0)
apollo_fixed <- c()

apollo_inputs <- apollo_validateInputs()
apollo_probabilities <- function(apollo_beta,apollo_inputs,functionality="estimate"){

  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[["alt1"]] = b_1 * (Chaku.1)^(1-alpha)/(1-alpha) + b_2 * (500-Seiko.1)^(1-alpha)/(1-alpha) * Prob.1/100
  V[["alt2"]] = b_1 * (Chaku.2)^(1-alpha)/(1-alpha) + b_2 * (500-Seiko.2)^(1-alpha)/(1-alpha) * Prob.2/100
  V[["alt3"]] = asc + b_1 * (Chaku.3)^(1-alpha)/(1-alpha) + b_2 * (500-Seiko.3)^(1-alpha)/(1-alpha) * Prob.3/100
  
  # Define settings for MNL model component
  mnl_settings <- list(
     alternatives = c(alt1 = 1, alt2 = 2, alt3 = 3),
     avail        = list(alt1 = 1, alt2 = 1, alt3 = 1),
     choiceVar    = choice,
     utilities     = 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_non <- apollo_estimate(apollo_beta,apollo_fixed,apollo_probabilities,apollo_inputs,estimate_settings=list(contraints=c("alpha > 0","alpha < 1")))
apollo_modelOutput(model_non, list(printPVal=2))


Re: Constraints do not work

Posted: 25 Nov 2022, 16:17
by dpalma
Hi,

There seems to be typo in your code, an "s" missing in constraints. The call to apollo_estimate should look as follows:

Code: Select all

model_non <- apollo_estimate(apollo_beta,apollo_fixed,apollo_probabilities, apollo_inputs,
                             estimate_settings=list(constraints=c("alpha > 0","alpha < 1")))
Please try again with that call, and don't forget to update Apollo to the latest available version. You can do this simply by typing install.packages("apollo") in the console.

Cheers
David

Re: Constraints do not work

Posted: 27 Nov 2022, 15:47
by intllawKuma
Dear Dr. Palma,

I did not notice the spelling mistake.
It worked well after I corrected the spelling mistake.
Thank you.