class: middle, inverse .leftcol30[ <center> <img src="https://madd.seas.gwu.edu/images/logo.png" width=250> </center> ] .rightcol70[ # Week 6: .fancy[Conjoint Questions] ###
EMSE 6035: Marketing Analytics for Design Decisions ###
John Paul Helveston ###
October 04, 2022 ] --- class: inverse, middle, center # Some Quarto tips --- ### .center[Convert a data frame to a markdown table with `kable()`] -- ```r library(tidyverse) mtcars %>% kable() ``` -- .font80[ <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> mpg </th> <th style="text-align:right;"> cyl </th> <th style="text-align:right;"> disp </th> <th style="text-align:right;"> hp </th> <th style="text-align:right;"> drat </th> <th style="text-align:right;"> wt </th> <th style="text-align:right;"> qsec </th> <th style="text-align:right;"> vs </th> <th style="text-align:right;"> am </th> <th style="text-align:right;"> gear </th> <th style="text-align:right;"> carb </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Mazda RX4 </td> <td style="text-align:right;"> 21.0 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 160.0 </td> <td style="text-align:right;"> 110 </td> <td style="text-align:right;"> 3.90 </td> <td style="text-align:right;"> 2.620 </td> <td style="text-align:right;"> 16.46 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Mazda RX4 Wag </td> <td style="text-align:right;"> 21.0 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 160.0 </td> <td style="text-align:right;"> 110 </td> <td style="text-align:right;"> 3.90 </td> <td style="text-align:right;"> 2.875 </td> <td style="text-align:right;"> 17.02 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Datsun 710 </td> <td style="text-align:right;"> 22.8 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 108.0 </td> <td style="text-align:right;"> 93 </td> <td style="text-align:right;"> 3.85 </td> <td style="text-align:right;"> 2.320 </td> <td style="text-align:right;"> 18.61 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> Hornet 4 Drive </td> <td style="text-align:right;"> 21.4 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 258.0 </td> <td style="text-align:right;"> 110 </td> <td style="text-align:right;"> 3.08 </td> <td style="text-align:right;"> 3.215 </td> <td style="text-align:right;"> 19.44 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> Hornet Sportabout </td> <td style="text-align:right;"> 18.7 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 360.0 </td> <td style="text-align:right;"> 175 </td> <td style="text-align:right;"> 3.15 </td> <td style="text-align:right;"> 3.440 </td> <td style="text-align:right;"> 17.02 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> Valiant </td> <td style="text-align:right;"> 18.1 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 225.0 </td> <td style="text-align:right;"> 105 </td> <td style="text-align:right;"> 2.76 </td> <td style="text-align:right;"> 3.460 </td> <td style="text-align:right;"> 20.22 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> Duster 360 </td> <td style="text-align:right;"> 14.3 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 360.0 </td> <td style="text-align:right;"> 245 </td> <td style="text-align:right;"> 3.21 </td> <td style="text-align:right;"> 3.570 </td> <td style="text-align:right;"> 15.84 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Merc 240D </td> <td style="text-align:right;"> 24.4 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 146.7 </td> <td style="text-align:right;"> 62 </td> <td style="text-align:right;"> 3.69 </td> <td style="text-align:right;"> 3.190 </td> <td style="text-align:right;"> 20.00 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> Merc 230 </td> <td style="text-align:right;"> 22.8 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 140.8 </td> <td style="text-align:right;"> 95 </td> <td style="text-align:right;"> 3.92 </td> <td style="text-align:right;"> 3.150 </td> <td style="text-align:right;"> 22.90 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> Merc 280 </td> <td style="text-align:right;"> 19.2 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 167.6 </td> <td style="text-align:right;"> 123 </td> <td style="text-align:right;"> 3.92 </td> <td style="text-align:right;"> 3.440 </td> <td style="text-align:right;"> 18.30 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Merc 280C </td> <td style="text-align:right;"> 17.8 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 167.6 </td> <td style="text-align:right;"> 123 </td> <td style="text-align:right;"> 3.92 </td> <td style="text-align:right;"> 3.440 </td> <td style="text-align:right;"> 18.90 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Merc 450SE </td> <td style="text-align:right;"> 16.4 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 275.8 </td> <td style="text-align:right;"> 180 </td> <td style="text-align:right;"> 3.07 </td> <td style="text-align:right;"> 4.070 </td> <td style="text-align:right;"> 17.40 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 3 </td> </tr> <tr> <td style="text-align:left;"> Merc 450SL </td> <td style="text-align:right;"> 17.3 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 275.8 </td> <td style="text-align:right;"> 180 </td> <td style="text-align:right;"> 3.07 </td> <td style="text-align:right;"> 3.730 </td> <td style="text-align:right;"> 17.60 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 3 </td> </tr> <tr> <td style="text-align:left;"> Merc 450SLC </td> <td style="text-align:right;"> 15.2 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 275.8 </td> <td style="text-align:right;"> 180 </td> <td style="text-align:right;"> 3.07 </td> <td style="text-align:right;"> 3.780 </td> <td style="text-align:right;"> 18.00 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 3 </td> </tr> <tr> <td style="text-align:left;"> Cadillac Fleetwood </td> <td style="text-align:right;"> 10.4 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 472.0 </td> <td style="text-align:right;"> 205 </td> <td style="text-align:right;"> 2.93 </td> <td style="text-align:right;"> 5.250 </td> <td style="text-align:right;"> 17.98 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Lincoln Continental </td> <td style="text-align:right;"> 10.4 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 460.0 </td> <td style="text-align:right;"> 215 </td> <td style="text-align:right;"> 3.00 </td> <td style="text-align:right;"> 5.424 </td> <td style="text-align:right;"> 17.82 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Chrysler Imperial </td> <td style="text-align:right;"> 14.7 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 440.0 </td> <td style="text-align:right;"> 230 </td> <td style="text-align:right;"> 3.23 </td> <td style="text-align:right;"> 5.345 </td> <td style="text-align:right;"> 17.42 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Fiat 128 </td> <td style="text-align:right;"> 32.4 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 78.7 </td> <td style="text-align:right;"> 66 </td> <td style="text-align:right;"> 4.08 </td> <td style="text-align:right;"> 2.200 </td> <td style="text-align:right;"> 19.47 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> Honda Civic </td> <td style="text-align:right;"> 30.4 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 75.7 </td> <td style="text-align:right;"> 52 </td> <td style="text-align:right;"> 4.93 </td> <td style="text-align:right;"> 1.615 </td> <td style="text-align:right;"> 18.52 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> Toyota Corolla </td> <td style="text-align:right;"> 33.9 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 71.1 </td> <td style="text-align:right;"> 65 </td> <td style="text-align:right;"> 4.22 </td> <td style="text-align:right;"> 1.835 </td> <td style="text-align:right;"> 19.90 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> Toyota Corona </td> <td style="text-align:right;"> 21.5 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 120.1 </td> <td style="text-align:right;"> 97 </td> <td style="text-align:right;"> 3.70 </td> <td style="text-align:right;"> 2.465 </td> <td style="text-align:right;"> 20.01 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> Dodge Challenger </td> <td style="text-align:right;"> 15.5 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 318.0 </td> <td style="text-align:right;"> 150 </td> <td style="text-align:right;"> 2.76 </td> <td style="text-align:right;"> 3.520 </td> <td style="text-align:right;"> 16.87 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> AMC Javelin </td> <td style="text-align:right;"> 15.2 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 304.0 </td> <td style="text-align:right;"> 150 </td> <td style="text-align:right;"> 3.15 </td> <td style="text-align:right;"> 3.435 </td> <td style="text-align:right;"> 17.30 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> Camaro Z28 </td> <td style="text-align:right;"> 13.3 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 350.0 </td> <td style="text-align:right;"> 245 </td> <td style="text-align:right;"> 3.73 </td> <td style="text-align:right;"> 3.840 </td> <td style="text-align:right;"> 15.41 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Pontiac Firebird </td> <td style="text-align:right;"> 19.2 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 400.0 </td> <td style="text-align:right;"> 175 </td> <td style="text-align:right;"> 3.08 </td> <td style="text-align:right;"> 3.845 </td> <td style="text-align:right;"> 17.05 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> Fiat X1-9 </td> <td style="text-align:right;"> 27.3 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 79.0 </td> <td style="text-align:right;"> 66 </td> <td style="text-align:right;"> 4.08 </td> <td style="text-align:right;"> 1.935 </td> <td style="text-align:right;"> 18.90 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> Porsche 914-2 </td> <td style="text-align:right;"> 26.0 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 120.3 </td> <td style="text-align:right;"> 91 </td> <td style="text-align:right;"> 4.43 </td> <td style="text-align:right;"> 2.140 </td> <td style="text-align:right;"> 16.70 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 5 </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> Lotus Europa </td> <td style="text-align:right;"> 30.4 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 95.1 </td> <td style="text-align:right;"> 113 </td> <td style="text-align:right;"> 3.77 </td> <td style="text-align:right;"> 1.513 </td> <td style="text-align:right;"> 16.90 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 5 </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> Ford Pantera L </td> <td style="text-align:right;"> 15.8 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 351.0 </td> <td style="text-align:right;"> 264 </td> <td style="text-align:right;"> 4.22 </td> <td style="text-align:right;"> 3.170 </td> <td style="text-align:right;"> 14.50 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 5 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Ferrari Dino </td> <td style="text-align:right;"> 19.7 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 145.0 </td> <td style="text-align:right;"> 175 </td> <td style="text-align:right;"> 3.62 </td> <td style="text-align:right;"> 2.770 </td> <td style="text-align:right;"> 15.50 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 5 </td> <td style="text-align:right;"> 6 </td> </tr> <tr> <td style="text-align:left;"> Maserati Bora </td> <td style="text-align:right;"> 15.0 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 301.0 </td> <td style="text-align:right;"> 335 </td> <td style="text-align:right;"> 3.54 </td> <td style="text-align:right;"> 3.570 </td> <td style="text-align:right;"> 14.60 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 5 </td> <td style="text-align:right;"> 8 </td> </tr> <tr> <td style="text-align:left;"> Volvo 142E </td> <td style="text-align:right;"> 21.4 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 121.0 </td> <td style="text-align:right;"> 109 </td> <td style="text-align:right;"> 4.11 </td> <td style="text-align:right;"> 2.780 </td> <td style="text-align:right;"> 18.60 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 2 </td> </tr> </tbody> </table> ] --- ### .center[Example from last year] ```r library(tidyverse) library(here) df <- read_csv(here("data", "competitors.csv")) df %>% kable() ``` .font80[ <table> <thead> <tr> <th style="text-align:left;"> brand </th> <th style="text-align:right;"> volumeInOz </th> <th style="text-align:right;"> quantity </th> <th style="text-align:left;"> price </th> <th style="text-align:left;"> pricePerCup </th> <th style="text-align:left;"> biodegrability </th> <th style="text-align:left;"> opacity </th> <th style="text-align:left;"> logo </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> greatValue </td> <td style="text-align:right;"> 9 </td> <td style="text-align:right;"> 50 </td> <td style="text-align:left;"> $2.98 </td> <td style="text-align:left;"> $0.06 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> TRUE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> decorRack </td> <td style="text-align:right;"> 9 </td> <td style="text-align:right;"> 50 </td> <td style="text-align:left;"> $5.99 </td> <td style="text-align:left;"> $0.12 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> TRUE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> tigerChef </td> <td style="text-align:right;"> 9 </td> <td style="text-align:right;"> 100 </td> <td style="text-align:left;"> $7.99 </td> <td style="text-align:left;"> $0.08 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> TRUE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> smartly </td> <td style="text-align:right;"> 9 </td> <td style="text-align:right;"> 80 </td> <td style="text-align:left;"> $2.79 </td> <td style="text-align:left;"> $0.03 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> solo </td> <td style="text-align:right;"> 9 </td> <td style="text-align:right;"> 50 </td> <td style="text-align:left;"> $4.04 </td> <td style="text-align:left;"> $0.08 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> greatValue </td> <td style="text-align:right;"> 9 </td> <td style="text-align:right;"> 100 </td> <td style="text-align:left;"> $3.76 </td> <td style="text-align:left;"> $0.04 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> ecoProducts </td> <td style="text-align:right;"> 9 </td> <td style="text-align:right;"> 1000 </td> <td style="text-align:left;"> $187.69 </td> <td style="text-align:left;"> $0.19 </td> <td style="text-align:left;"> TRUE </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> TRUE </td> </tr> <tr> <td style="text-align:left;"> worldCentric </td> <td style="text-align:right;"> 9 </td> <td style="text-align:right;"> 2000 </td> <td style="text-align:left;"> $220.00 </td> <td style="text-align:left;"> $0.11 </td> <td style="text-align:left;"> TRUE </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> TRUE </td> </tr> <tr> <td style="text-align:left;"> hefty </td> <td style="text-align:right;"> 18 </td> <td style="text-align:right;"> 50 </td> <td style="text-align:left;"> $3.98 </td> <td style="text-align:left;"> $0.08 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> TRUE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> solo </td> <td style="text-align:right;"> 18 </td> <td style="text-align:right;"> 50 </td> <td style="text-align:left;"> $3.98 </td> <td style="text-align:left;"> $0.08 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> TRUE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> greatValue </td> <td style="text-align:right;"> 18 </td> <td style="text-align:right;"> 120 </td> <td style="text-align:left;"> $5.98 </td> <td style="text-align:left;"> $0.05 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> TRUE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> upAndUp </td> <td style="text-align:right;"> 18 </td> <td style="text-align:right;"> 36 </td> <td style="text-align:left;"> $2.49 </td> <td style="text-align:left;"> $0.07 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> TRUE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> greatValue </td> <td style="text-align:right;"> 16 </td> <td style="text-align:right;"> 25 </td> <td style="text-align:left;"> $1.98 </td> <td style="text-align:left;"> $0.08 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> solo </td> <td style="text-align:right;"> 18 </td> <td style="text-align:right;"> 25 </td> <td style="text-align:left;"> $3.05 </td> <td style="text-align:left;"> $0.12 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> upAndUp </td> <td style="text-align:right;"> 18 </td> <td style="text-align:right;"> 28 </td> <td style="text-align:left;"> $2.99 </td> <td style="text-align:left;"> $0.11 </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> FALSE </td> </tr> <tr> <td style="text-align:left;"> worldCentric </td> <td style="text-align:right;"> 16 </td> <td style="text-align:right;"> 1000 </td> <td style="text-align:left;"> $181.00 </td> <td style="text-align:left;"> $0.18 </td> <td style="text-align:left;"> TRUE </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> TRUE </td> </tr> <tr> <td style="text-align:left;"> ecoProducts </td> <td style="text-align:right;"> 16 </td> <td style="text-align:right;"> 1000 </td> <td style="text-align:left;"> $279.85 </td> <td style="text-align:left;"> $0.28 </td> <td style="text-align:left;"> TRUE </td> <td style="text-align:left;"> FALSE </td> <td style="text-align:left;"> TRUE </td> </tr> </tbody> </table> ] --- class: center, middle # More `kable()` formatting options:<br>[{kableExtra} package](https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html) --- # References **Simple approach**: Insert a footnote with `^[]` ### markdown `The Eiffel Tower is 324 meters tall^[From the [Eiffel Tower wikipedia page](https://en.wikipedia.org/wiki/Eiffel_Tower)]` ### render The Eiffel Tower is 324 meters tall<sup>1</sup> .font70[<sup>1</sup>From the [Eiffel Tower wikipedia page](https://en.wikipedia.org/wiki/Eiffel_Tower)] --- # References **Complex (but more complete) approach**: Use bibtex https://quarto.org/docs/authoring/footnotes-and-citations.html You can insert citations with `[@citekey]`, and a "References" table will be automatically created. -- <br> **Footnotes are perfectly fine for this class** --- class: inverse, middle # Week 6: .fancy[Conjoint Questions] ### 1. Defining choice questions in R ### 2. Displaying choice questions in Quarto ### QUIZ 2 ### 3. Choice questions in formr --- class: inverse, middle # Week 6: .fancy[Conjoint Questions] ### 1. .orange[Defining choice questions in R] ### 2. Displaying choice questions in Quarto ### QUIZ 2 ### 3. Choice questions in formr --- class: center ## Download the [logitr-cars](https://github.com/jhelvy/logitr-cars) repo from GitHub <center> <img src="images/logitr-cars.png" width=900> </center> --- ## .center[We'll be using the [{cbcTools} package](https://jhelvy.github.io/cbcTools/) today] ```r install.packages("cbcTools") ``` --- # Choice question components ### 1. Generate `profiles` for each attribute and level ### 2. Create a survey `design` data frame from `profiles` --- class: center # Basic Design Any combination of attributes can be shown in each choice question .leftcol[.font90[ ## Question 1 <table> <tbody> <tr> <td style="text-align:left;"> Option: </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> 3 </td> </tr> <tr> <td style="text-align:left;"> Price: </td> <td style="text-align:left;"> $15 </td> <td style="text-align:left;"> $20 </td> <td style="text-align:left;"> $15 </td> </tr> <tr> <td style="text-align:left;"> Fuel Economy: </td> <td style="text-align:left;"> 30 (mpg) </td> <td style="text-align:left;"> 30 (mpg) </td> <td style="text-align:left;"> 30 (mpg) </td> </tr> <tr> <td style="text-align:left;"> Accel. Time: </td> <td style="text-align:left;"> 6 (s) </td> <td style="text-align:left;"> 6 (s) </td> <td style="text-align:left;"> 7 (s) </td> </tr> <tr> <td style="text-align:left;"> Powertrain: </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> Gasoline </td> </tr> </tbody> </table> ]] .rightcol[.font90[ ## Question 2 <table> <tbody> <tr> <td style="text-align:left;"> Option: </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> 3 </td> </tr> <tr> <td style="text-align:left;"> Price: </td> <td style="text-align:left;"> $20 </td> <td style="text-align:left;"> $15 </td> <td style="text-align:left;"> $20 </td> </tr> <tr> <td style="text-align:left;"> Fuel Economy: </td> <td style="text-align:left;"> 30 (mpg) </td> <td style="text-align:left;"> 20 (mpg) </td> <td style="text-align:left;"> 25 (mpg) </td> </tr> <tr> <td style="text-align:left;"> Accel. Time: </td> <td style="text-align:left;"> 8 (s) </td> <td style="text-align:left;"> 7 (s) </td> <td style="text-align:left;"> 7 (s) </td> </tr> <tr> <td style="text-align:left;"> Powertrain: </td> <td style="text-align:left;"> Electric </td> <td style="text-align:left;"> Electric </td> <td style="text-align:left;"> Gasoline </td> </tr> </tbody> </table> ]] --- class: center # Labeled Design One attribute is used as the "label" - choice options are fixed according to the label .leftcol[.font90[ ## Question 1 <table> <tbody> <tr> <td style="text-align:left;"> Option: </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> Electric </td> </tr> <tr> <td style="text-align:left;"> Price: </td> <td style="text-align:left;"> $15 </td> <td style="text-align:left;"> $20 </td> </tr> <tr> <td style="text-align:left;"> Fuel Economy: </td> <td style="text-align:left;"> 20 (mpg) </td> <td style="text-align:left;"> 25 (mpg) </td> </tr> <tr> <td style="text-align:left;"> Accel. Time: </td> <td style="text-align:left;"> 7 (s) </td> <td style="text-align:left;"> 6 (s) </td> </tr> </tbody> </table> ]] .rightcol[.font90[ ## Question 2 <table> <tbody> <tr> <td style="text-align:left;"> Option: </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> Electric </td> </tr> <tr> <td style="text-align:left;"> Price: </td> <td style="text-align:left;"> $20 </td> <td style="text-align:left;"> $15 </td> </tr> <tr> <td style="text-align:left;"> Fuel Economy: </td> <td style="text-align:left;"> 20 (mpg) </td> <td style="text-align:left;"> 30 (mpg) </td> </tr> <tr> <td style="text-align:left;"> Accel. Time: </td> <td style="text-align:left;"> 6 (s) </td> <td style="text-align:left;"> 8 (s) </td> </tr> </tbody> </table> ]] --- class: center # Design with a "None" option A "none" option means they can choose an "other" option .leftcol[.font90[ ## Question 1 <table> <tbody> <tr> <td style="text-align:left;"> Option: </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> 3 </td> <td style="text-align:left;"> None </td> </tr> <tr> <td style="text-align:left;"> Price: </td> <td style="text-align:left;"> $15 </td> <td style="text-align:left;"> $20 </td> <td style="text-align:left;"> $15 </td> <td style="text-align:left;"> </td> </tr> <tr> <td style="text-align:left;"> Fuel Economy: </td> <td style="text-align:left;"> 30 (mpg) </td> <td style="text-align:left;"> 25 (mpg) </td> <td style="text-align:left;"> 25 (mpg) </td> <td style="text-align:left;"> </td> </tr> <tr> <td style="text-align:left;"> Accel. Time: </td> <td style="text-align:left;"> 7 (s) </td> <td style="text-align:left;"> 6 (s) </td> <td style="text-align:left;"> 7 (s) </td> <td style="text-align:left;"> </td> </tr> <tr> <td style="text-align:left;"> Powertrain: </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> </td> </tr> </tbody> </table> ]] .rightcol[.font90[ ## Question 2 <table> <tbody> <tr> <td style="text-align:left;"> Option: </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> 3 </td> <td style="text-align:left;"> None </td> </tr> <tr> <td style="text-align:left;"> Price: </td> <td style="text-align:left;"> $25 </td> <td style="text-align:left;"> $20 </td> <td style="text-align:left;"> $25 </td> <td style="text-align:left;"> </td> </tr> <tr> <td style="text-align:left;"> Fuel Economy: </td> <td style="text-align:left;"> 20 (mpg) </td> <td style="text-align:left;"> 25 (mpg) </td> <td style="text-align:left;"> 30 (mpg) </td> <td style="text-align:left;"> </td> </tr> <tr> <td style="text-align:left;"> Accel. Time: </td> <td style="text-align:left;"> 7 (s) </td> <td style="text-align:left;"> 6 (s) </td> <td style="text-align:left;"> 8 (s) </td> <td style="text-align:left;"> </td> </tr> <tr> <td style="text-align:left;"> Powertrain: </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> Electric </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> </td> </tr> </tbody> </table> ]] --- class: middle, inverse, center # Open `logitr-cars.Rproj` --- # Attribute-specific features Some attributes may only be valid for certain levels of other attributes > **Example**: The driving range of an electric vehicle (EV) only applies to EVs and not gasoline-powered vehicles. -- <br> To implement this, edit `profiles` prior to using `cbc_design()` --- # Restricted profiles Sometimes you may want to not allow a specific combination of features - use `cbc_restrict()` to implement this (see `logitr-cars` code 1.3) -- <br> # .red[Warning]: Avoid restrictions if possible! --- class: inverse
20
:
00
## Your Turn 1. With your team, discuss the specific choice question design for your project - Regular or labeled? - Include a "none" option (outside good) or not? - Include restrictions? 2. Edit the `make-choice-questions.R` file to design your choice questions. --- class: inverse, middle # Week 6: .fancy[Conjoint Questions] ### 1. Defining choice questions in R ### 2. .orange[Displaying choice questions in Quarto] ### QUIZ 2 ### 3. Choice questions in formr --- # .center[Displaying your choice questions online] ### 1. Export your choice questions as a .csv file ### 2. Upload your .csv file somewhere (e.g. GitHub) ### 3. Use R code to extract the values to display ### 4. Use RMarkdown to display the values --- ### 1. Export your choice questions as a .csv file ```r write_csv(design, here('choice_questions.csv')) ``` -- ### 2. Upload your .csv file somewhere .leftcol[.center[ ### Inside a formr run (private) <center> <img src="images/upload.png" width=500> </center> ]] .rightcol[.center[ ### github.com (public) .font200[[
](https://github.com/)] [apples example](https://github.com/jhelvy/formr4conjoint) ]] --- ## .center[Use R code to extract the values to display] ### - Read `choice_questions.csv` from web ### - Randomly choose a respondent ID ### - Filter rows for that respondent ID ### - Serialize the data frame to json format --- # .center[Side note on serializing a data frame] .center[Converts a data frame to one long string] ```r df ``` ``` #> profileID altID price fuelEconomy accelTime powertrain #> 1 7 1 15 30 6 Gasoline #> 2 8 2 20 30 6 Gasoline #> 3 16 3 15 30 7 Gasoline ``` ```r df_json <- jsonlite::serializeJSON(df) df_json ``` ``` #> {"type":"list","attributes":{"names":{"type":"character","attributes":{},"value":["profileID","altID","price","fuelEconomy","accelTime","powertrain"]},"class":{"type":"character","attributes":{},"value":["data.frame"]},"row.names":{"type":"integer","attributes":{},"value":[1,2,3]}},"value":[{"type":"integer","attributes":{},"value":[7,8,16]},{"type":"integer","attributes":{},"value":[1,2,3]},{"type":"double","attributes":{},"value":[15,20,15]},{"type":"double","attributes":{},"value":[30,30,30]},{"type":"double","attributes":{},"value":[6,6,7]},{"type":"integer","attributes":{"levels":{"type":"character","attributes":{},"value":["Gasoline","Electric"]},"class":{"type":"character","attributes":{},"value":["factor"]}},"value":[1,1,1]}]} ``` --- # .center[Use RMarkdown to display the values] .leftcol[ Create separate data frames for each alternative ```r library(dplyr) alts <- jsonlite::unserializeJSON(df_json) alt1 <- alts %>% filter(altID == 1) alt2 <- alts %>% filter(altID == 2) alt3 <- alts %>% filter(altID == 3) ``` ] -- .rightcol[ Use RMarkdown formatting to display content in each alternative ```r ***Option 1** ***Price**: $ `r alt1$price` ***Powertrain**: $ `r alt1$powertrain` ***Fuel Economy**: `r alt1$fuelEconomy` mpg ***0-60 Accel. Time**: `r alt1$accelTime` s ``` **Option 1** **Price**: $ 15<br> **Powertrain**: $ Gasoline<br> **Fuel Economy**: 30 mpg<br> **0-60 Accel. Time**: 6 s ] --- ## .center[Show options in a table with `kable()`] .leftcol[ ```r library(dplyr) alts <- jsonlite::unserializeJSON(df_json) %>% # Add $ sign to price mutate(price = scales::dollar(price)) %>% # Make nicer attribute labels select( `Option:` = altID, `Powertrain:` = powertrain, `Price:` = price, `Fuel Economy (mpg):` = fuelEconomy, `Accel. Time (s):` = accelTime) # Drop row names row.names(alts) <- NULL ``` ] .rightcol[ Display the _transpose_, `t(alts)` ```r kable(t(alts)) ``` <table> <tbody> <tr> <td style="text-align:left;"> Option: </td> <td style="text-align:left;"> 1 </td> <td style="text-align:left;"> 2 </td> <td style="text-align:left;"> 3 </td> </tr> <tr> <td style="text-align:left;"> Powertrain: </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> Gasoline </td> </tr> <tr> <td style="text-align:left;"> Price: </td> <td style="text-align:left;"> $15 </td> <td style="text-align:left;"> $20 </td> <td style="text-align:left;"> $15 </td> </tr> <tr> <td style="text-align:left;"> Fuel Economy (mpg): </td> <td style="text-align:left;"> 30 </td> <td style="text-align:left;"> 30 </td> <td style="text-align:left;"> 30 </td> </tr> <tr> <td style="text-align:left;"> Accel. Time (s): </td> <td style="text-align:left;"> 6 </td> <td style="text-align:left;"> 6 </td> <td style="text-align:left;"> 7 </td> </tr> </tbody> </table> ] --- class: center ## Download the [formr4conjoint](https://github.com/jhelvy/formr4conjoint) repo from GitHub (code used in the related [blog post](https://www.jhelvy.com/posts/2021-09-18-choice-based-conjoint-surveys-in-r-with-formr/)) <center> <img src="images/formr4conjoint.png" width=870> </center> --- class: inverse
20
:
00
## Your Turn .leftcol80[ 1. With your team, upload your `choice_questions.csv` file somewhere online (e.g. inside a formr run or on a GitHub repo). 2. Edit the `p2-choice-questions.qmd` or `p2-choice-questions-table.qmd` file to implement your choice questions in RMarkdown. You should be able to render the file to visually test how one of your choice questions is rendering. ] --- class: inverse # Quiz 2
10
:
00
.leftcol[ ### Download the template from the #class channel ### Make sure you unzip it! ### When done, submit your `quiz2.qmd` on Blackboard ] .rightcol[ <center> <img src="https://github.com/emse-p4a-gwu/2022-Spring/raw/main/images/quiz_doge.png" width="400"> </center> ] --- class: inverse, middle # Week 6: .fancy[Conjoint Questions] ### 1. Defining choice questions in R ### 2. Displaying choice questions in Quarto ### QUIZ 2 ### 3. .orange[Choice questions in formr] --- # Your first few rows <br> ### - Read `choice_questions.csv` from web ### - Randomly choose a respondent ID ### - Filter rows for that respondent ID ### - Serialize the data frame to json format --- ### Using the `calculate` type ([example sheet](https://docs.google.com/spreadsheets/d/1Ih3Pt6uz-gp5vc0SBxBzl4K0aZoRLwI6dtdtZiXSLz0/edit#gid=1611481919)) .leftcol[ RMarkdown ```r # Read in the choice questions library(tidyverse) design <- read_csv("https://raw.githubusercontent.com/jhelvy/formr4conjoint/master/survey/choice_questions.csv") # Define the respondent ID respondentID <- sample(design$respID, 1) # Create the subset of rows for that respondent ID df <- design %>% filter(respID == respondentID) %>% mutate(image = paste0("https://raw.githubusercontent.com/jhelvy/formr4conjoint/master/survey/images/", image)) # Convert df to json df_json <- jsonlite::serializeJSON(df) ``` ] .rightcol[ Google sheet <center> <img src="images/calculate.png" width=100%> </center> ] --- ## Random choice questions as **buttons** ([example sheet](https://docs.google.com/spreadsheets/d/1Ih3Pt6uz-gp5vc0SBxBzl4K0aZoRLwI6dtdtZiXSLz0/edit?usp=sharing)) Use the `mc_button` question type .leftcol[ ### `label` - Show your question text - Insert a code chunk to create one-row data frame for each alternative ### `choice` columns - Insert RMarkdown code to display each alternative ] .rightcol[ <center> <img src="images/cbc_buttons.png" width=100%> </center> ] --- ## Random choice questions as **table** ([example sheet](https://docs.google.com/spreadsheets/d/1EG14Eh9kDBvE_iETfm6l6g90mrQ5sl_zbWaXkwIOGLU/edit?usp=sharing)) - Use the `mc_button` question type .leftcol[ ### `label` - Show your question text - Insert a code chunk to modify `alts` data frame & display it using `kable()` - Use [kableExtra](https://haozhu233.github.io/kableExtra/awesome_table_in_html.html#Basic_HTML_table) to control table styling ### `choice` columns - Simple text / number for each option ] .rightcol[ <center> <img src="images/cbc_table.png" width=100%> </center> ] --- class: inverse ## Your Turn 1. Discuss the layout you would prefer to implement for your choice questions (buttons or table). 2. Make a Google Sheet using your team Google account to start implementing your conjoint questions. .leftcol[.center[ ## [buttons example sheet](https://docs.google.com/spreadsheets/d/1Ih3Pt6uz-gp5vc0SBxBzl4K0aZoRLwI6dtdtZiXSLz0/edit?usp=sharing) ]] .rightcol[.center[ ## [table example sheet](https://docs.google.com/spreadsheets/d/1EG14Eh9kDBvE_iETfm6l6g90mrQ5sl_zbWaXkwIOGLU/edit?usp=sharing) ]]