Aan de slagGa gratis aan de slag

Create mlogit.data object

I've pre-loaded the mlogit package and a data frame called chocolate_df that has the chocolate data in long format. We want to fit a choice model to this data, but first we need to convert it to an mlogit.data object. This can be tricky, but it is vital to using mlogit() successfully. Before you start writing code, type head(chocolate_df) directly into the console so that you can refer to the column names.

Deze oefening maakt deel uit van de cursus

Choice Modeling for Marketing in R

Cursus bekijken

Oefeninstructies

  • Identify the names of the columns where the choice and the alternative numbers are stored. Fill in the names of the columns for the choice and the alt.var in the mlogit.data() command.
  • Identify the column numbers where the attributes Brand, Price and Type are stored. Pass these column numbers as a vector for the varying input in the mlogit command.
  • Use str() to confirm that you've converted the data to an mlogit.data object.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# use mlogit.data() to convert chocolate_df to mlogit.data
chocolate <- mlogit.data(chocolate_df, shape = "long",
                         choice = "____", alt.var = "____", 
                         varying = ____)
                         
# use str() to confirm that chocolate is an mlogit.data object
Code bewerken en uitvoeren