This function prepares choice data for estimation.
prepare_data(
form,
choice_data,
re = NULL,
alternatives = NULL,
ordered = FALSE,
ranked = FALSE,
base = NULL,
id = "id",
idc = NULL,
standardize = NULL,
impute = "complete_cases"
)An object of class RprobitB_data.
[formula]
A model description with the structure choice ~ A | B | C, where
choice is the name of the dependent variable (the choices),
A are names of alternative and choice situation specific
covariates with a coefficient that is constant across alternatives,
B are names of choice situation specific covariates with
alternative specific coefficients,
and C are names of alternative and choice situation specific
covariates with alternative specific coefficients.
Multiple covariates (of one type) are separated by a + sign.
By default, alternative specific constants (ASCs) are added to the model.
They can be removed by adding +0 in the second spot.
In the ordered probit model (ordered = TRUE), the formula
object has the simple structure choice ~ A. ASCs are not estimated.
[data.frame]
Choice data in wide format, where each row represents one choice occasion.
[character() | NULL]
Names of covariates with random effects.
If re = NULL (the default), there are no random effects.
To have random effects for the ASCs, include "ASC" in re.
[character()]
The names of the choice alternatives. If not specified, the choice set is
defined by the observed choices.
If ordered = TRUE, alternatives is assumed to be specified with
the alternatives ordered from worst to best.
[logical(1)]
If TRUE, the choice set alternatives is assumed to be ordered
from worst to best.
[logical(1)]
Are the alternatives ranked?
[character(1)]
The name of the base alternative for covariates that are not
alternative specific (i.e. type 2 covariates and ASCs).
Ignored and set to NULL if the model has no alternative specific
covariates (e.g. in the ordered probit model).
By default, base is the last element of alternatives.
[character(1)]
The name of the column in choice_data that contains unique identifier
for each decision maker.
[character(1)]
The name of the column in choice_data that contains
unique identifier for each choice situation of each decision maker.
By default, these identifier are generated by the order of appearance.
[character() | "all"]
Names of covariates that get standardized.
Covariates of type 1 or 3 have to be addressed by
<covariate>_<alternative>.
If standardize = "all", all covariates get standardized.
A character that specifies how to handle missing covariate entries in
choice_data, one of:
"complete_cases", removes all rows containing missing
covariate entries (the default),
"zero", replaces missing covariate entries by zero
(only for numeric columns),
"mean", imputes missing covariate entries by the mean
(only for numeric columns).
Requirements for the data.frame choice_data:
It must contain a column named id which contains unique
identifier for each decision maker.
It can contain a column named idc which contains unique
identifier for each choice situation of each decision maker.
If this information is missing, these identifier are generated
automatically by the appearance of the choices in the data set.
It can contain a column named choice with the observed
choices, where choice must match the name of the dependent
variable in form.
Such a column is required for model fitting but not for prediction.
It must contain a numeric column named p_j for each alternative
specific covariate p in form and each choice alternative j
in alternatives.
It must contain a numeric column named q for each covariate q
in form that is constant across alternatives.
In the ordered case (ordered = TRUE), the column choice must
contain the full ranking of the alternatives in each choice occasion as a
character, where the alternatives are separated by commas, see the examples.
See the vignette on choice data for more details.
check_form() for checking the model formula
overview_effects() for an overview of the model effects
create_lagged_cov() for creating lagged covariates
as_cov_names() for re-labeling alternative-specific covariates
simulate_choices() for simulating choice data
train_test() for splitting choice data into a train and test subset
data <- prepare_data(
form = choice ~ price + time + comfort + change | 0,
choice_data = train_choice,
re = c("price", "time"),
id = "deciderID",
idc = "occasionID",
standardize = c("price", "time")
)
### ranked case
choice_data <- data.frame(
"id" = 1:3, "choice" = c("A,B,C", "A,C,B", "B,C,A"), "cov" = 1
)
data <- prepare_data(
form = choice ~ 0 | cov + 0,
choice_data = choice_data,
ranked = TRUE
)
Run the code above in your browser using DataLab