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.

Dummy vs. linear coding

Ask questions about model specifications. Ideally include a mathematical explanation of your proposed model.
Post Reply
Patrick_K
Posts: 9
Joined: 14 May 2022, 14:20

Dummy vs. linear coding

Post by Patrick_K »

Dear Stephane,
I have a question on coding of attributes.
Our experiment has three attributes: A1 has only numerical levels, A2 and A3 have three numerical levels and one nominal level each.
In my first estimation approach I have dummy-coded A2 and A3.
We have now tried to separate A2 and A3 and estimate one linear parameter (for the three numerical levels) and one dummy-coded parameter (for the one nominal level) for A2 and A3 each.
Please find below the dummy coded and the linear/dummy coded approach.
Dummy coding:
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
Daten_Choice_Pivot <- read.csv("DatenApollo.csv", header = TRUE)

Daten_Choice_Pivot$Beitr_1[which(Daten_Choice_Pivot$Beitr_1==1)] <- 1.6
Daten_Choice_Pivot$Beitr_1[which(Daten_Choice_Pivot$Beitr_1==2)] <- 1.8
Daten_Choice_Pivot$Beitr_1[which(Daten_Choice_Pivot$Beitr_1==3)] <- 3.3
Daten_Choice_Pivot$Beitr_1[which(Daten_Choice_Pivot$Beitr_1==4)] <- 4.8

Daten_Choice_Pivot$Beitr_2[which(Daten_Choice_Pivot$Beitr_2==1)] <- 1.6
Daten_Choice_Pivot$Beitr_2[which(Daten_Choice_Pivot$Beitr_2==2)] <- 1.8
Daten_Choice_Pivot$Beitr_2[which(Daten_Choice_Pivot$Beitr_2==3)] <- 3.3
Daten_Choice_Pivot$Beitr_2[which(Daten_Choice_Pivot$Beitr_2==4)] <- 4.8

Daten_Choice_Pivot$Beitr_3[which(Daten_Choice_Pivot$Beitr_3==1)] <- 1.6
Daten_Choice_Pivot$Beitr_3[which(Daten_Choice_Pivot$Beitr_3==2)] <- 1.8
Daten_Choice_Pivot$Beitr_3[which(Daten_Choice_Pivot$Beitr_3==3)] <- 3.3
Daten_Choice_Pivot$Beitr_3[which(Daten_Choice_Pivot$Beitr_3==4)] <- 4.8
database = Daten_Choice_Pivot
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any kept fixed during estimation
apollo_beta = c(b0 = 0, b_Beitr = 0,
b_HoeheEEE300 = 0,
b_HoeheEEE600 = 0,
b_HoeheEEE900 = 0,
b_HoeheEEEunb = 0,
b_ZeitEEE12 = 0,
b_ZeitEEE42 = 0,
b_ZeitEEE72 = 0,
b_ZeitEEEunb = 0)
### Vector with parameter names (in quotes) to be kept fixed at
# their starting values during estimation.
# Use apollo_beta_fixed = c() if none
apollo_fixed = c("b_HoeheEEEunb", "b_ZeitEEEunb")
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities=function(apollo_beta, apollo_inputs,
functionality="estimate"){

### Function initialisation: do not change the following three commands
### Attach and detach inputs, and create empty list of probabilities
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))
P = list()

### List of MNL utilities: must use the same names as in mnl_settings
V = list()

V[["SQ"]] = b0 + b_Beitr*Beitr_1 + b_HoeheEEE300*(HoeheEEE_1==1) + b_HoeheEEE600*(HoeheEEE_1==2) +
b_HoeheEEE900*(HoeheEEE_1==3)+ b_HoeheEEEunb*(HoeheEEE_1==4) + b_ZeitEEE12*(ZeitEEE_1==1) +
b_ZeitEEE42*(ZeitEEE_1==2) + b_ZeitEEE72*(ZeitEEE_1==3) + b_ZeitEEEunb*(ZeitEEE_1==4)
V[["RefA"]] = b_Beitr*Beitr_2 + b_HoeheEEE300*(HoeheEEE_2==1) + b_HoeheEEE600*(HoeheEEE_2==2) +
b_HoeheEEE900*(HoeheEEE_2==3)+ b_HoeheEEEunb*(HoeheEEE_2==4) + b_ZeitEEE12*(ZeitEEE_2==1) +
b_ZeitEEE42*(ZeitEEE_2==2) + b_ZeitEEE72*(ZeitEEE_2==3) + b_ZeitEEEunb*(ZeitEEE_2==4)
V[["RefB"]] = b_Beitr*Beitr_3 + b_HoeheEEE300*(HoeheEEE_3==1) + b_HoeheEEE600*(HoeheEEE_3==2) +
b_HoeheEEE900*(HoeheEEE_3==3)+ b_HoeheEEEunb*(HoeheEEE_3==4) + b_ZeitEEE12*(ZeitEEE_3==1) +
b_ZeitEEE42*(ZeitEEE_3==2) + b_ZeitEEE72*(ZeitEEE_3==3) + b_ZeitEEEunb*(ZeitEEE_3==4)

### Define settings for MNL model component
mnl_settings = list(
alternatives = c(SQ=1, RefA=2, RefB=3),
choiceVar = Choice,
utilities = V
)

Linear/Dummy coding:)
# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS ####
# ################################################################# #
Daten_Choice_Pivot <- read.csv("DatenApollo.csv", header = TRUE)

#Beitrag
Daten_Choice_Pivot$Beitr_1[which(Daten_Choice_Pivot$Beitr_1==1)] <- 1.6
Daten_Choice_Pivot$Beitr_1[which(Daten_Choice_Pivot$Beitr_1==2)] <- 1.8
Daten_Choice_Pivot$Beitr_1[which(Daten_Choice_Pivot$Beitr_1==3)] <- 3.3
Daten_Choice_Pivot$Beitr_1[which(Daten_Choice_Pivot$Beitr_1==4)] <- 4.8

Daten_Choice_Pivot$Beitr_2[which(Daten_Choice_Pivot$Beitr_2==1)] <- 1.6
Daten_Choice_Pivot$Beitr_2[which(Daten_Choice_Pivot$Beitr_2==2)] <- 1.8
Daten_Choice_Pivot$Beitr_2[which(Daten_Choice_Pivot$Beitr_2==3)] <- 3.3
Daten_Choice_Pivot$Beitr_2[which(Daten_Choice_Pivot$Beitr_2==4)] <- 4.8

Daten_Choice_Pivot$Beitr_3[which(Daten_Choice_Pivot$Beitr_3==1)] <- 1.6
Daten_Choice_Pivot$Beitr_3[which(Daten_Choice_Pivot$Beitr_3==2)] <- 1.8
Daten_Choice_Pivot$Beitr_3[which(Daten_Choice_Pivot$Beitr_3==3)] <- 3.3
Daten_Choice_Pivot$Beitr_3[which(Daten_Choice_Pivot$Beitr_3==4)] <- 4.8

# HoeheEEE
Daten_Choice_Pivot$HoeheEEE_1[which(Daten_Choice_Pivot$HoeheEEE_1==1)] <- 3
Daten_Choice_Pivot$HoeheEEE_1[which(Daten_Choice_Pivot$HoeheEEE_1==2)] <- 6
Daten_Choice_Pivot$HoeheEEE_1[which(Daten_Choice_Pivot$HoeheEEE_1==3)] <- 9

#Daten_Choice_Pivot$HoeheEEE_2[which(Daten_Choice_Pivot$HoeheEEE_2==1)] <- 3
#Daten_Choice_Pivot$HoeheEEE_2[which(Daten_Choice_Pivot$HoeheEEE_2==2)] <- 6
#Daten_Choice_Pivot$HoeheEEE_2[which(Daten_Choice_Pivot$HoeheEEE_2==3)] <- 9

Daten_Choice_Pivot$HoeheEEE_2 <- ifelse(Daten_Choice_Pivot$HoeheEEE_2==1, 3,
ifelse(Daten_Choice_Pivot$HoeheEEE_2==2, 6,
ifelse(Daten_Choice_Pivot$HoeheEEE_2==3, 9, 4)))

#Daten_Choice_Pivot$HoeheEEE_3[which(Daten_Choice_Pivot$HoeheEEE_3==1)] <- 3
#Daten_Choice_Pivot$HoeheEEE_3[which(Daten_Choice_Pivot$HoeheEEE_3==2)] <- 6
#Daten_Choice_Pivot$HoeheEEE_3[which(Daten_Choice_Pivot$HoeheEEE_3==3)] <- 9


Daten_Choice_Pivot$HoeheEEE_3 <- ifelse(Daten_Choice_Pivot$HoeheEEE_3==1, 3,
ifelse(Daten_Choice_Pivot$HoeheEEE_3==2, 6,
ifelse(Daten_Choice_Pivot$HoeheEEE_3==3, 9, 4)))

#ZeitEEE
Daten_Choice_Pivot$ZeitEEE_1[which(Daten_Choice_Pivot$ZeitEEE_1==1)] <- 1.2
Daten_Choice_Pivot$ZeitEEE_1[which(Daten_Choice_Pivot$ZeitEEE_1==2)] <- 4.2
Daten_Choice_Pivot$ZeitEEE_1[which(Daten_Choice_Pivot$ZeitEEE_1==3)] <- 7.2

Daten_Choice_Pivot$ZeitEEE_2[which(Daten_Choice_Pivot$ZeitEEE_2==1)] <- 1.2
Daten_Choice_Pivot$ZeitEEE_2[which(Daten_Choice_Pivot$ZeitEEE_2==2)] <- 4.2
Daten_Choice_Pivot$ZeitEEE_2[which(Daten_Choice_Pivot$ZeitEEE_2==3)] <- 7.2

Daten_Choice_Pivot$ZeitEEE_3[which(Daten_Choice_Pivot$ZeitEEE_3==1)] <- 1.2
Daten_Choice_Pivot$ZeitEEE_3[which(Daten_Choice_Pivot$ZeitEEE_3==2)] <- 4.2
Daten_Choice_Pivot$ZeitEEE_3[which(Daten_Choice_Pivot$ZeitEEE_3==3)] <- 7.2

database = Daten_Choice_Pivot
# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any kept fixed during estimation
apollo_beta = c(b0 = 0,
b_Beitr = 0,
b_HoeheEEE = 0,
b_HoeheEEEunb = 0, # dummy
b_ZeitEEE = 0,
b_ZeitEEEunb = 0) # dummy
### Vector with parameter names (in quotes) to be kept fixed at
# their starting values during estimation.
# Use apollo_beta_fixed = c() if none
apollo_fixed = c() #"b_HoeheEEEunb", "b_ZeitEEEunb")
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities=function(apollo_beta, apollo_inputs,
functionality="estimate"){

### Function initialisation: do not change the following three commands
### Attach and detach inputs, and create empty list of probabilities
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))
P = list()

### List of MNL utilities: must use the same names as in mnl_settings
V = list()
V[["SQ"]] = b0 + b_Beitr*Beitr_1 + b_HoeheEEE*HoeheEEE_1 + b_HoeheEEEunb*(HoeheEEE_1==4) + b_ZeitEEE*ZeitEEE_1 +
+ b_ZeitEEEunb*(ZeitEEE_1==4)
V[["RefA"]] = b_Beitr*Beitr_2 + b_HoeheEEE*(HoeheEEE_2==3|HoeheEEE_2==6|HoeheEEE_2==9)*HoeheEEE_2 + b_HoeheEEEunb*(HoeheEEE_2==4) + b_ZeitEEE*ZeitEEE_2 +
+ b_ZeitEEEunb*(ZeitEEE_2==4)
V[["RefB"]] = b_Beitr*Beitr_3 + b_HoeheEEE*(HoeheEEE_3==3|HoeheEEE_3==6|HoeheEEE_3==9)*HoeheEEE_3 + b_HoeheEEEunb*(HoeheEEE_3==4) + b_ZeitEEE*ZeitEEE_3 +
+ b_ZeitEEEunb*(ZeitEEE_3==4)

### Define settings for MNL model component
mnl_settings = list(
alternatives = c(SQ=1, RefA=2, RefB=3),
choiceVar = Choice,
utilities = V
)

My questions are now:
1) Is this a reliable approach? And is there anything in the coding that doesn´t make sense to you?
2) We´ve also tried to estimate linear and squared parameters to check if squared parameters should be included in the estimation. If we estimate the following code, there is an error saying: “Error in apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, :
Parameter b_ZeitEEEunb does not influence the log-likelihood of your model!”
Do you see any error in the coding or can imagine why the error message occurs?

#Beitrag
Daten_Choice_Pivot$Beitr_1[which(Daten_Choice_Pivot$Beitr_1==1)] <- 1.6
Daten_Choice_Pivot$Beitr_1[which(Daten_Choice_Pivot$Beitr_1==2)] <- 1.8
Daten_Choice_Pivot$Beitr_1[which(Daten_Choice_Pivot$Beitr_1==3)] <- 3.3
Daten_Choice_Pivot$Beitr_1[which(Daten_Choice_Pivot$Beitr_1==4)] <- 4.8

Daten_Choice_Pivot$Beitr_2[which(Daten_Choice_Pivot$Beitr_2==1)] <- 1.6
Daten_Choice_Pivot$Beitr_2[which(Daten_Choice_Pivot$Beitr_2==2)] <- 1.8
Daten_Choice_Pivot$Beitr_2[which(Daten_Choice_Pivot$Beitr_2==3)] <- 3.3
Daten_Choice_Pivot$Beitr_2[which(Daten_Choice_Pivot$Beitr_2==4)] <- 4.8

Daten_Choice_Pivot$Beitr_3[which(Daten_Choice_Pivot$Beitr_3==1)] <- 1.6
Daten_Choice_Pivot$Beitr_3[which(Daten_Choice_Pivot$Beitr_3==2)] <- 1.8
Daten_Choice_Pivot$Beitr_3[which(Daten_Choice_Pivot$Beitr_3==3)] <- 3.3
Daten_Choice_Pivot$Beitr_3[which(Daten_Choice_Pivot$Beitr_3==4)] <- 4.8

# HoeheEEE
Daten_Choice_Pivot$HoeheEEE_1[which(Daten_Choice_Pivot$HoeheEEE_1==1)] <- 3
Daten_Choice_Pivot$HoeheEEE_1[which(Daten_Choice_Pivot$HoeheEEE_1==2)] <- 6
Daten_Choice_Pivot$HoeheEEE_1[which(Daten_Choice_Pivot$HoeheEEE_1==3)] <- 9

#Daten_Choice_Pivot$HoeheEEE_2[which(Daten_Choice_Pivot$HoeheEEE_2==1)] <- 3
#Daten_Choice_Pivot$HoeheEEE_2[which(Daten_Choice_Pivot$HoeheEEE_2==2)] <- 6
#Daten_Choice_Pivot$HoeheEEE_2[which(Daten_Choice_Pivot$HoeheEEE_2==3)] <- 9

Daten_Choice_Pivot$HoeheEEE_2 <- ifelse(Daten_Choice_Pivot$HoeheEEE_2==1, 3,
ifelse(Daten_Choice_Pivot$HoeheEEE_2==2, 6,
ifelse(Daten_Choice_Pivot$HoeheEEE_2==3, 9, 4)))

#Daten_Choice_Pivot$HoeheEEE_3[which(Daten_Choice_Pivot$HoeheEEE_3==1)] <- 3
#Daten_Choice_Pivot$HoeheEEE_3[which(Daten_Choice_Pivot$HoeheEEE_3==2)] <- 6
#Daten_Choice_Pivot$HoeheEEE_3[which(Daten_Choice_Pivot$HoeheEEE_3==3)] <- 9


Daten_Choice_Pivot$HoeheEEE_3 <- ifelse(Daten_Choice_Pivot$HoeheEEE_3==1, 3,
ifelse(Daten_Choice_Pivot$HoeheEEE_3==2, 6,
ifelse(Daten_Choice_Pivot$HoeheEEE_3==3, 9, 4)))


#ZeitEEE
Daten_Choice_Pivot$ZeitEEE_1[which(Daten_Choice_Pivot$ZeitEEE_1==1)] <- 1.2
Daten_Choice_Pivot$ZeitEEE_1[which(Daten_Choice_Pivot$ZeitEEE_1==2)] <- 4.2
Daten_Choice_Pivot$ZeitEEE_1[which(Daten_Choice_Pivot$ZeitEEE_1==3)] <- 7.2

Daten_Choice_Pivot$ZeitEEE_2[which(Daten_Choice_Pivot$ZeitEEE_2==1)] <- 1.2
Daten_Choice_Pivot$ZeitEEE_2[which(Daten_Choice_Pivot$ZeitEEE_2==2)] <- 4.2
Daten_Choice_Pivot$ZeitEEE_2[which(Daten_Choice_Pivot$ZeitEEE_2==3)] <- 7.2

Daten_Choice_Pivot$ZeitEEE_3[which(Daten_Choice_Pivot$ZeitEEE_3==1)] <- 1.2
Daten_Choice_Pivot$ZeitEEE_3[which(Daten_Choice_Pivot$ZeitEEE_3==2)] <- 4.2
Daten_Choice_Pivot$ZeitEEE_3[which(Daten_Choice_Pivot$ZeitEEE_3==3)] <- 7.2

database = Daten_Choice_Pivot


# ################################################################# #
#### DEFINE MODEL PARAMETERS ####
# ################################################################# #
### Vector of parameters, including any kept fixed during estimation
apollo_beta = c(b0 = 0,
b_Beitr = 0,
b_Beitr_sq = 0,
b_HoeheEEE = 0,
b_HoeheEEE_sq = 0,
b_HoeheEEEunb = 0, # dummy
b_ZeitEEE = 0,
b_ZeitEEE_sq = 0,
b_ZeitEEEunb = 0) # dummy
### Vector with parameter names (in quotes) to be kept fixed at
# their starting values during estimation.
# Use apollo_beta_fixed = c() if none
apollo_fixed = c() #"b_HoeheEEEunb", "b_ZeitEEEunb")
# ################################################################# #
#### GROUP AND VALIDATE INPUTS ####
# ################################################################# #
apollo_inputs = apollo_validateInputs()
# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION ####
# ################################################################# #
apollo_probabilities=function(apollo_beta, apollo_inputs,
functionality="estimate"){

### Function initialisation: do not change the following three commands
### Attach and detach inputs, and create empty list of probabilities
apollo_attach(apollo_beta, apollo_inputs)
on.exit(apollo_detach(apollo_beta, apollo_inputs))
P = list()

### List of MNL utilities: must use the same names as in mnl_settings
V = list()

V[["SQ"]] = b0 + b_Beitr*Beitr_1 + b_Beitr_sq*Beitr_1^2 + b_HoeheEEE*HoeheEEE_1 + b_HoeheEEE_sq*HoeheEEE_1^2 + b_HoeheEEEunb*(HoeheEEE_1==4) + b_ZeitEEE*ZeitEEE_1 + b_ZeitEEE_sq*ZeitEEE_1^2
+ b_ZeitEEEunb*(ZeitEEE_1==4)
V[["RefA"]] = b_Beitr*Beitr_2 + b_Beitr_sq*Beitr_2^2 + b_HoeheEEE*(HoeheEEE_2==3|HoeheEEE_2==6|HoeheEEE_2==9)*HoeheEEE_2 + b_HoeheEEE_sq*(HoeheEEE_2==3|HoeheEEE_2==6|HoeheEEE_2==9)*HoeheEEE_2^2 + b_HoeheEEEunb*(HoeheEEE_2==4) + b_ZeitEEE*ZeitEEE_2 + b_ZeitEEE_sq*ZeitEEE_2^2
+ b_ZeitEEEunb*(ZeitEEE_2==4)
V[["RefB"]] = b_Beitr*Beitr_3 + b_Beitr_sq*Beitr_3^2 + b_HoeheEEE*(HoeheEEE_3==3|HoeheEEE_3==6|HoeheEEE_3==9)*HoeheEEE_3 + b_HoeheEEE_sq*(HoeheEEE_3==3|HoeheEEE_3==6|HoeheEEE_3==9)*HoeheEEE_3^2 + b_HoeheEEEunb*(HoeheEEE_3==4) + b_ZeitEEE*ZeitEEE_3 + b_ZeitEEE_sq*ZeitEEE_3^2
+ b_ZeitEEEunb*(ZeitEEE_3==4)

### Define settings for MNL model component
mnl_settings = list(
alternatives = c(SQ=1, RefA=2, RefB=3),
choiceVar = Choice,
utilities = V
)

Thank you in advance for your advice!
Best,
Patrick
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Dummy vs. linear coding

Post by stephanehess »

Hi

there's nothing wrong in principle with the idea, but your implementation is not correct.

You have:

Code: Select all

Daten_Choice_Pivot$ZeitEEE_1[which(Daten_Choice_Pivot$ZeitEEE_1==1)] <- 1.2
Daten_Choice_Pivot$ZeitEEE_1[which(Daten_Choice_Pivot$ZeitEEE_1==2)] <- 4.2
Daten_Choice_Pivot$ZeitEEE_1[which(Daten_Choice_Pivot$ZeitEEE_1==3)] <- 7.2
which means that you replace the categorical levels by continuous for the first three. but then you use:

Code: Select all

b_ZeitEEE*ZeitEEE_1 + b_ZeitEEEunb*(ZeitEEE_1==4)
which would mean that b_ZeitEEE would multiply a value of 4 when the level is unknown

At the very least, you would need:

Code: Select all

b_ZeitEEE*ZeitEEE_1*( ZeitEEE_1!=4)+ b_ZeitEEEunb*(ZeitEEE_1==4)
Same issue in your non-linear one at the end. In relation to the error you're getting, this is caused by the line break

Using your first utility as an example, you have:

Code: Select all

V[["SQ"]] = b0 + b_Beitr*Beitr_1 + b_Beitr_sq*Beitr_1^2 + b_HoeheEEE*HoeheEEE_1 + b_HoeheEEE_sq*HoeheEEE_1^2 + b_HoeheEEEunb*(HoeheEEE_1==4) + b_ZeitEEE*ZeitEEE_1 + b_ZeitEEE_sq*ZeitEEE_1^2
+ b_ZeitEEEunb*(ZeitEEE_1==4)
As the + is at the start of the line instead of the end of the previous line, R assumes that the input is finished in the previous line, so doesn't consider the extra bit. Best to use brackets:

Code: Select all

V[["SQ"]] = (b0 + b_Beitr*Beitr_1 + b_Beitr_sq*Beitr_1^2 + b_HoeheEEE*HoeheEEE_1 + b_HoeheEEE_sq*HoeheEEE_1^2 + b_HoeheEEEunb*(HoeheEEE_1==4) + b_ZeitEEE*ZeitEEE_1 + b_ZeitEEE_sq*ZeitEEE_1^2
+ b_ZeitEEEunb*(ZeitEEE_1==4))
and of course fix the same issue as above
--------------------------------
Stephane Hess
www.stephanehess.me.uk
Patrick_K
Posts: 9
Joined: 14 May 2022, 14:20

Re: Dummy vs. linear coding

Post by Patrick_K »

Hi Stephane,

thank you very much for checking the code.
Did I get your hints right when rewriting the code like below?

V[["SQ"]] = b0 + b_Beitr*Beitr_1 + b_HoeheEEE*HoeheEEE_1*( HoeheEEE_1!=4) + b_HoeheEEEunb*HoeheEEE_1*(HoeheEEE_1==4) +
b_ZeitEEE*ZeitEEE_1*( ZeitEEE_1!=4)+ b_ZeitEEEunb* ZeitEEE_1*(ZeitEEE_1==4)
V[["RefA"]] = b_Beitr*Beitr_2 + b_HoeheEEE*(HoeheEEE_2==3|HoeheEEE_2==6|HoeheEEE_2==9)*HoeheEEE_2 + b_HoeheEEEunb*HoeheEEE_2*(HoeheEEE_2==4) +b_ZeitEEE*ZeitEEE_2*( ZeitEEE_2!=4)+ b_ZeitEEEunb* ZeitEEE_2*(ZeitEEE_2==4)
V[["RefB"]] = b_Beitr*Beitr_3 + b_HoeheEEE*(HoeheEEE_3==3|HoeheEEE_3==6|HoeheEEE_3==9)*HoeheEEE_3 + b_HoeheEEEunb*HoeheEEE_3*(HoeheEEE_3==4) + b_ZeitEEE*ZeitEEE_3*( ZeitEEE_3!=4)+ b_ZeitEEEunb* ZeitEEE_3*(ZeitEEE_3==4)
stephanehess
Site Admin
Posts: 974
Joined: 24 Apr 2020, 16:29

Re: Dummy vs. linear coding

Post by stephanehess »

Hi

apologies for the slow response, the forum was offline due to a technical issue.

You have

Code: Select all

V[["SQ"]] = b0 + b_Beitr*Beitr_1 + b_HoeheEEE*HoeheEEE_1*( HoeheEEE_1!=4) + b_HoeheEEEunb*HoeheEEE_1*(HoeheEEE_1==4) +
b_ZeitEEE*ZeitEEE_1*( ZeitEEE_1!=4)+ b_ZeitEEEunb* ZeitEEE_1*(ZeitEEE_1==4)
while I think it should be

Code: Select all

V[["SQ"]] = b0 + b_Beitr*Beitr_1 + b_HoeheEEE*HoeheEEE_1*( HoeheEEE_1!=4) + b_HoeheEEEunb*(HoeheEEE_1==4) +
b_ZeitEEE*ZeitEEE_1*( ZeitEEE_1!=4)+ b_ZeitEEEunb*(ZeitEEE_1==4)
so drop the multiplication by the number 4 when the level is 4

Same for the other alternatives

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