difference in test statistic and p-value R and Python script Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar ManaraADF: Reject or keep null hypothesis (difference p-value & test statistic)What is the difference between $t$-statistic and test-statistic?Hypothesis testing: t-test and p-value conflictWhich test to calculate the p-value?Conflicting results of summary() and anova() for a mixed model with interactions in lmer+lmerTestks_2samp test in Python scipy - low D statistic, low p-value?Calculating a p-value from the t-statistic of a two sided test?P Value query, Independent t-testReproducing t-test in R gives different result than built-in functionHow are degrees of freedom used in the Welch's t-test to determine p-value?

Putting Ant-Man on house arrest

Marquee sign letters

Why I cannot instantiate a class whose constructor is private in a friend class?

Was there ever a LEGO store in Miami International Airport?

Coin Game with infinite paradox

RIP Packet Format

What is ls Largest Number Formed by only moving two sticks in 508?

Is it accepted to use working hours to read general interest books?

Are these square matrices always diagonalisable?

false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'

How was Lagrange appointed professor of mathematics so early?

Suing a Police Officer Instead of the Police Department

Stretch a Tikz tree

What does the black goddess statue do and what is it?

Is there a possibility to generate a list dynamically in Latex?

In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω

How would you suggest I follow up with coworkers about our deadline that's today?

France's Public Holidays' Puzzle

/bin/ls sorts differently than just ls

Retract an already submitted Recommendation Letter (written for an undergrad student)

Will I lose my paid in full property

Why doesn't the university give past final exams' answers?

Protagonist's race is hidden - should I reveal it?

Why isn't everyone flabbergasted about Bran's "gift"?



difference in test statistic and p-value R and Python script



Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar ManaraADF: Reject or keep null hypothesis (difference p-value & test statistic)What is the difference between $t$-statistic and test-statistic?Hypothesis testing: t-test and p-value conflictWhich test to calculate the p-value?Conflicting results of summary() and anova() for a mixed model with interactions in lmer+lmerTestks_2samp test in Python scipy - low D statistic, low p-value?Calculating a p-value from the t-statistic of a two sided test?P Value query, Independent t-testReproducing t-test in R gives different result than built-in functionHow are degrees of freedom used in the Welch's t-test to determine p-value?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2












$begingroup$


I am trying to compute welch's t-test statistic and p-value manually for educational purpose.



I have written a function for welch's t-test as follows,



def welch_t_test(vec_a, vec_b):
mean_a = vec_a.mean()
mean_b = vec_b.mean()
var_a = vec_a.var()
var_b = vec_b.var()

T = (mean_a - mean_b)/((var_a-vec_a.size) + (var_b-vec_b.size))
return T


This function returns T-test statistic. To find p-value, I am using pt function from R-3.3.3.



My example inputs:



A = [6,3,0,7,7,0,5,7,7,6]
B = [10,18,14,18,12,13,15,18,16,18]


My test statisitc is:



Test statistic: -7.065217391304347


When I try to compute p-value using pt of R like pt(-7.06521,19) I get 5.038877e-07 whereas My Rs defualt t.test function result yields test statistic of -8.1321 and p-value of 1.95e07. I see the qualitatively both are similar but there exists a quantitative difference. Could someone point out why is that so? And also df in R shows some fractional number, in my case df of R is 17.987 which I suppose 19 following n-1 formula for degrees of freedom.










share|cite|improve this question









$endgroup$







  • 1




    $begingroup$
    At first glance--In your first block of code, I think your denominator for the Welch t statistic is incorrect. I will give it a try. It's always a good idea to make sure software is giving answers that agree with known formulas.
    $endgroup$
    – BruceET
    Apr 19 at 8:07


















2












$begingroup$


I am trying to compute welch's t-test statistic and p-value manually for educational purpose.



I have written a function for welch's t-test as follows,



def welch_t_test(vec_a, vec_b):
mean_a = vec_a.mean()
mean_b = vec_b.mean()
var_a = vec_a.var()
var_b = vec_b.var()

T = (mean_a - mean_b)/((var_a-vec_a.size) + (var_b-vec_b.size))
return T


This function returns T-test statistic. To find p-value, I am using pt function from R-3.3.3.



My example inputs:



A = [6,3,0,7,7,0,5,7,7,6]
B = [10,18,14,18,12,13,15,18,16,18]


My test statisitc is:



Test statistic: -7.065217391304347


When I try to compute p-value using pt of R like pt(-7.06521,19) I get 5.038877e-07 whereas My Rs defualt t.test function result yields test statistic of -8.1321 and p-value of 1.95e07. I see the qualitatively both are similar but there exists a quantitative difference. Could someone point out why is that so? And also df in R shows some fractional number, in my case df of R is 17.987 which I suppose 19 following n-1 formula for degrees of freedom.










share|cite|improve this question









$endgroup$







  • 1




    $begingroup$
    At first glance--In your first block of code, I think your denominator for the Welch t statistic is incorrect. I will give it a try. It's always a good idea to make sure software is giving answers that agree with known formulas.
    $endgroup$
    – BruceET
    Apr 19 at 8:07














2












2








2





$begingroup$


I am trying to compute welch's t-test statistic and p-value manually for educational purpose.



I have written a function for welch's t-test as follows,



def welch_t_test(vec_a, vec_b):
mean_a = vec_a.mean()
mean_b = vec_b.mean()
var_a = vec_a.var()
var_b = vec_b.var()

T = (mean_a - mean_b)/((var_a-vec_a.size) + (var_b-vec_b.size))
return T


This function returns T-test statistic. To find p-value, I am using pt function from R-3.3.3.



My example inputs:



A = [6,3,0,7,7,0,5,7,7,6]
B = [10,18,14,18,12,13,15,18,16,18]


My test statisitc is:



Test statistic: -7.065217391304347


When I try to compute p-value using pt of R like pt(-7.06521,19) I get 5.038877e-07 whereas My Rs defualt t.test function result yields test statistic of -8.1321 and p-value of 1.95e07. I see the qualitatively both are similar but there exists a quantitative difference. Could someone point out why is that so? And also df in R shows some fractional number, in my case df of R is 17.987 which I suppose 19 following n-1 formula for degrees of freedom.










share|cite|improve this question









$endgroup$




I am trying to compute welch's t-test statistic and p-value manually for educational purpose.



I have written a function for welch's t-test as follows,



def welch_t_test(vec_a, vec_b):
mean_a = vec_a.mean()
mean_b = vec_b.mean()
var_a = vec_a.var()
var_b = vec_b.var()

T = (mean_a - mean_b)/((var_a-vec_a.size) + (var_b-vec_b.size))
return T


This function returns T-test statistic. To find p-value, I am using pt function from R-3.3.3.



My example inputs:



A = [6,3,0,7,7,0,5,7,7,6]
B = [10,18,14,18,12,13,15,18,16,18]


My test statisitc is:



Test statistic: -7.065217391304347


When I try to compute p-value using pt of R like pt(-7.06521,19) I get 5.038877e-07 whereas My Rs defualt t.test function result yields test statistic of -8.1321 and p-value of 1.95e07. I see the qualitatively both are similar but there exists a quantitative difference. Could someone point out why is that so? And also df in R shows some fractional number, in my case df of R is 17.987 which I suppose 19 following n-1 formula for degrees of freedom.







t-test p-value






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










asked Apr 19 at 6:08









RussellBRussellB

1226




1226







  • 1




    $begingroup$
    At first glance--In your first block of code, I think your denominator for the Welch t statistic is incorrect. I will give it a try. It's always a good idea to make sure software is giving answers that agree with known formulas.
    $endgroup$
    – BruceET
    Apr 19 at 8:07













  • 1




    $begingroup$
    At first glance--In your first block of code, I think your denominator for the Welch t statistic is incorrect. I will give it a try. It's always a good idea to make sure software is giving answers that agree with known formulas.
    $endgroup$
    – BruceET
    Apr 19 at 8:07








1




1




$begingroup$
At first glance--In your first block of code, I think your denominator for the Welch t statistic is incorrect. I will give it a try. It's always a good idea to make sure software is giving answers that agree with known formulas.
$endgroup$
– BruceET
Apr 19 at 8:07





$begingroup$
At first glance--In your first block of code, I think your denominator for the Welch t statistic is incorrect. I will give it a try. It's always a good idea to make sure software is giving answers that agree with known formulas.
$endgroup$
– BruceET
Apr 19 at 8:07











1 Answer
1






active

oldest

votes


















2












$begingroup$

Welch t statistic:



Using R as a calculator:



A = c(6,3,0,7,7,0,5,7,7,6)
B = c(10,18,14,18,12,13,15,18,16,18)
m = length(A); n = length(B)
t = ( mean(A) - mean(B) ) / sqrt( var(A)/m + var(B)/n ); t
[1] -8.132062


From t.test in R, where Welch is the default 2-sample t test.



A = c(6,3,0,7,7,0,5,7,7,6)
B = c(10,18,14,18,12,13,15,18,16,18)
t.test(A, B)

Welch Two Sample t-test

data: A and B
t = -8.1321, df = 17.987, p-value = 1.95e-07
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-13.086987 -7.713013
sample estimates:
mean of x mean of y
4.8 15.2


So the two versions agree that $T = -8.1321.$



Welch degrees of freedom and P-values:



Traditionally, t distributions had integer degrees of freedom, but the
approximate DF for the Welch t test gives non-integer DF and there is no
problem extending the formula for the density function of t to accommodate
non-integer DF.



The (moderately messy) formula for DF of the Welch test can be found in
many statistics texts and in Wikipedia. This formula gives degrees of freedom $nu$ with
$$ min(m-1, n-1) le nu le m + n - 2.$$
Roughly speaking $nu$ is near the upper limit when sample sizes and sample variances are nearly equal.



Two-sided alternative. For the default 2-sided t test, the P-value in R
is obtained using the CDF function pt. The result agrees with the rounded
value given in the printout. For closer comparison, the unrounded P-value can be obtained using $-notation. (I think the very slight difference is that
R does not print the fractional degrees of freedom to more decimal places.)



2 * pt(-8.1321, 17.987)
[1] 1.949439e-07
t.test(A,B)$p.value
[1] 1.949789e-07


Left-sided alternative. For a left-sided test, the P-values from pt and t.test are again in
substantial agreement:



pt(-8.1321, 17.987)
[1] 9.747197e-08
t.test(A,B, alt="less")$p.value
[1] 9.748945e-08 # P-value captured from 't.test' procedure

t.test(A,B, alt="less")$parameter
df
17.98672

pt(t.test(A,B, alt="less")$statistic, t.test(A,B, alt="less")$parameter)
t
9.748945e-08 # exact agreement with exact DF





share|cite|improve this answer











$endgroup$













    Your Answer








    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
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstats.stackexchange.com%2fquestions%2f403917%2fdifference-in-test-statistic-and-p-value-r-and-python-script%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









    2












    $begingroup$

    Welch t statistic:



    Using R as a calculator:



    A = c(6,3,0,7,7,0,5,7,7,6)
    B = c(10,18,14,18,12,13,15,18,16,18)
    m = length(A); n = length(B)
    t = ( mean(A) - mean(B) ) / sqrt( var(A)/m + var(B)/n ); t
    [1] -8.132062


    From t.test in R, where Welch is the default 2-sample t test.



    A = c(6,3,0,7,7,0,5,7,7,6)
    B = c(10,18,14,18,12,13,15,18,16,18)
    t.test(A, B)

    Welch Two Sample t-test

    data: A and B
    t = -8.1321, df = 17.987, p-value = 1.95e-07
    alternative hypothesis: true difference in means is not equal to 0
    95 percent confidence interval:
    -13.086987 -7.713013
    sample estimates:
    mean of x mean of y
    4.8 15.2


    So the two versions agree that $T = -8.1321.$



    Welch degrees of freedom and P-values:



    Traditionally, t distributions had integer degrees of freedom, but the
    approximate DF for the Welch t test gives non-integer DF and there is no
    problem extending the formula for the density function of t to accommodate
    non-integer DF.



    The (moderately messy) formula for DF of the Welch test can be found in
    many statistics texts and in Wikipedia. This formula gives degrees of freedom $nu$ with
    $$ min(m-1, n-1) le nu le m + n - 2.$$
    Roughly speaking $nu$ is near the upper limit when sample sizes and sample variances are nearly equal.



    Two-sided alternative. For the default 2-sided t test, the P-value in R
    is obtained using the CDF function pt. The result agrees with the rounded
    value given in the printout. For closer comparison, the unrounded P-value can be obtained using $-notation. (I think the very slight difference is that
    R does not print the fractional degrees of freedom to more decimal places.)



    2 * pt(-8.1321, 17.987)
    [1] 1.949439e-07
    t.test(A,B)$p.value
    [1] 1.949789e-07


    Left-sided alternative. For a left-sided test, the P-values from pt and t.test are again in
    substantial agreement:



    pt(-8.1321, 17.987)
    [1] 9.747197e-08
    t.test(A,B, alt="less")$p.value
    [1] 9.748945e-08 # P-value captured from 't.test' procedure

    t.test(A,B, alt="less")$parameter
    df
    17.98672

    pt(t.test(A,B, alt="less")$statistic, t.test(A,B, alt="less")$parameter)
    t
    9.748945e-08 # exact agreement with exact DF





    share|cite|improve this answer











    $endgroup$

















      2












      $begingroup$

      Welch t statistic:



      Using R as a calculator:



      A = c(6,3,0,7,7,0,5,7,7,6)
      B = c(10,18,14,18,12,13,15,18,16,18)
      m = length(A); n = length(B)
      t = ( mean(A) - mean(B) ) / sqrt( var(A)/m + var(B)/n ); t
      [1] -8.132062


      From t.test in R, where Welch is the default 2-sample t test.



      A = c(6,3,0,7,7,0,5,7,7,6)
      B = c(10,18,14,18,12,13,15,18,16,18)
      t.test(A, B)

      Welch Two Sample t-test

      data: A and B
      t = -8.1321, df = 17.987, p-value = 1.95e-07
      alternative hypothesis: true difference in means is not equal to 0
      95 percent confidence interval:
      -13.086987 -7.713013
      sample estimates:
      mean of x mean of y
      4.8 15.2


      So the two versions agree that $T = -8.1321.$



      Welch degrees of freedom and P-values:



      Traditionally, t distributions had integer degrees of freedom, but the
      approximate DF for the Welch t test gives non-integer DF and there is no
      problem extending the formula for the density function of t to accommodate
      non-integer DF.



      The (moderately messy) formula for DF of the Welch test can be found in
      many statistics texts and in Wikipedia. This formula gives degrees of freedom $nu$ with
      $$ min(m-1, n-1) le nu le m + n - 2.$$
      Roughly speaking $nu$ is near the upper limit when sample sizes and sample variances are nearly equal.



      Two-sided alternative. For the default 2-sided t test, the P-value in R
      is obtained using the CDF function pt. The result agrees with the rounded
      value given in the printout. For closer comparison, the unrounded P-value can be obtained using $-notation. (I think the very slight difference is that
      R does not print the fractional degrees of freedom to more decimal places.)



      2 * pt(-8.1321, 17.987)
      [1] 1.949439e-07
      t.test(A,B)$p.value
      [1] 1.949789e-07


      Left-sided alternative. For a left-sided test, the P-values from pt and t.test are again in
      substantial agreement:



      pt(-8.1321, 17.987)
      [1] 9.747197e-08
      t.test(A,B, alt="less")$p.value
      [1] 9.748945e-08 # P-value captured from 't.test' procedure

      t.test(A,B, alt="less")$parameter
      df
      17.98672

      pt(t.test(A,B, alt="less")$statistic, t.test(A,B, alt="less")$parameter)
      t
      9.748945e-08 # exact agreement with exact DF





      share|cite|improve this answer











      $endgroup$















        2












        2








        2





        $begingroup$

        Welch t statistic:



        Using R as a calculator:



        A = c(6,3,0,7,7,0,5,7,7,6)
        B = c(10,18,14,18,12,13,15,18,16,18)
        m = length(A); n = length(B)
        t = ( mean(A) - mean(B) ) / sqrt( var(A)/m + var(B)/n ); t
        [1] -8.132062


        From t.test in R, where Welch is the default 2-sample t test.



        A = c(6,3,0,7,7,0,5,7,7,6)
        B = c(10,18,14,18,12,13,15,18,16,18)
        t.test(A, B)

        Welch Two Sample t-test

        data: A and B
        t = -8.1321, df = 17.987, p-value = 1.95e-07
        alternative hypothesis: true difference in means is not equal to 0
        95 percent confidence interval:
        -13.086987 -7.713013
        sample estimates:
        mean of x mean of y
        4.8 15.2


        So the two versions agree that $T = -8.1321.$



        Welch degrees of freedom and P-values:



        Traditionally, t distributions had integer degrees of freedom, but the
        approximate DF for the Welch t test gives non-integer DF and there is no
        problem extending the formula for the density function of t to accommodate
        non-integer DF.



        The (moderately messy) formula for DF of the Welch test can be found in
        many statistics texts and in Wikipedia. This formula gives degrees of freedom $nu$ with
        $$ min(m-1, n-1) le nu le m + n - 2.$$
        Roughly speaking $nu$ is near the upper limit when sample sizes and sample variances are nearly equal.



        Two-sided alternative. For the default 2-sided t test, the P-value in R
        is obtained using the CDF function pt. The result agrees with the rounded
        value given in the printout. For closer comparison, the unrounded P-value can be obtained using $-notation. (I think the very slight difference is that
        R does not print the fractional degrees of freedom to more decimal places.)



        2 * pt(-8.1321, 17.987)
        [1] 1.949439e-07
        t.test(A,B)$p.value
        [1] 1.949789e-07


        Left-sided alternative. For a left-sided test, the P-values from pt and t.test are again in
        substantial agreement:



        pt(-8.1321, 17.987)
        [1] 9.747197e-08
        t.test(A,B, alt="less")$p.value
        [1] 9.748945e-08 # P-value captured from 't.test' procedure

        t.test(A,B, alt="less")$parameter
        df
        17.98672

        pt(t.test(A,B, alt="less")$statistic, t.test(A,B, alt="less")$parameter)
        t
        9.748945e-08 # exact agreement with exact DF





        share|cite|improve this answer











        $endgroup$



        Welch t statistic:



        Using R as a calculator:



        A = c(6,3,0,7,7,0,5,7,7,6)
        B = c(10,18,14,18,12,13,15,18,16,18)
        m = length(A); n = length(B)
        t = ( mean(A) - mean(B) ) / sqrt( var(A)/m + var(B)/n ); t
        [1] -8.132062


        From t.test in R, where Welch is the default 2-sample t test.



        A = c(6,3,0,7,7,0,5,7,7,6)
        B = c(10,18,14,18,12,13,15,18,16,18)
        t.test(A, B)

        Welch Two Sample t-test

        data: A and B
        t = -8.1321, df = 17.987, p-value = 1.95e-07
        alternative hypothesis: true difference in means is not equal to 0
        95 percent confidence interval:
        -13.086987 -7.713013
        sample estimates:
        mean of x mean of y
        4.8 15.2


        So the two versions agree that $T = -8.1321.$



        Welch degrees of freedom and P-values:



        Traditionally, t distributions had integer degrees of freedom, but the
        approximate DF for the Welch t test gives non-integer DF and there is no
        problem extending the formula for the density function of t to accommodate
        non-integer DF.



        The (moderately messy) formula for DF of the Welch test can be found in
        many statistics texts and in Wikipedia. This formula gives degrees of freedom $nu$ with
        $$ min(m-1, n-1) le nu le m + n - 2.$$
        Roughly speaking $nu$ is near the upper limit when sample sizes and sample variances are nearly equal.



        Two-sided alternative. For the default 2-sided t test, the P-value in R
        is obtained using the CDF function pt. The result agrees with the rounded
        value given in the printout. For closer comparison, the unrounded P-value can be obtained using $-notation. (I think the very slight difference is that
        R does not print the fractional degrees of freedom to more decimal places.)



        2 * pt(-8.1321, 17.987)
        [1] 1.949439e-07
        t.test(A,B)$p.value
        [1] 1.949789e-07


        Left-sided alternative. For a left-sided test, the P-values from pt and t.test are again in
        substantial agreement:



        pt(-8.1321, 17.987)
        [1] 9.747197e-08
        t.test(A,B, alt="less")$p.value
        [1] 9.748945e-08 # P-value captured from 't.test' procedure

        t.test(A,B, alt="less")$parameter
        df
        17.98672

        pt(t.test(A,B, alt="less")$statistic, t.test(A,B, alt="less")$parameter)
        t
        9.748945e-08 # exact agreement with exact DF






        share|cite|improve this answer














        share|cite|improve this answer



        share|cite|improve this answer








        edited Apr 19 at 7:43

























        answered Apr 19 at 7:33









        BruceETBruceET

        7,2361721




        7,2361721



























            draft saved

            draft discarded
















































            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%2f403917%2fdifference-in-test-statistic-and-p-value-r-and-python-script%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

            Sum ergo cogito? 1 nng

            三茅街道4182Guuntc Dn precexpngmageondP