class: middle, inverse .leftcol30[ <center> <img src="https://github.com/emse-madd-gwu/emse-madd-gwu.github.io/raw/master/images/logo.png" width=250> </center> ] .rightcol70[ # Week 8: .fancy[Optimization & MLE] ###
EMSE 6035: Marketing Analytics for Design Decisions ###
John Paul Helveston ###
October 19, 2022 ] --- class: inverse, middle # Week 8: .fancy[Optimization & MLE] ### 1. Maximum likelihood estimation ### 2. Optimization (in general) ### BREAK ### 3. Joins ### 4. Pilot data cleaning --- class: inverse, middle # Week 8: .fancy[Optimization & MLE] ### 1. .orange[Maximum likelihood estimation] ### 2. Optimization (in general) ### BREAK ### 3. Joins ### 4. Pilot data cleaning --- background-color: #EEEDEE ## .center[Computing the likelihood] .leftcol[ <center> <img src="images/pdf.png" width=100%> </center> ] .rightcol[ `\(x\)`: an observation `\(f(x)\)`: probability of observing `\(x\)` ] --- background-color: #EEEDEE ## .center[Computing the likelihood] .leftcol[ <center> <img src="images/pdf.png" width=100%> </center> ] .rightcol[ `\(x\)`: an observation `\(f(x)\)`: probability of observing `\(x\)` `\(\mathcal{L}(\theta | x)\)`: probability that `\(\theta\)` are the true parameters, given that observed `\(x\)` **We want to estimate `\(\theta\)`** ] --- background-color: #EEEDEE class: center ## We actually compute the _log_-likelihood<br>(converts multiplication to addition) <center> <img src="images/logl.png" width=700> </center> --- class: inverse # Practice Question 1 **Observations** - Height of students (inches): ``` #> [1] 65 69 66 67 68 72 68 69 63 70 ``` a) Let's say we know that the height of students, `\(\tilde{x}\)`, in a classroom follows a normal distribution. A professor obtains the above height measurements students in her classroom. What is the log-likelihood that `\(\tilde{x} \sim \mathcal{N} (68, 4)\)`? In other words, compute `\(\ln \mathcal{L} (\mu = 68, \sigma = 4)\)`. -- b) Compute the log-likelihood function using the same standard deviation `\((\sigma = 4)\)` but with the following different values for the mean, `\(\mu: 66, 67, 68, 69, 70\)`. How do the results compare? Which value for `\(\mu\)` produces the highest log-likelihood? --- class: inverse, middle # Week 8: .fancy[Optimization & MLE] ### 1. Maximum likelihood estimation ### 2. .orange[Optimization (in general)] ### BREAK ### 3. Joins ### 4. Pilot data cleaning --- background-color: #EEEDEE class: center, middle .leftcol40[ # `\(f(x)\)` ] .rightcol60[ <center> <img src="images/fx.png" width=100%> </center> ] --- background-color: #EEEDEE class: center, middle .leftcol40[ <center> <img src="images/first_order.png" width=100%> </center> ] .rightcol60[ <center> <img src="images/fx.png" width=100%> </center> ] --- background-color: #EEEDEE class: center, middle .leftcol40[ <center> <img src="images/second_order.png" width=100%> </center> ] .rightcol60[ <center> <img src="images/fx.png" width=100%> </center> ] --- background-color: #EEEDEE class: center, middle <center> <img src="images/conditions.png" width=1000> </center> --- background-color: #EEEDEE class: center, middle <center> <img src="images/algorithms.png" width=1200> </center> --- class: inverse # Practice Question 2 .leftcol80[ Consider the following function: `$$f(x) = x^2 - 6x$$` The gradient is: `$$\nabla f(x) = 2x - 6$$` Using the starting point `\(x = 1\)` and the step size `\(\gamma = 0.3\)`, apply the gradient descent method to compute the next **three** points in the search algorithm. ] --- background-color: #EEEDEE class: center, middle <center> <img src="images/conditions.png" width=1000> </center> --- class: inverse # Practice Question 3 .leftcol80[ Consider the following function: $$ f(\underline{x}) = x_1^2 + 4x_2^2 $$ The gradient is: $$ \nabla f(\underline{x}) = `\begin{bmatrix} 2x_1 \\ 8x_2 \end{bmatrix}` $$ Using the starting point `\(\underline{x}_0 = [1, 1]\)` and the step size `\(\gamma = 0.15\)`, apply the gradient descent method to compute the next **three** points in the search algorithm. ] --- class: center ## Download the [logitr-cars](https://github.com/emse-madd-gwu/logitr-cars) repo from GitHub <center> <img src="images/logitr-cars.png" width=900> </center> --- # .center[Estimating utility models] <br> .rightcol80[ ## 1. Open `logitr-cars.Rproj` ## 2. Open `code/3.1-model-mnl.R` ] --- background-color: #EEEDEE # Maximum likelihood estimation .leftcol[ <center> <img src="images/mle1.png" width=100%> </center> ] .rightcol[ <center> <img src="images/mle2.png" width=100%> </center> ] --- class: inverse, center # .fancy[Break]
05
:
00
--- class: inverse, middle # Week 8: .fancy[Optimization & MLE] ### 1. Maximum likelihood estimation ### 2. Optimization (in general) ### BREAK ### 3. .orange[Joins] ### 4. Pilot data cleaning --- class: center ## What's wrong with this map? <center> <img src="images/join_fail.png" height=500> </center> --- ### Likely culprit: Merging two columns .leftcol[ ```r head(names) ``` ``` #> state_name #> 1 Alabama #> 2 Alaska #> 3 Arizona #> 4 Arkansas #> 5 Armed Forces Africa #> 6 Armed Forces Americas ``` ```r head(abbs) ``` ``` #> state_abb #> 1 AA #> 2 AE #> 3 AE #> 4 AE #> 5 AE #> 6 AK ``` ] -- .rightcol[ ```r result <- cbind(names, abbs) head(result) ``` ``` #> state_name state_abb #> 1 Alabama AA #> 2 Alaska AE #> 3 Arizona AE #> 4 Arkansas AE #> 5 Armed Forces Africa AE #> 6 Armed Forces Americas AK ``` ] --- ## Joins 1. `inner_join()` 2. `left_join()` / `right_join()` 3. `full_join()` -- ‍Example: `band_members` & `band_instruments` .leftcol[ ```r band_members ``` ``` #> # A tibble: 3 × 2 #> name band #> <chr> <chr> #> 1 Mick Stones #> 2 John Beatles #> 3 Paul Beatles ``` ] .rightcol[ ```r band_instruments ``` ``` #> # A tibble: 3 × 2 #> name plays #> <chr> <chr> #> 1 John guitar #> 2 Paul bass #> 3 Keith guitar ``` ] --- .leftcol[ ## `inner_join()` ```r band_members %>% inner_join(band_instruments) ``` ``` #> # A tibble: 2 × 3 #> name band plays #> <chr> <chr> <chr> #> 1 John Beatles guitar #> 2 Paul Beatles bass ``` ] .rightcol[ <br> <center> <img src="images/inner_join.gif"> </center> ] --- .leftcol[ ## `full_join()` ```r band_members %>% full_join(band_instruments) ``` ``` #> # A tibble: 4 × 3 #> name band plays #> <chr> <chr> <chr> #> 1 Mick Stones <NA> #> 2 John Beatles guitar #> 3 Paul Beatles bass #> 4 Keith <NA> guitar ``` ] .rightcol[ <br> <center> <img src="images/full_join.gif"> </center> ] --- .leftcol[ ## `left_join()` ```r band_members %>% left_join(band_instruments) ``` ``` #> # A tibble: 3 × 3 #> name band plays #> <chr> <chr> <chr> #> 1 Mick Stones <NA> #> 2 John Beatles guitar #> 3 Paul Beatles bass ``` ] .rightcol[ <br> <center> <img src="images/left_join.gif"> </center> ] --- .leftcol[ ## `right_join()` ```r band_members %>% right_join(band_instruments) ``` ``` #> # A tibble: 3 × 3 #> name band plays #> <chr> <chr> <chr> #> 1 John Beatles guitar #> 2 Paul Beatles bass #> 3 Keith <NA> guitar ``` ] .rightcol[ <br> <center> <img src="images/right_join.gif"> </center> ] --- ## Specify the joining variable name .leftcol[ ```r band_members %>% left_join(band_instruments) ``` ``` #> Joining, by = "name" ``` ``` #> # A tibble: 3 × 3 #> name band plays #> <chr> <chr> <chr> #> 1 Mick Stones <NA> #> 2 John Beatles guitar #> 3 Paul Beatles bass ``` ] -- .rightcol[ ```r band_members %>% left_join(band_instruments, * by = 'name') ``` ``` #> # A tibble: 3 × 3 #> name band plays #> <chr> <chr> <chr> #> 1 Mick Stones <NA> #> 2 John Beatles guitar #> 3 Paul Beatles bass ``` ] --- ## Specify the joining variable name If the names differ, use `by = c("left_name" = "joining_name")` -- .leftcol[ ```r band_members ``` ``` #> # A tibble: 3 × 2 #> name band #> <chr> <chr> #> 1 Mick Stones #> 2 John Beatles #> 3 Paul Beatles ``` ```r band_instruments2 ``` ``` #> # A tibble: 3 × 2 #> artist plays #> <chr> <chr> #> 1 John guitar #> 2 Paul bass #> 3 Keith guitar ``` ] -- .rightcol[ ```r band_members %>% left_join(band_instruments2, * by = c("name" = "artist")) ``` ``` #> # A tibble: 3 × 3 #> name band plays #> <chr> <chr> <chr> #> 1 Mick Stones <NA> #> 2 John Beatles guitar #> 3 Paul Beatles bass ``` ] --- ## Specify the joining variable name Or just rename the joining variable in a pipe .leftcol[ ```r band_members ``` ``` #> # A tibble: 3 × 2 #> name band #> <chr> <chr> #> 1 Mick Stones #> 2 John Beatles #> 3 Paul Beatles ``` ```r band_instruments2 ``` ``` #> # A tibble: 3 × 2 #> artist plays #> <chr> <chr> #> 1 John guitar #> 2 Paul bass #> 3 Keith guitar ``` ] .rightcol[ ```r band_members %>% * rename(artist = name) %>% left_join(band_instruments2, * by = "artist") ``` ``` #> # A tibble: 3 × 3 #> artist band plays #> <chr> <chr> <chr> #> 1 Mick Stones <NA> #> 2 John Beatles guitar #> 3 Paul Beatles bass ``` ] --- class: inverse
15
:
00
## Your turn .leftcol[ 1. Write code to read in the `state_abbs.csv` and `state_regions.csv` data files in the "data" folder. 2. Create a new data frame called `states` by joining the two data frames `states_abbs` and `state_regions` together. The result should be a data frame with variables `region`, `name`, `abb`. ] .rightcol[ Your result should look like this: ```r head(states) ``` ``` #> # A tibble: 6 × 3 #> region name abb #> <chr> <chr> <chr> #> 1 Northeast Maine ME #> 2 Northeast New Hampshire NH #> 3 Northeast Vermont VT #> 4 Northeast Massachusetts MA #> 5 Northeast Rhode Island RI #> 6 Northeast Connecticut CT ``` ] --- class: inverse, middle # Week 8: .fancy[Optimization & MLE] ### 1. Maximum likelihood estimation ### 2. Optimization (in general) ### BREAK ### 3. Joins ### 4. .orange[Pilot data cleaning] --- class: center ## Download the [formr4conjoint](https://github.com/jhelvy/formr4conjoint) repo from GitHub <center> <img src="images/formr4conjoint.png" width=900> </center> --- # .center[Cleaning formr survey data] <br> .rightcol80[ ## 1. Open `formr4conjoint.Rproj` ## 2. Open `code/data_cleaning.R` ] --- class: inverse ## Team time .leftcol80[ ### For the rest of class, work with your team mates to start importing and cleaning your pilot survey data ]