class: center, middle, title-slide .title[ # Practical 2 ] .author[ ### Olivier Gimenez ] .date[ ### last updated: 2024-10-17 ] --- ## MLE of the parameters of a Normal distribution * Assume we have collected data on the height of 100 people: ``` r # set seed for random numbers set.seed(2020) # simulate data from Normal distribution n <- 100 height <- rnorm(n, mean=170, sd=10) ``` --- .center[ ![](practical2_files/figure-html/unnamed-chunk-2-1.svg)<!-- --> ] --- * We consider a Normal distribution for the model. * Compute the MLE of the parameters of the Normal distribution. * Hint: Use functions `optim()` and `dnorm()`. --- # Solution Function for likelihood of normal distribution w/ mean `\(\mu\)` and standard deviation `\(\sigma\)`: ``` r negloglik <- function(theta, data) { mu <- theta[1] sigma <- theta[2] x <- data -sum(dnorm(x, mean = mu, sd = sigma, log = TRUE)) } negloglik(theta = c(150,1), height) ``` ``` ## [1] 28530.45 ``` --- * Minimiiiiiize ``` r fit <- optim(par = c(1,1), fn = negloglik, data = height) fit ``` ``` ## $par ## [1] 171.05358 11.16656 ## ## $value ## [1] 382.9207 ## ## $counts ## function gradient ## 135 NA ## ## $convergence ## [1] 0 ## ## $message ## NULL ``` --- .center[ ![](practical2_files/figure-html/unnamed-chunk-5-1.svg)<!-- --> ]