3
$\begingroup$

I have an internal model that I unfortunately cannot share, but I've reproduced the issue below. My model has a binary target of 0 and 1. After running the predict function with type = "response", the values are still greater than 1 or less than 0 for some observations:

require(tidyverse)
data("iris")
iris
iris.model <- iris %>%
  mutate(isSetosa = case_when(Species == "setosa" ~ 1,
                             TRUE ~ 0)) %>%
  select(-Species)

mymodel <- glm(isSetosa ~ ., data = iris.model)
pred <- as.data.frame(predict(mymodel, type = "response")) %>%
  rename("preds" = 1)  %>%
  mutate(newpred = exp(preds)/(1+exp(preds)))

head(pred)

      preds   newpred
1 0.9789278 0.7268954
2 0.8442979 0.6993696
3 0.9021272 0.7113864
4 0.8263080 0.6955737
5 0.9966096 0.7303915
6 1.0169842 0.7343847

Given the fitted values are the same whether I choose "link" or "response" for type, I manually calculated the probabilities for each observation, but they seem lower than I would expect for the iris dataset.

What am I doing wrong here?

$\endgroup$
4
  • 7
    $\begingroup$ You forgot the family argument in your glm call. I do that all the time. $\endgroup$ Oct 5, 2020 at 21:02
  • 3
    $\begingroup$ This question may be simple, but it is not off-topic. "It needs statistical expertise to understand or answer", which is a direct quote from our on-topic page. I don't see someone at StackOverflow understanding that you need to specify the family=binomial argument. So let's reopen and write a short answer. $\endgroup$ Oct 5, 2020 at 21:08
  • $\begingroup$ Thanks, all, for the sanity check. I figured it would be something simple. $\endgroup$
    – Geoff Koch
    Oct 6, 2020 at 12:07
  • 2
    $\begingroup$ For future reference, when you post here, you would do best to use base R code. Not everyone will know how to read tidyverse type code. $\endgroup$ Oct 7, 2020 at 19:32

1 Answer 1

2
$\begingroup$

You forgot the family argument in the glm call. You probably wanted family=binomial or family= binomial(link = "logit").

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.