sta-363-s20 / community

Discussion, Q&A, everything you want to say, formatted nicely
1 stars 0 forks source link

Lab2Ex3 #36

Closed anishprasanna closed 4 years ago

anishprasanna commented 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!

Exercise 2

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)

Exercise 3

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))
LucyMcGowan commented 4 years ago

There’s a small mistake in your code - double check your variance equation (hint: there are just 2 classes in this example)

anishprasanna commented 4 years ago

Thanks! got it to work