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.

Coding interaction effects in RPL model

Ask questions about model specifications. Ideally include a mathematical explanation of your proposed model.
Post Reply
MattTDL
Posts: 2
Joined: 17 Oct 2020, 15:53

Coding interaction effects in RPL model

Post by MattTDL »

I'm trying to specify an RPL model with interaction effects between variables. I'm having some issues understanding the syntax of how this is done in Apollo.

Suppose I have two attributes and two alternatives, and I want to estimate the mean and standard deviation of the two attributes and the interaction between them. I.e., I want to estimate mu of b1, mu of b2, and mu of b1*b2, along with the inter-subject SD of each. Would someone be willing to point out how to do this?

Here's a mock-up of the code I have:

apollo_beta=c(
mu_b1 = 0
mu_b2 = 0
mu_b1_b2 = 0 ## This is the key variable I want to estimate (b1*b2)
sigma_b1 = 0
sigma_b2 = 0
sigma_b1_b2 = 0
)

apollo_draws = list(
interDrawsType = "mlhs",
interNDraws = 100,
interUnifDraws = c(),
interNormDraws = c(
"inter_draws_b1",
"inter_draws_b2",
"inter_draws_b1_b2
)
)

apollo_randCoeff = function(apollo_beta, apollo_inputs){
randcoeff = list()

randcoeff[["b_X1"]] = mu_b1 + sigma_b1 * inter_draws_b1
randcoeff[["b_X2"]] = mu_b2 + sigma_b2 * inter_draws_b2

randcoeff[["b_X1_X2"]] = mu_b1_b2 + sigma_b1_b2 * inter_draws_b1_b2 ## I'm unsure about how to specify the coefficient

return(randcoeff)
}

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
## skipping some code
P = list()
V = list()
V[['Alt1']] = b_X1 * Alt1_X1 +
b_X2 * Alt1_X2 +
b_X1_X2 ## I'm unsure about how to specify the utility function for alternative 1

V[['Alt2']] = b_X1 * Alt2_X1 +
b_X2 * Alt2_X2 +
b_X1_X2 ## I'm unsure about how to specify the utility function for alternative 1

## skipping some code
}

Thanks!
stephanehess
Site Admin
Posts: 1046
Joined: 24 Apr 2020, 16:29

Re: Coding interaction effects in RPL model

Post by stephanehess »

Hi

I think part of the confusion arises here as black box software doesn't make clear what interactions actually are. What you're looking for here is to model the interaction between the attributes, not the parameters, so it should say that you're estimating a mu for X1*X2, not b1*b2

The only change in your specification that is actually needed is

Code: Select all

V[['Alt1']] = b_X1 * Alt1_X1 + 
b_X2 * Alt1_X2 +
b_X1_X2 * Alt1_X1 * Alt1_X2

V[['Alt2']] = b_X1 * Alt2_X1 + 
b_X2 * Alt2_X2 +
b_X1_X2 * Alt2_X1 * Alt2_X2
--------------------------------
Stephane Hess
www.stephanehess.me.uk
MattTDL
Posts: 2
Joined: 17 Oct 2020, 15:53

Re: Coding interaction effects in RPL model

Post by MattTDL »

Ok thank you for the reply. I implemented the change in the specification you suggested, and the code runs.

Earlier, I had tried a 'hack' solution of creating new interaction variables outside of Apollo by multiplying the variables in the database together using base R prior to specifying the attributes. The outputs are essentially the same when I follow what you recommend vs my 'hack'.

//

I have one final question - I centered the original variables prior to multiplying them together to create interactions, so as to reduce multicollinearity between the original variables and their interaction terms. I take this from standard practice in 'normal' logistic regression, but does this also apply to RPL models? I notice the outputs are quite different if I don't center the variables.

//
# Here's a mock-up of my code in case the above isn't clear.

# Center the variables
database$cAlt1_X1 <- database$Alt1_X1 - mean(database$Alt1_X1)
database$cAlt1_X2 <- database$Alt1_X2 - mean(database$Alt1_X2)

# Multiplying one alternative vs another
database$Alt1_X1_X2 <- database$cAlt1_X1 * database$cAlt1_X2
...
apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))
P = list()
V = list()

V[['Alt1']] = b_X1 * cAlt1_X1 +
b_X2 * cAlt1_X2 +
b_X1_X2 * cAlt1_X1 * cAlt1_X2

V[['Alt2']] = b_X1 * cAlt2_X1 +
b_X2 * cAlt2_X2 +
b_X1_X2 * cAlt2_X1 * cAlt2_X2
...
stephanehess
Site Admin
Posts: 1046
Joined: 24 Apr 2020, 16:29

Re: Coding interaction effects in RPL model

Post by stephanehess »

Hi

sorry for the slow reply.

I would definitely not mean centre them. Interactions aside, think about the fact that the mean is possibly different for the same attribute for different alternatives. Then you could end up with a situation where in the actual data, x1_alt1>x1_alt2, but with the mean centred one, x1_alt1<x1_alt2

Stephane
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Post Reply