Logistic regression BIC: what's the right N?












7












$begingroup$


TL;DR: Which $N$ is correct for BIC in logistic regression, the aggregated binomial or Bernoulli $N$?



Suppose I have a data set to which I'd like to apply logistic regression. For the sake of example, suppose there are $j=5$ groups with $m=100$ participants each, for a total $n=500$. The outcome is 0 or 1. For example, the following data set (R code):



library(dplyr)

set.seed(44)
d <- tibble(y = rbinom(500, 1, .5),
x = factor(rep(LETTERS[1:5], each = 100)))


There are two ways I can represent this: as is, above, treating every observation as a Bernoulli random variable, or aggregating observations within groups and treating each observation as Binomial. The number of rows in the data set will be 500 in the first instance, and 5 in the second.



I can construct the aggregated data set:



d %>% 
group_by(x, y) %>%
summarise(n = n()) %>%
spread(y, n) %>%
rename(f = `0`, s = `1`) %>%
mutate(n = s + f) -> d_agg


I can then fit the logistic regression using both data sets in R:



g_bern  <- glm(y ~ x,          data=d,     family=binomial)
g_binom <- glm(cbind(s,f) ~ x, data=d_agg, family=binomial)


and compute the AIC:



AIC(g_bern)   # [1] 693.8487
AIC(g_binom) # [1] 35.21523


which of course differ by a constant



2*sum(lchoose(d_agg$n, d_agg$s))  # [1] 658.6335


as expected (see: Logistic Regression: Bernoulli vs. Binomial Response Variables).



However, the BICs differ by that constant AND a factor that depends on the "number of observations", and the number of observations differ in each:



BIC(g_bern)    # [1] 714.9217
BIC(g_binom) # [1] 33.26242
nobs(g_bern) # [1] 500
nobs(g_binom) # [1] 5


Just to confirm, we can recalculate BIC for both:



-2*logLik(g_bern) + attr(logLik(g_bern),"df")*log(nobs(g_bern))
# 'log Lik.' 714.9217 (df=5)
-2*logLik(g_binom) + attr(logLik(g_binom),"df")*log(nobs(g_binom))
# 'log Lik.' 33.26242 (df=5)


and indeed the only place these two numbers differ is $N$.



This surprises me, since I would think that R would "know" which of the two to use to prevent ambiguity. It has the same information in both cases.



Which one is "right"? Or is BIC really this arbitrary?










share|cite|improve this question









New contributor




Salad dressing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    7












    $begingroup$


    TL;DR: Which $N$ is correct for BIC in logistic regression, the aggregated binomial or Bernoulli $N$?



    Suppose I have a data set to which I'd like to apply logistic regression. For the sake of example, suppose there are $j=5$ groups with $m=100$ participants each, for a total $n=500$. The outcome is 0 or 1. For example, the following data set (R code):



    library(dplyr)

    set.seed(44)
    d <- tibble(y = rbinom(500, 1, .5),
    x = factor(rep(LETTERS[1:5], each = 100)))


    There are two ways I can represent this: as is, above, treating every observation as a Bernoulli random variable, or aggregating observations within groups and treating each observation as Binomial. The number of rows in the data set will be 500 in the first instance, and 5 in the second.



    I can construct the aggregated data set:



    d %>% 
    group_by(x, y) %>%
    summarise(n = n()) %>%
    spread(y, n) %>%
    rename(f = `0`, s = `1`) %>%
    mutate(n = s + f) -> d_agg


    I can then fit the logistic regression using both data sets in R:



    g_bern  <- glm(y ~ x,          data=d,     family=binomial)
    g_binom <- glm(cbind(s,f) ~ x, data=d_agg, family=binomial)


    and compute the AIC:



    AIC(g_bern)   # [1] 693.8487
    AIC(g_binom) # [1] 35.21523


    which of course differ by a constant



    2*sum(lchoose(d_agg$n, d_agg$s))  # [1] 658.6335


    as expected (see: Logistic Regression: Bernoulli vs. Binomial Response Variables).



    However, the BICs differ by that constant AND a factor that depends on the "number of observations", and the number of observations differ in each:



    BIC(g_bern)    # [1] 714.9217
    BIC(g_binom) # [1] 33.26242
    nobs(g_bern) # [1] 500
    nobs(g_binom) # [1] 5


    Just to confirm, we can recalculate BIC for both:



    -2*logLik(g_bern) + attr(logLik(g_bern),"df")*log(nobs(g_bern))
    # 'log Lik.' 714.9217 (df=5)
    -2*logLik(g_binom) + attr(logLik(g_binom),"df")*log(nobs(g_binom))
    # 'log Lik.' 33.26242 (df=5)


    and indeed the only place these two numbers differ is $N$.



    This surprises me, since I would think that R would "know" which of the two to use to prevent ambiguity. It has the same information in both cases.



    Which one is "right"? Or is BIC really this arbitrary?










    share|cite|improve this question









    New contributor




    Salad dressing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      7












      7








      7


      2



      $begingroup$


      TL;DR: Which $N$ is correct for BIC in logistic regression, the aggregated binomial or Bernoulli $N$?



      Suppose I have a data set to which I'd like to apply logistic regression. For the sake of example, suppose there are $j=5$ groups with $m=100$ participants each, for a total $n=500$. The outcome is 0 or 1. For example, the following data set (R code):



      library(dplyr)

      set.seed(44)
      d <- tibble(y = rbinom(500, 1, .5),
      x = factor(rep(LETTERS[1:5], each = 100)))


      There are two ways I can represent this: as is, above, treating every observation as a Bernoulli random variable, or aggregating observations within groups and treating each observation as Binomial. The number of rows in the data set will be 500 in the first instance, and 5 in the second.



      I can construct the aggregated data set:



      d %>% 
      group_by(x, y) %>%
      summarise(n = n()) %>%
      spread(y, n) %>%
      rename(f = `0`, s = `1`) %>%
      mutate(n = s + f) -> d_agg


      I can then fit the logistic regression using both data sets in R:



      g_bern  <- glm(y ~ x,          data=d,     family=binomial)
      g_binom <- glm(cbind(s,f) ~ x, data=d_agg, family=binomial)


      and compute the AIC:



      AIC(g_bern)   # [1] 693.8487
      AIC(g_binom) # [1] 35.21523


      which of course differ by a constant



      2*sum(lchoose(d_agg$n, d_agg$s))  # [1] 658.6335


      as expected (see: Logistic Regression: Bernoulli vs. Binomial Response Variables).



      However, the BICs differ by that constant AND a factor that depends on the "number of observations", and the number of observations differ in each:



      BIC(g_bern)    # [1] 714.9217
      BIC(g_binom) # [1] 33.26242
      nobs(g_bern) # [1] 500
      nobs(g_binom) # [1] 5


      Just to confirm, we can recalculate BIC for both:



      -2*logLik(g_bern) + attr(logLik(g_bern),"df")*log(nobs(g_bern))
      # 'log Lik.' 714.9217 (df=5)
      -2*logLik(g_binom) + attr(logLik(g_binom),"df")*log(nobs(g_binom))
      # 'log Lik.' 33.26242 (df=5)


      and indeed the only place these two numbers differ is $N$.



      This surprises me, since I would think that R would "know" which of the two to use to prevent ambiguity. It has the same information in both cases.



      Which one is "right"? Or is BIC really this arbitrary?










      share|cite|improve this question









      New contributor




      Salad dressing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      TL;DR: Which $N$ is correct for BIC in logistic regression, the aggregated binomial or Bernoulli $N$?



      Suppose I have a data set to which I'd like to apply logistic regression. For the sake of example, suppose there are $j=5$ groups with $m=100$ participants each, for a total $n=500$. The outcome is 0 or 1. For example, the following data set (R code):



      library(dplyr)

      set.seed(44)
      d <- tibble(y = rbinom(500, 1, .5),
      x = factor(rep(LETTERS[1:5], each = 100)))


      There are two ways I can represent this: as is, above, treating every observation as a Bernoulli random variable, or aggregating observations within groups and treating each observation as Binomial. The number of rows in the data set will be 500 in the first instance, and 5 in the second.



      I can construct the aggregated data set:



      d %>% 
      group_by(x, y) %>%
      summarise(n = n()) %>%
      spread(y, n) %>%
      rename(f = `0`, s = `1`) %>%
      mutate(n = s + f) -> d_agg


      I can then fit the logistic regression using both data sets in R:



      g_bern  <- glm(y ~ x,          data=d,     family=binomial)
      g_binom <- glm(cbind(s,f) ~ x, data=d_agg, family=binomial)


      and compute the AIC:



      AIC(g_bern)   # [1] 693.8487
      AIC(g_binom) # [1] 35.21523


      which of course differ by a constant



      2*sum(lchoose(d_agg$n, d_agg$s))  # [1] 658.6335


      as expected (see: Logistic Regression: Bernoulli vs. Binomial Response Variables).



      However, the BICs differ by that constant AND a factor that depends on the "number of observations", and the number of observations differ in each:



      BIC(g_bern)    # [1] 714.9217
      BIC(g_binom) # [1] 33.26242
      nobs(g_bern) # [1] 500
      nobs(g_binom) # [1] 5


      Just to confirm, we can recalculate BIC for both:



      -2*logLik(g_bern) + attr(logLik(g_bern),"df")*log(nobs(g_bern))
      # 'log Lik.' 714.9217 (df=5)
      -2*logLik(g_binom) + attr(logLik(g_binom),"df")*log(nobs(g_binom))
      # 'log Lik.' 33.26242 (df=5)


      and indeed the only place these two numbers differ is $N$.



      This surprises me, since I would think that R would "know" which of the two to use to prevent ambiguity. It has the same information in both cases.



      Which one is "right"? Or is BIC really this arbitrary?







      r logistic generalized-linear-model model-comparison bic






      share|cite|improve this question









      New contributor




      Salad dressing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|cite|improve this question









      New contributor




      Salad dressing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|cite|improve this question




      share|cite|improve this question








      edited 7 hours ago









      gung

      108k34263527




      108k34263527






      New contributor




      Salad dressing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 9 hours ago









      Salad dressingSalad dressing

      361




      361




      New contributor




      Salad dressing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Salad dressing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Salad dressing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          5












          $begingroup$

          The BIC (and the AIC) are relative measures for comparing models. However, it makes no sense to compare what is otherwise the same model between using an aggregated vs. a disaggregated response. Nor would it make sense to compare models that would otherwise be different (e.g., different regressors), but where one model uses an aggregated response and the other model uses a disaggregated version of the response. As long as the two models being compared both represent the response variable in the same format, everything will be fine. Note that the two formats are ultimately equivalent—they contain the same information and mostly just look different on the outside, see: Input format for response in binomial glm in R.






          share|cite|improve this answer









          $endgroup$





















            2












            $begingroup$

            Interesting question! Coming at this from an applied setting, I think you need to remember that both BIC and AIC are measures of relative model fit.



            In other words, these measures don't tell you much when you examine them for a single model, but can help you to select an appropriate model among a set of competing models. In particular:




            1. If your goal is to find the 'best' among those competing models for prediction of the outcome variable, then select the model with the lowest AIC value;

            2. If your goal is to find the 'best' among those competing models for understanding and describing the effects of the predictor variables included in the model on the outcome variable, then select the model with the lowest BIC value.


            In defining your set of competing models, you would have to make sure the models follow the same conceptual framework. Thus, you would either compare several binomial logistic regression models or several binary logistic models, but not a mixture of both. (It is important to compare like with like, otherwise you won't know if a model won the competition based on its own merits or simply because you changed the model specification/fitting procedure.)



            From this perspective, the only thing that matters is that R is consistent when computing the AIC and BIC across models of the same type (e.g., binomial logistic regression models).



            Just to clarify: g_bern is a binary logistic regression model, whereas g_binom is a binomial logistic regression model. While they both model the probability of success in one trial, you wouldn't mix together variations of these models when defining your set of competing models (for the reasons explained above and also covered by @gung).






            share|cite|improve this answer











            $endgroup$













              Your Answer





              StackExchange.ifUsing("editor", function () {
              return StackExchange.using("mathjaxEditing", function () {
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
              });
              });
              }, "mathjax-editing");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "65"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });






              Salad dressing is a new contributor. Be nice, and check out our Code of Conduct.










              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstats.stackexchange.com%2fquestions%2f396545%2flogistic-regression-bic-whats-the-right-n%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5












              $begingroup$

              The BIC (and the AIC) are relative measures for comparing models. However, it makes no sense to compare what is otherwise the same model between using an aggregated vs. a disaggregated response. Nor would it make sense to compare models that would otherwise be different (e.g., different regressors), but where one model uses an aggregated response and the other model uses a disaggregated version of the response. As long as the two models being compared both represent the response variable in the same format, everything will be fine. Note that the two formats are ultimately equivalent—they contain the same information and mostly just look different on the outside, see: Input format for response in binomial glm in R.






              share|cite|improve this answer









              $endgroup$


















                5












                $begingroup$

                The BIC (and the AIC) are relative measures for comparing models. However, it makes no sense to compare what is otherwise the same model between using an aggregated vs. a disaggregated response. Nor would it make sense to compare models that would otherwise be different (e.g., different regressors), but where one model uses an aggregated response and the other model uses a disaggregated version of the response. As long as the two models being compared both represent the response variable in the same format, everything will be fine. Note that the two formats are ultimately equivalent—they contain the same information and mostly just look different on the outside, see: Input format for response in binomial glm in R.






                share|cite|improve this answer









                $endgroup$
















                  5












                  5








                  5





                  $begingroup$

                  The BIC (and the AIC) are relative measures for comparing models. However, it makes no sense to compare what is otherwise the same model between using an aggregated vs. a disaggregated response. Nor would it make sense to compare models that would otherwise be different (e.g., different regressors), but where one model uses an aggregated response and the other model uses a disaggregated version of the response. As long as the two models being compared both represent the response variable in the same format, everything will be fine. Note that the two formats are ultimately equivalent—they contain the same information and mostly just look different on the outside, see: Input format for response in binomial glm in R.






                  share|cite|improve this answer









                  $endgroup$



                  The BIC (and the AIC) are relative measures for comparing models. However, it makes no sense to compare what is otherwise the same model between using an aggregated vs. a disaggregated response. Nor would it make sense to compare models that would otherwise be different (e.g., different regressors), but where one model uses an aggregated response and the other model uses a disaggregated version of the response. As long as the two models being compared both represent the response variable in the same format, everything will be fine. Note that the two formats are ultimately equivalent—they contain the same information and mostly just look different on the outside, see: Input format for response in binomial glm in R.







                  share|cite|improve this answer












                  share|cite|improve this answer



                  share|cite|improve this answer










                  answered 7 hours ago









                  gunggung

                  108k34263527




                  108k34263527

























                      2












                      $begingroup$

                      Interesting question! Coming at this from an applied setting, I think you need to remember that both BIC and AIC are measures of relative model fit.



                      In other words, these measures don't tell you much when you examine them for a single model, but can help you to select an appropriate model among a set of competing models. In particular:




                      1. If your goal is to find the 'best' among those competing models for prediction of the outcome variable, then select the model with the lowest AIC value;

                      2. If your goal is to find the 'best' among those competing models for understanding and describing the effects of the predictor variables included in the model on the outcome variable, then select the model with the lowest BIC value.


                      In defining your set of competing models, you would have to make sure the models follow the same conceptual framework. Thus, you would either compare several binomial logistic regression models or several binary logistic models, but not a mixture of both. (It is important to compare like with like, otherwise you won't know if a model won the competition based on its own merits or simply because you changed the model specification/fitting procedure.)



                      From this perspective, the only thing that matters is that R is consistent when computing the AIC and BIC across models of the same type (e.g., binomial logistic regression models).



                      Just to clarify: g_bern is a binary logistic regression model, whereas g_binom is a binomial logistic regression model. While they both model the probability of success in one trial, you wouldn't mix together variations of these models when defining your set of competing models (for the reasons explained above and also covered by @gung).






                      share|cite|improve this answer











                      $endgroup$


















                        2












                        $begingroup$

                        Interesting question! Coming at this from an applied setting, I think you need to remember that both BIC and AIC are measures of relative model fit.



                        In other words, these measures don't tell you much when you examine them for a single model, but can help you to select an appropriate model among a set of competing models. In particular:




                        1. If your goal is to find the 'best' among those competing models for prediction of the outcome variable, then select the model with the lowest AIC value;

                        2. If your goal is to find the 'best' among those competing models for understanding and describing the effects of the predictor variables included in the model on the outcome variable, then select the model with the lowest BIC value.


                        In defining your set of competing models, you would have to make sure the models follow the same conceptual framework. Thus, you would either compare several binomial logistic regression models or several binary logistic models, but not a mixture of both. (It is important to compare like with like, otherwise you won't know if a model won the competition based on its own merits or simply because you changed the model specification/fitting procedure.)



                        From this perspective, the only thing that matters is that R is consistent when computing the AIC and BIC across models of the same type (e.g., binomial logistic regression models).



                        Just to clarify: g_bern is a binary logistic regression model, whereas g_binom is a binomial logistic regression model. While they both model the probability of success in one trial, you wouldn't mix together variations of these models when defining your set of competing models (for the reasons explained above and also covered by @gung).






                        share|cite|improve this answer











                        $endgroup$
















                          2












                          2








                          2





                          $begingroup$

                          Interesting question! Coming at this from an applied setting, I think you need to remember that both BIC and AIC are measures of relative model fit.



                          In other words, these measures don't tell you much when you examine them for a single model, but can help you to select an appropriate model among a set of competing models. In particular:




                          1. If your goal is to find the 'best' among those competing models for prediction of the outcome variable, then select the model with the lowest AIC value;

                          2. If your goal is to find the 'best' among those competing models for understanding and describing the effects of the predictor variables included in the model on the outcome variable, then select the model with the lowest BIC value.


                          In defining your set of competing models, you would have to make sure the models follow the same conceptual framework. Thus, you would either compare several binomial logistic regression models or several binary logistic models, but not a mixture of both. (It is important to compare like with like, otherwise you won't know if a model won the competition based on its own merits or simply because you changed the model specification/fitting procedure.)



                          From this perspective, the only thing that matters is that R is consistent when computing the AIC and BIC across models of the same type (e.g., binomial logistic regression models).



                          Just to clarify: g_bern is a binary logistic regression model, whereas g_binom is a binomial logistic regression model. While they both model the probability of success in one trial, you wouldn't mix together variations of these models when defining your set of competing models (for the reasons explained above and also covered by @gung).






                          share|cite|improve this answer











                          $endgroup$



                          Interesting question! Coming at this from an applied setting, I think you need to remember that both BIC and AIC are measures of relative model fit.



                          In other words, these measures don't tell you much when you examine them for a single model, but can help you to select an appropriate model among a set of competing models. In particular:




                          1. If your goal is to find the 'best' among those competing models for prediction of the outcome variable, then select the model with the lowest AIC value;

                          2. If your goal is to find the 'best' among those competing models for understanding and describing the effects of the predictor variables included in the model on the outcome variable, then select the model with the lowest BIC value.


                          In defining your set of competing models, you would have to make sure the models follow the same conceptual framework. Thus, you would either compare several binomial logistic regression models or several binary logistic models, but not a mixture of both. (It is important to compare like with like, otherwise you won't know if a model won the competition based on its own merits or simply because you changed the model specification/fitting procedure.)



                          From this perspective, the only thing that matters is that R is consistent when computing the AIC and BIC across models of the same type (e.g., binomial logistic regression models).



                          Just to clarify: g_bern is a binary logistic regression model, whereas g_binom is a binomial logistic regression model. While they both model the probability of success in one trial, you wouldn't mix together variations of these models when defining your set of competing models (for the reasons explained above and also covered by @gung).







                          share|cite|improve this answer














                          share|cite|improve this answer



                          share|cite|improve this answer








                          edited 6 hours ago

























                          answered 7 hours ago









                          Isabella GhementIsabella Ghement

                          7,206320




                          7,206320






















                              Salad dressing is a new contributor. Be nice, and check out our Code of Conduct.










                              draft saved

                              draft discarded


















                              Salad dressing is a new contributor. Be nice, and check out our Code of Conduct.













                              Salad dressing is a new contributor. Be nice, and check out our Code of Conduct.












                              Salad dressing is a new contributor. Be nice, and check out our Code of Conduct.
















                              Thanks for contributing an answer to Cross Validated!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              Use MathJax to format equations. MathJax reference.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstats.stackexchange.com%2fquestions%2f396545%2flogistic-regression-bic-whats-the-right-n%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              宮崎県

                              濃尾地震

                              シテ島