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 02, 2024 ] --- 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 `^[]` .leftcol[ ### This markdown... `The Eiffel Tower is 324 meters tall^[From the [Eiffel Tower wikipedia page](https://en.wikipedia.org/wiki`<br>`/Eiffel_Tower)]` ] .rightcol[ ### ...renders as this 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 # 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 surveydown --- class: inverse, middle # Week 6: .fancy[Conjoint Questions] ### 1. .orange[Defining choice questions in R] ### 2. Displaying choice questions in surveydown --- ## Download & unzip two repos: ## - [logitr-cars](https://github.com/jhelvy/logitr-cars/archive/refs/heads/main.zip) ## - [demo-choice-based-conjoint](https://github.com/surveydown-dev/demo-choice-based-conjoint/archive/refs/heads/main.zip) (code used in the [blog post](https://surveydown.org/blog/2024-08-28-choice-based-conjoint-surveys-with-surveydown/) on conjoint in surveydown) --- class: middle, inverse, center # Open `logitr-cars.Rproj` --- ## .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;"> $25 </td> <td style="text-align:left;"> $25 </td> </tr> <tr> <td style="text-align:left;"> Fuel Economy: </td> <td style="text-align:left;"> 25 (mpg) </td> <td style="text-align:left;"> 25 (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;"> 6 (s) </td> <td style="text-align:left;"> 7 (s) </td> <td style="text-align:left;"> 8 (s) </td> </tr> <tr> <td style="text-align:left;"> Powertrain: </td> <td style="text-align:left;"> Electric </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> Electric </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;"> $25 </td> <td style="text-align:left;"> $25 </td> </tr> <tr> <td style="text-align:left;"> Fuel Economy: </td> <td style="text-align:left;"> 25 (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;"> 7 (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;"> Electric </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;"> 30 (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> </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;"> $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;"> 20 (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;"> 7 (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;"> $20 </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;"> 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;"> Electric </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;"> $15 </td> <td style="text-align:left;"> $25 </td> <td style="text-align:left;"> $20 </td> <td style="text-align:left;"> </td> </tr> <tr> <td style="text-align:left;"> Fuel Economy: </td> <td style="text-align:left;"> 25 (mpg) </td> <td style="text-align:left;"> 30 (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;"> 8 (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;"> Electric </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> Gasoline </td> <td style="text-align:left;"> </td> </tr> </tbody> </table> ]] --- # 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()` (see `logitr-cars` code 1.2) --- # 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 ## Open `survey.Rproj` in "demo-choice-based-conjoint" 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? 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 surveydown] --- # .center[Displaying choice questions in your survey] ### 1. Read in the design file (`choice_questions.csv`) ### 2. Sample and store a random respondent ID ### 3. Filter the design for the respondentID ### 4. Define a function to create question options ### 5. Create the options for each choice question ### 6. Create each choice question (6 in total) --- ### 1. Read in the design file (`choice_questions.csv`) ``` r design <- readr::read_csv("choice_questions.csv") ``` --- ### 2. Sample and store a random respondent ID Sample a random respondentID ``` r respondentID <- sample(design$respID, 1) ``` Store the respondentID ``` r sd_store_value(respondentID, "respID") ``` In your survey data, you will have a `respID` column --- ### 3. Filter the design for the respondentID ``` r df <- design %>% filter(respID == respondentID) %>% mutate(image = paste0("images/", image)) ``` Images are stored in `"images"` folder, so we paste on that path -- Result looks like this: ``` r head(df) ``` ``` #> # A tibble: 6 × 9 #> respID qID altID obsID profileID type price freshness image #> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <chr> <chr> #> 1 1646 1 1 13161 37 Gala 1 Average images/gala.jpg #> 2 1646 1 2 13161 3 Honeycrisp 1 Excellent images/honeycrisp.jpg #> 3 1646 1 3 13161 47 Gala 2 Average images/gala.jpg #> 4 1646 2 1 13162 51 Fuji 2.5 Average images/fuji.jpg #> 5 1646 2 2 13162 31 Fuji 4 Excellent images/fuji.jpg #> 6 1646 2 3 13162 75 Red Delicious 1 Poor images/redDelicious.jpg ``` --- .leftcol[ ### 4. Define a function to create question options ] .rightcol[.code70[ ``` r make_cbc_options <- function(df) { alt1 <- df |> filter(altID == 1) alt2 <- df |> filter(altID == 2) alt3 <- df |> filter(altID == 3) options <- c("option_1", "option_2", "option_3") names(options) <- c( glue(" **Option 1**<br> <img src='{alt1$image}' width=100><br> **Type**: {alt1$type}<br> **Price**: $ {alt1$price} / lb "), glue(" **Option 2**<br> <img src='{alt2$image}' width=100><br> **Type**: {alt2$type}<br> **Price**: $ {alt2$price} / lb "), glue(" **Option 3**<br> <img src='{alt3$image}' width=100><br> **Type**: {alt3$type}<br> **Price**: $ {alt3$price} / lb ") ) return(options) } ``` ]] --- .leftcol[ ### 4. Define a function to create question options Make one-row data frames for each alternative. For example, `alt1` looks like this: ``` #> Rows: 1 #> Columns: 9 #> $ respID <dbl> 1646 #> $ qID <dbl> 1 #> $ altID <dbl> 1 #> $ obsID <dbl> 13161 #> $ profileID <dbl> 37 #> $ type <chr> "Gala" #> $ price <dbl> 1 #> $ freshness <chr> "Average" #> $ image <chr> "images/gala.jpg" ``` ] .rightcol[.code70[ ``` r make_cbc_options <- function(df) { * alt1 <- df |> filter(altID == 1) * alt2 <- df |> filter(altID == 2) * alt3 <- df |> filter(altID == 3) options <- c("option_1", "option_2", "option_3") names(options) <- c( glue(" **Option 1**<br> <img src='{alt1$image}' width=100><br> **Type**: {alt1$type}<br> **Price**: $ {alt1$price} / lb "), glue(" **Option 2**<br> <img src='{alt2$image}' width=100><br> **Type**: {alt2$type}<br> **Price**: $ {alt2$price} / lb "), glue(" **Option 3**<br> <img src='{alt3$image}' width=100><br> **Type**: {alt3$type}<br> **Price**: $ {alt3$price} / lb ") ) return(options) } ``` ]] --- .leftcol[ ### 4. Define a function to create question options ``` r alt1[c('image', 'type', 'price')] ``` ``` #> # A tibble: 1 × 3 #> image type price #> <chr> <chr> <dbl> #> 1 images/gala.jpg Gala 1 ``` Highlighted section renders as: ``` #> **Option 1**<br> #> <img src='images/gala.jpg' width=100><br> #> **Type**: Gala<br> #> **Price**: $ 1 / lb ``` ] .rightcol[.code70[ ``` r make_cbc_options <- function(df) { alt1 <- df |> filter(altID == 1) alt2 <- df |> filter(altID == 2) alt3 <- df |> filter(altID == 3) options <- c("option_1", "option_2", "option_3") names(options) <- c( * glue(" * **Option 1**<br> * <img src='{alt1$image}' width=100><br> * **Type**: {alt1$type}<br> * **Price**: $ {alt1$price} / lb * "), glue(" **Option 2**<br> <img src='{alt2$image}' width=100><br> **Type**: {alt2$type}<br> **Price**: $ {alt2$price} / lb "), glue(" **Option 3**<br> <img src='{alt3$image}' width=100><br> **Type**: {alt3$type}<br> **Price**: $ {alt3$price} / lb ") ) return(options) } ``` ]] --- ### 5. Create the options for each choice question ``` r cbc1_options <- make_cbc_options(df |> filter(qID == 1)) ``` Value stored in your data: ``` #> option_1 ``` .leftcol[ Label: ``` #> [1] " **Option 1**<br>\n <img src='images/gala.jpg' width=100><br>\n **Type**: Gala<br>\n **Price**: $ 1 / lb" ``` ] .rightcol[ Renders as: **Option 1**
**Type**: Gala
**Price**: $ 1 / lb ] --- ### 5. Create the options for each choice question Now make the options for each question ``` r cbc1_options <- make_cbc_options(df |> filter(qID == 1)) cbc2_options <- make_cbc_options(df |> filter(qID == 2)) cbc3_options <- make_cbc_options(df |> filter(qID == 3)) cbc4_options <- make_cbc_options(df |> filter(qID == 4)) cbc5_options <- make_cbc_options(df |> filter(qID == 5)) cbc6_options <- make_cbc_options(df |> filter(qID == 6)) ``` --- ### 6. Create each choice question (6 in total) .leftcol[ Code: ``` r sd_question( type = 'mc_buttons', id = 'cbc_q1', label = "(1 of 6) If these were your only options, which would you choose?", option = cbc1_options ) ``` ] .rightcol[ Renders as: <center> <img src="images/example.png" width=100%> </center> ] --- ## .center[TABLE layout: Show options in a table with `kable()`] ``` r library(dplyr) alts <- df %>% filter(qID == 1) %>% mutate( # Add $ sign to price and html code for image path price = paste(scales::dollar(price), "/ lb"), image = paste0('<img src="', image, '" width=100>')) %>% # Make nicer attribute labels select( `Option:` = altID, ` ` = image, `Price:` = price, `Type:` = type, `Freshness:` = freshness) # Drop row names row.names(alts) <- NULL ``` --- ## .center[Option: Show options in a table with `kable()`] Display the _transpose_, `t(alts)` ``` r kbl(t(alts), escape = FALSE) %>% kable_styling( bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE, position = "center" ) ``` <table class="table table-striped table-hover table-condensed" style="color: black; width: auto !important; margin-left: auto; margin-right: auto;"> <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;"> </td> <td style="text-align:left;"> <img src="images/gala.jpg" width="100"> </td> <td style="text-align:left;"> <img src="images/honeycrisp.jpg" width="100"> </td> <td style="text-align:left;"> <img src="images/gala.jpg" width="100"> </td> </tr> <tr> <td style="text-align:left;"> Price: </td> <td style="text-align:left;"> $1 / lb </td> <td style="text-align:left;"> $1 / lb </td> <td style="text-align:left;"> $2 / lb </td> </tr> <tr> <td style="text-align:left;"> Type: </td> <td style="text-align:left;"> Gala </td> <td style="text-align:left;"> Honeycrisp </td> <td style="text-align:left;"> Gala </td> </tr> <tr> <td style="text-align:left;"> Freshness: </td> <td style="text-align:left;"> Average </td> <td style="text-align:left;"> Excellent </td> <td style="text-align:left;"> Average </td> </tr> </tbody> </table> --- class: center ## .center[TABLE layout: Show options in a table with `kable()`] Your choice question can be defined in the `survey.qmd`<br>file as it is just choosing one of three options: <center> <img src="images/example-table.png" width=550> </center> --- class: inverse
−
+
20
:
00
## Your Turn With your team, choose a format to work with (buttons or table) for your choice question, then modify the example survey to match your project. ## Versions: ## Buttons layout: [demo-choice-based-conjoint](https://github.com/surveydown-dev/demo-choice-based-conjoint/archive/refs/heads/main.zip) ## Table layout: [demo-choice-based-conjoint-table](https://github.com/surveydown-dev/demo-choice-based-conjoint-table/archive/refs/heads/main.zip)