Sign of Cohen's d is unaffected by reversing order of factor levels in R












0















I'm using Cohen's d (implemented using cohen.d() from the effsize package) as a measure of effect size in my dependent variable between two levels of a factor.



My code looks like this: cohen.d(d, f) where d is a vector of numeric values and f is a factor with two levels: "A" and "B".



Based on my understanding, the sign of Cohen's d is dependent on the order of means (i.e. factor levels) entered into the formula. However, my cohen.d() command returns a negative value (and negative CIs), even if I reverse the order of levels in f.



Here is a reproducible example:



library('effsize')
# Load in Chickweight data
a=ChickWeight

# Cohens d requires two levels in factor f, so take the first two available in Diet
a=a[a$Diet==c(1,2),]
a$Diet=a$Diet[ , drop=T]

# Compute cohen's d with default order of Diet
d1 = a$weight
f1 = a$Diet
cohen1 = cohen.d(d1,f1)

# Re-order levels of Diet
a$Diet = relevel(a$Diet, ref=2)

# Re-compute cohen's d
d2 = a$weight
f2 = a$Diet
cohen2 = cohen.d(d2,f2)

# Compare values
cohen1
cohen2


Can anyone explain why this is the case, and/or if I'm doing something wrong?



Thanks in advance for any advice!










share|improve this question



























    0















    I'm using Cohen's d (implemented using cohen.d() from the effsize package) as a measure of effect size in my dependent variable between two levels of a factor.



    My code looks like this: cohen.d(d, f) where d is a vector of numeric values and f is a factor with two levels: "A" and "B".



    Based on my understanding, the sign of Cohen's d is dependent on the order of means (i.e. factor levels) entered into the formula. However, my cohen.d() command returns a negative value (and negative CIs), even if I reverse the order of levels in f.



    Here is a reproducible example:



    library('effsize')
    # Load in Chickweight data
    a=ChickWeight

    # Cohens d requires two levels in factor f, so take the first two available in Diet
    a=a[a$Diet==c(1,2),]
    a$Diet=a$Diet[ , drop=T]

    # Compute cohen's d with default order of Diet
    d1 = a$weight
    f1 = a$Diet
    cohen1 = cohen.d(d1,f1)

    # Re-order levels of Diet
    a$Diet = relevel(a$Diet, ref=2)

    # Re-compute cohen's d
    d2 = a$weight
    f2 = a$Diet
    cohen2 = cohen.d(d2,f2)

    # Compare values
    cohen1
    cohen2


    Can anyone explain why this is the case, and/or if I'm doing something wrong?



    Thanks in advance for any advice!










    share|improve this question

























      0












      0








      0








      I'm using Cohen's d (implemented using cohen.d() from the effsize package) as a measure of effect size in my dependent variable between two levels of a factor.



      My code looks like this: cohen.d(d, f) where d is a vector of numeric values and f is a factor with two levels: "A" and "B".



      Based on my understanding, the sign of Cohen's d is dependent on the order of means (i.e. factor levels) entered into the formula. However, my cohen.d() command returns a negative value (and negative CIs), even if I reverse the order of levels in f.



      Here is a reproducible example:



      library('effsize')
      # Load in Chickweight data
      a=ChickWeight

      # Cohens d requires two levels in factor f, so take the first two available in Diet
      a=a[a$Diet==c(1,2),]
      a$Diet=a$Diet[ , drop=T]

      # Compute cohen's d with default order of Diet
      d1 = a$weight
      f1 = a$Diet
      cohen1 = cohen.d(d1,f1)

      # Re-order levels of Diet
      a$Diet = relevel(a$Diet, ref=2)

      # Re-compute cohen's d
      d2 = a$weight
      f2 = a$Diet
      cohen2 = cohen.d(d2,f2)

      # Compare values
      cohen1
      cohen2


      Can anyone explain why this is the case, and/or if I'm doing something wrong?



      Thanks in advance for any advice!










      share|improve this question














      I'm using Cohen's d (implemented using cohen.d() from the effsize package) as a measure of effect size in my dependent variable between two levels of a factor.



      My code looks like this: cohen.d(d, f) where d is a vector of numeric values and f is a factor with two levels: "A" and "B".



      Based on my understanding, the sign of Cohen's d is dependent on the order of means (i.e. factor levels) entered into the formula. However, my cohen.d() command returns a negative value (and negative CIs), even if I reverse the order of levels in f.



      Here is a reproducible example:



      library('effsize')
      # Load in Chickweight data
      a=ChickWeight

      # Cohens d requires two levels in factor f, so take the first two available in Diet
      a=a[a$Diet==c(1,2),]
      a$Diet=a$Diet[ , drop=T]

      # Compute cohen's d with default order of Diet
      d1 = a$weight
      f1 = a$Diet
      cohen1 = cohen.d(d1,f1)

      # Re-order levels of Diet
      a$Diet = relevel(a$Diet, ref=2)

      # Re-compute cohen's d
      d2 = a$weight
      f2 = a$Diet
      cohen2 = cohen.d(d2,f2)

      # Compare values
      cohen1
      cohen2


      Can anyone explain why this is the case, and/or if I'm doing something wrong?



      Thanks in advance for any advice!







      r






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 14:07









      LyamLyam

      287




      287
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I'm not entirely sure what the reasoning behind the issue in your example is (maybe someone else can comment here), but if you look at the examples under ?cohen.d, there are a few different methods for calculating it:



          treatment = rnorm(100,mean=10)
          control = rnorm(100,mean=12)
          d = (c(treatment,control))
          f = rep(c("Treatment","Control"),each=100)
          ## compute Cohen's d
          ## treatment and control
          cohen.d(treatment,control)
          ## data and factor
          cohen.d(d,f)
          ## formula interface
          cohen.d(d ~ f)


          If you use the first example of cohen.d(treatment, control) and reverse that to cohen.d(control, treatment) you get the following:



          cohen.d(treatment, control)
          Cohen's d

          d estimate: -1.871982 (large)
          95 percent confidence interval:
          inf sup
          -2.206416 -1.537547

          cohen.d(control, treatment)
          Cohen's d

          d estimate: 1.871982 (large)
          95 percent confidence interval:
          inf sup
          1.537547 2.206416


          So using the two-vector method from the examples with your data, we can do:



          a1 <- a[a$Diet == 1,"weight"]
          a2 <- a[a$Diet == 2,"weight"]
          cohen3a <- cohen.d(a1, a2)
          cohen3b <- cohen.d(a2, a1)


          I noticed that f in the ?cohen.d examples is not a factor, but a character vector. I tried playing around with the cohen.d(d, f) method, but didn't find a solution. Would like to see if someone else has anything regarding that.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263872%2fsign-of-cohens-d-is-unaffected-by-reversing-order-of-factor-levels-in-r%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I'm not entirely sure what the reasoning behind the issue in your example is (maybe someone else can comment here), but if you look at the examples under ?cohen.d, there are a few different methods for calculating it:



            treatment = rnorm(100,mean=10)
            control = rnorm(100,mean=12)
            d = (c(treatment,control))
            f = rep(c("Treatment","Control"),each=100)
            ## compute Cohen's d
            ## treatment and control
            cohen.d(treatment,control)
            ## data and factor
            cohen.d(d,f)
            ## formula interface
            cohen.d(d ~ f)


            If you use the first example of cohen.d(treatment, control) and reverse that to cohen.d(control, treatment) you get the following:



            cohen.d(treatment, control)
            Cohen's d

            d estimate: -1.871982 (large)
            95 percent confidence interval:
            inf sup
            -2.206416 -1.537547

            cohen.d(control, treatment)
            Cohen's d

            d estimate: 1.871982 (large)
            95 percent confidence interval:
            inf sup
            1.537547 2.206416


            So using the two-vector method from the examples with your data, we can do:



            a1 <- a[a$Diet == 1,"weight"]
            a2 <- a[a$Diet == 2,"weight"]
            cohen3a <- cohen.d(a1, a2)
            cohen3b <- cohen.d(a2, a1)


            I noticed that f in the ?cohen.d examples is not a factor, but a character vector. I tried playing around with the cohen.d(d, f) method, but didn't find a solution. Would like to see if someone else has anything regarding that.






            share|improve this answer




























              0














              I'm not entirely sure what the reasoning behind the issue in your example is (maybe someone else can comment here), but if you look at the examples under ?cohen.d, there are a few different methods for calculating it:



              treatment = rnorm(100,mean=10)
              control = rnorm(100,mean=12)
              d = (c(treatment,control))
              f = rep(c("Treatment","Control"),each=100)
              ## compute Cohen's d
              ## treatment and control
              cohen.d(treatment,control)
              ## data and factor
              cohen.d(d,f)
              ## formula interface
              cohen.d(d ~ f)


              If you use the first example of cohen.d(treatment, control) and reverse that to cohen.d(control, treatment) you get the following:



              cohen.d(treatment, control)
              Cohen's d

              d estimate: -1.871982 (large)
              95 percent confidence interval:
              inf sup
              -2.206416 -1.537547

              cohen.d(control, treatment)
              Cohen's d

              d estimate: 1.871982 (large)
              95 percent confidence interval:
              inf sup
              1.537547 2.206416


              So using the two-vector method from the examples with your data, we can do:



              a1 <- a[a$Diet == 1,"weight"]
              a2 <- a[a$Diet == 2,"weight"]
              cohen3a <- cohen.d(a1, a2)
              cohen3b <- cohen.d(a2, a1)


              I noticed that f in the ?cohen.d examples is not a factor, but a character vector. I tried playing around with the cohen.d(d, f) method, but didn't find a solution. Would like to see if someone else has anything regarding that.






              share|improve this answer


























                0












                0








                0







                I'm not entirely sure what the reasoning behind the issue in your example is (maybe someone else can comment here), but if you look at the examples under ?cohen.d, there are a few different methods for calculating it:



                treatment = rnorm(100,mean=10)
                control = rnorm(100,mean=12)
                d = (c(treatment,control))
                f = rep(c("Treatment","Control"),each=100)
                ## compute Cohen's d
                ## treatment and control
                cohen.d(treatment,control)
                ## data and factor
                cohen.d(d,f)
                ## formula interface
                cohen.d(d ~ f)


                If you use the first example of cohen.d(treatment, control) and reverse that to cohen.d(control, treatment) you get the following:



                cohen.d(treatment, control)
                Cohen's d

                d estimate: -1.871982 (large)
                95 percent confidence interval:
                inf sup
                -2.206416 -1.537547

                cohen.d(control, treatment)
                Cohen's d

                d estimate: 1.871982 (large)
                95 percent confidence interval:
                inf sup
                1.537547 2.206416


                So using the two-vector method from the examples with your data, we can do:



                a1 <- a[a$Diet == 1,"weight"]
                a2 <- a[a$Diet == 2,"weight"]
                cohen3a <- cohen.d(a1, a2)
                cohen3b <- cohen.d(a2, a1)


                I noticed that f in the ?cohen.d examples is not a factor, but a character vector. I tried playing around with the cohen.d(d, f) method, but didn't find a solution. Would like to see if someone else has anything regarding that.






                share|improve this answer













                I'm not entirely sure what the reasoning behind the issue in your example is (maybe someone else can comment here), but if you look at the examples under ?cohen.d, there are a few different methods for calculating it:



                treatment = rnorm(100,mean=10)
                control = rnorm(100,mean=12)
                d = (c(treatment,control))
                f = rep(c("Treatment","Control"),each=100)
                ## compute Cohen's d
                ## treatment and control
                cohen.d(treatment,control)
                ## data and factor
                cohen.d(d,f)
                ## formula interface
                cohen.d(d ~ f)


                If you use the first example of cohen.d(treatment, control) and reverse that to cohen.d(control, treatment) you get the following:



                cohen.d(treatment, control)
                Cohen's d

                d estimate: -1.871982 (large)
                95 percent confidence interval:
                inf sup
                -2.206416 -1.537547

                cohen.d(control, treatment)
                Cohen's d

                d estimate: 1.871982 (large)
                95 percent confidence interval:
                inf sup
                1.537547 2.206416


                So using the two-vector method from the examples with your data, we can do:



                a1 <- a[a$Diet == 1,"weight"]
                a2 <- a[a$Diet == 2,"weight"]
                cohen3a <- cohen.d(a1, a2)
                cohen3b <- cohen.d(a2, a1)


                I noticed that f in the ?cohen.d examples is not a factor, but a character vector. I tried playing around with the cohen.d(d, f) method, but didn't find a solution. Would like to see if someone else has anything regarding that.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 '18 at 17:00









                QwfqwfQwfqwf

                1838




                1838






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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.


                    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%2fstackoverflow.com%2fquestions%2f53263872%2fsign-of-cohens-d-is-unaffected-by-reversing-order-of-factor-levels-in-r%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

                    Full-time equivalent

                    Bicuculline

                    さくらももこ