Closed anishprasanna closed 4 years ago
I am confused as to why my posterior probabilities are not the same while classifying x=3. Its relatively close, but I was under the impression they were supposed to be the same. Thanks!
df %>% group_by(y) %>% summarise(n = n()) %>% mutate(pi = n / sum(n))%>% pull(pi) -> pi df %>% group_by(y) %>% summarise(mu = mean(x)) %>% pull(mu) -> mu df %>% group_by(y) %>% summarise(var_k = var(x), n = n()) %>% mutate(v = ((n - 1) / (sum(n) - 3)) * var_k) %>% summarise(sigma_sq = sum(v)) %>% pull(sigma_sq) -> sigma_sq z <- 3 z * (mu / sigma_sq) - mu^2 / (2 * sigma_sq) + log(pi)
z <- 3 d <- z * (mu / sigma_sq) - mu^2 / (2 * sigma_sq) + log(pi) exp(d) / sum(exp(d))
Posterior probability using MASS
library(MASS) model <- lda(y ~ x, data = df) predict(model, newdata = data.frame(z = 3))
There’s a small mistake in your code - double check your variance equation (hint: there are just 2 classes in this example)
Thanks! got it to work
I am confused as to why my posterior probabilities are not the same while classifying x=3. Its relatively close, but I was under the impression they were supposed to be the same. Thanks!
Exercise 2
Exercise 3
Posterior probability using MASS