Learn R Programming

mlogit (version 0.1-1)

mlogit.data: data.frame for logit model

Description

shape a data.frame in a suitable form for the use of the mlogit function.

Usage

mlogit.data(x, choice, shape = c("wide","long"), varying = NULL,
            sep=".",alt.var = NULL, id.var = NULL,
            alt.levels = NULL, opposite = NULL, ...)

Arguments

x
a data.frame,
choice
the variable indicating the choice made,
shape
the shape of the data.frame: whether long if each row is an alternative or wide if each row is an observation,
varying
the indexes of the variables that are alternative specific,
sep
the seperator of the variable name and the alternative name (only relevant for a wide data.frame),
alt.var
the name of the variable that contains the alternative index (for a long data.frame only) or the name under which the alternative index will be stored (the default name is alt),
id.var
the name of the variable that contains the choice index or the name under which the choice index will be stored,
alt.levels
the name of the alternatives: if null, for a wide data.frame, they are guessed from the variable names and the choice variable (both should be the same), for a long data.frame, they are guessed from the a
opposite
returns the opposite of the specified variables,
...
further arguments passed to reshape.

Value

  • A data.frame in long format, i.e. one line for each alternative. The first two columns contain the choice index and the alternative index. The choice variable is a boolean which indicates the choice made. This function use reshape if the data.frame is in wide format.

See Also

reshape

Examples

Run this code
# ModeChoice is a long data.frame 

data("ModeChoice",package="Ecdat")
ModeChoice <- mlogit.data(ModeChoice,choice="mode",shape="long",
                 alt.levels=c("air","train","bus","car"))

# Same but with an alt variable called altm

data("ModeChoice",package="Ecdat")
ModeChoice$altm <- rep(c("air","train","bus","car"),210)
ModeChoice <- mlogit.data(ModeChoice,choice="mode",shape="vert",
                 alt.var="altm")

# Same but with an chid variable called ind 

data("ModeChoice",package="Ecdat")
ModeChoice$ind <- rep(1:210,each=4)
ModeChoice <- mlogit.data(ModeChoice,choice="mode",shape="long",
                 id.var="ind",alt.levels=c("air","train","bus","car"))

# Same but with two own provided variables

data("ModeChoice",package="Ecdat")
ModeChoice$ind <- rep(1:210,each=4)
ModeChoice$altm <- rep(c("air","train","bus","car"),210)
ModeChoice <- mlogit.data(ModeChoice,choice="mode",shape="long",
                 id.var="ind",alt.var="altm")


# Train is a wide data.frame with columns 'choiceid' is the choice
#  index, the alternatives are named "ch1" and "ch2", the opposite
# of the variables is returned

data("Train",package="Ecdat")
Train <- mlogit.data(Train,choice="choice",shape="wide",
                     varying=4:11,alt.levels=c("ch1","ch2"),
                     opposite=c("price","time","change","comfort"), sep="")

# Car is a wide data.frame, indexes names are provided

data("Car",package="Ecdat")
Carl <-
mlogit.data(Car,varying=5:70,shape="wide",sep="",choice="choice",
            id.var="Mr",alt.var="Car",alt.levels=1:6)

Run the code above in your browser using DataLab