Logo

RunningTTEST

Updated 2023-11-14 15:43:20.743000

Syntax

SELECT [westclintech].[wct].[RunningTTEST](
  <@X, float,>
 ,<@Y, float,>
 ,<@TAILS, int,>
 ,<@TTYPE, int,>
 ,<@RowNum, int,>
 ,<@Id, tinyint,>)

Description

Use the scalar function RunningTTEST to calculate the Student’s t-Test of column values in an ordered resultant table, without the need for a self-join. The t-Test is calculated over all the values from the first value to the last value in the ordered group or partition. If the column values are presented to the functions out of order, an error message will be generated.

Arguments

@Id

a unique identifier for the RunningTTEST calculation. @Id allows you to specify multiple running t-Test calculations within a resultant table. @Id is an expression of type tinyint or of a type that can be implicitly converted to tinyint.

@TAILS

specifies the number of distribution tails. If @Tails = 1 the one-tailed distribution is returned. If @Tails = 2 the two-tailed distribution is returned. @TAILS is an expression of type int or of a type that can be implicitly converted to int.

@TTYPE

is the kind of t-Test to perform. If @TTYPE =1, perform the paired test. If @TTYPE = 2, perform the two-sample equal variance test. If @TTYPE = 3, perform the two-sample unequal variance test. @TTYPE is an expression of type int or of a type that can be implicitly converted to int.

@RowNum

the number of the row within the group for which the t-Testis being calculated. If @RowNum for the current row in a set is less than or equal to the previous @RowNum and @RowNum is not equal to 1, an error message will be generated. @RowNum is an expression of type int or of a type that can be implicitly converted to int.

@X

the x-value passed into the function. @X is an expression of type float or of a type that can be implicitly converted to float.

@Y

the y-value passed into the function. @Y is an expression of type float or of a type that can be implicitly converted to float.

Return Type

float

Remarks

If @Id is NULL then @Id = 0.

To calculate moving t-Test, use the MovingTTEST function.

To calculate the t-Test for an entire data set, use the TTEST function.

If @RowNum is equal to 1, RunningTTEST is equal to zero.

@RowNum must be in ascending order.

There may be cases where the order in which the data are returned to the function and the order in which the results are returned are different, generally due to parallelism. You can use OPTION(MAXDOP 1) or OPTION(MAXDOP 1,FORCE ORDER) to help eliminate this problem

Examples

In this example, we have 20 rows of data and we want to calculate the t-Test for x and y starting from the first row.

SELECT rn,

       x,

       y,

       wct.RunningTTEST(   x,                               --@X

                           y,                               --@Y

                           1,                               --@TAILS

                           1,                               --@TTYPE

                           ROW_NUMBER() OVER (ORDER by rn), --@RowNum

                           NULL                             --@Id

                       ) as [PAIRED T TEST]

FROM

(

    VALUES

        (1, 102, 118),

        (2, 142, 89),

        (3, 110, 72),

        (4, 110, 101),

        (5, 96, 133),

        (6, 101, 97),

        (7, 99, 110),

        (8, 96, 112),

        (9, 96, 101),

        (10, 126, 97),

        (11, 98, 102),

        (12, 105, 107),

        (13, 108, 52),

        (14, 126, 152),

        (15, 72, 148),

        (16, 114, 60),

        (17, 111, 70),

        (18, 100, 69),

        (19, 118, 100),

        (20, 84, 66)

) n (rn, x, y);

This produces the following result.

{"columns":[{"field":"rn","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"y","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"PAIRED T TEST"}],"rows":[{"rn":"1","x":"102","y":"118","PAIRED T TEST":"NULL"},{"rn":"2","x":"142","y":"89","PAIRED T TEST":"0.343324287673918"},{"rn":"3","x":"110","y":"72","PAIRED T TEST":"0.177573653110776"},{"rn":"4","x":"110","y":"101","PAIRED T TEST":"0.1323245043316"},{"rn":"5","x":"96","y":"133","PAIRED T TEST":"0.300831927745503"},{"rn":"6","x":"101","y":"97","PAIRED T TEST":"0.279567054776969"},{"rn":"7","x":"99","y":"110","PAIRED T TEST":"0.32295200882193"},{"rn":"8","x":"96","y":"112","PAIRED T TEST":"0.392573746764456"},{"rn":"9","x":"96","y":"101","PAIRED T TEST":"0.413799013100269"},{"rn":"10","x":"126","y":"97","PAIRED T TEST":"0.299580396651529"},{"rn":"11","x":"98","y":"102","PAIRED T TEST":"0.314163559135612"},{"rn":"12","x":"105","y":"107","PAIRED T TEST":"0.321165965172453"},{"rn":"13","x":"108","y":"52","PAIRED T TEST":"0.178190609695489"},{"rn":"14","x":"126","y":"152","PAIRED T TEST":"0.256869936459987"},{"rn":"15","x":"72","y":"148","PAIRED T TEST":"0.488326684115475"},{"rn":"16","x":"114","y":"60","PAIRED T TEST":"0.367100505513088"},{"rn":"17","x":"111","y":"70","PAIRED T TEST":"0.275167932640836"},{"rn":"18","x":"100","y":"69","PAIRED T TEST":"0.215261044421339"},{"rn":"19","x":"118","y":"100","PAIRED T TEST":"0.183464999011208"},{"rn":"20","x":"84","y":"66","PAIRED T TEST":"0.154928215657014"}]}

In this example, we have 20 rows of data and we want to calculate the t-Test for x and z as well as y and z starting from the first row. Notice that each t-Test column requires a different @Id.

SELECT rn,
       x,
       y,
       z,
       wct.RunningTTEST(x, z, 1, 1, ROW_NUMBER() OVER (ORDER by rn), NULL) as [PAIRED T TEST xz],
       wct.RunningTTEST(y, z, 1, 1, ROW_NUMBER() OVER (ORDER by rn), 1) as [PAIRED T TEST yz]
FROM
(
    VALUES
        (1, 102, 106, 118),
        (2, 142, 99, 89),
        (3, 110, 99, 72),
        (4, 110, 119, 101),
        (5, 96, 106, 133),
        (6, 101, 95, 97),
        (7, 99, 90, 110),
        (8, 96, 110, 112),
        (9, 96, 101, 101),
        (10, 126, 111, 97),
        (11, 98, 79, 102),
        (12, 105, 93, 107),
        (13, 108, 96, 52),
        (14, 126, 82, 152),
        (15, 72, 107, 148),
        (16, 114, 96, 60),
        (17, 111, 92, 70),
        (18, 100, 122, 69),
        (19, 118, 105, 100),
        (20, 84, 93, 66)
) n (rn, x, y, z);

This produces the following result.

{"columns":[{"field":"rn","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"x","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"y","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"z       PA","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"RED T TEST xz       PA"},{"field":"RED T TEST yz"}],"rows":[{"rn":"1","x":"102","y":"106","z       PA":"118","RED T TEST xz       PA":"NULL","RED T TEST yz":"NULL"},{"rn":"2","x":"142","y":"99","z       PA":"89","RED T TEST xz       PA":"0.343324287673918","RED T TEST yz":"0.471142061623696"},{"rn":"3","x":"110","y":"99","z       PA":"72","RED T TEST xz       PA":"0.177573653110776","RED T TEST yz":"0.268637501554641"},{"rn":"4","x":"110","y":"119","z       PA":"101","RED T TEST xz       PA":"0.1323245043316","RED T TEST yz":"0.143912633939745"},{"rn":"5","x":"96","y":"106","z       PA":"133","RED T TEST xz       PA":"0.300831927745503","RED T TEST yz":"0.381776828090856"},{"rn":"6","x":"101","y":"95","z       PA":"97","RED T TEST xz       PA":"0.279567054776969","RED T TEST yz":"0.39318719664846"},{"rn":"7","x":"99","y":"90","z       PA":"110","RED T TEST xz       PA":"0.32295200882193","RED T TEST yz":"0.456931716246789"},{"rn":"8","x":"96","y":"110","z       PA":"112","RED T TEST xz       PA":"0.392573746764456","RED T TEST yz":"0.441763479139861"},{"rn":"9","x":"96","y":"101","z       PA":"101","RED T TEST xz       PA":"0.413799013100269","RED T TEST yz":"0.441053495066801"},{"rn":"10","x":"126","y":"111","z       PA":"97","RED T TEST xz       PA":"0.299580396651529","RED T TEST yz":"0.456999645393713"},{"rn":"11","x":"98","y":"79","z       PA":"102","RED T TEST xz       PA":"0.314163559135612","RED T TEST yz":"0.389020734261484"},{"rn":"12","x":"105","y":"93","z       PA":"107","RED T TEST xz       PA":"0.321165965172453","RED T TEST yz":"0.307142004911739"},{"rn":"13","x":"108","y":"96","z       PA":"52","RED T TEST xz       PA":"0.178190609695489","RED T TEST yz":"0.433182651770177"},{"rn":"14","x":"126","y":"82","z       PA":"152","RED T TEST xz       PA":"0.256869936459987","RED T TEST yz":"0.295689725079382"},{"rn":"15","x":"72","y":"107","z       PA":"148","RED T TEST xz       PA":"0.488326684115475","RED T TEST yz":"0.193384809174959"},{"rn":"16","x":"114","y":"96","z       PA":"60","RED T TEST xz       PA":"0.367100505513088","RED T TEST yz":"0.302618971415815"},{"rn":"17","x":"111","y":"92","z       PA":"70","RED T TEST xz       PA":"0.275167932640836","RED T TEST yz":"0.371627955172775"},{"rn":"18","x":"100","y":"122","z       PA":"69","RED T TEST xz       PA":"0.215261044421339","RED T TEST yz":"0.46134448377815"},{"rn":"19","x":"118","y":"105","z       PA":"100","RED T TEST xz       PA":"0.183464999011208","RED T TEST yz":"0.446462433674427"},{"rn":"20","x":"84","y":"93","z       PA":"66","RED T TEST xz       PA":"0.154928215657014","RED T TEST yz":"0.370545416684929"}]}