SUPPORT / SAMPLES & SAS NOTES
 

Support

Sample 24984: Computation of LSMEANS, and Standard Errors and p-Values for Differences of LSMEANS

DetailsCodeResultsAboutRate It

Computation of LSMEANS, and Standard Errors and p-Values for Differences of LSMEANS

This example demonstrates the calculation of the LSMEANS, their standard errors, t-statistics, and associated p-values from the TDIFF and PDIFF options in the LSMEANS statement of PROC GLM. SAS code is provided that reproduces the output from the LSMEANS statement and illustrates a method to obtain the information in an output data set.

The LSMEANS are computed as L*β, where L is the hypothesis matrix, β is defined as ginv(X`X)*X`Y, and the standard error of L*β is defined as sqrt[L*ginv(X`X)*L`*σ2], where ginv is the generalized inverse and σ2 is estimated by the mean square error (MSE). The results generated by the PDIFF option are presented in a table that includes the p-values from testing the null hypotheses LSMEAN(i)=LSMEAN(j). The TDIFF option provides the associated t-statistics. The results of the PDIFF option in the LSMEANS statement can be reproduced by the CONTRAST statement or the ESTIMATE statement as shown below.

Multiple comparison testing of LSMEANS became available in SAS 6.10 with the addition of the ADJUST= option on the LSMEANS statement. Specifying the ADJUST= option with one of the following tests Bonferroni, Scheffe, Dunnett, Sidak, Simulate, SMM (or GT2), or Tukey will adjust the p-values for the multiple comparisons. If you specify the PDIFF option and omit the ADJUST= option, the default method is to analyze all pairwise comparisons using t-tests providing no p-value adjustment.

The OUTSTAT= data set in PROC GLM contains the results from the CONTRAST statement that uses the mean square error as the denominator. The OUTSTAT= data set does not contain the results from the ESTIMATE statement. Beginning in SAS 7, all SAS procedures use ODS (the Output Delivery System) which among other things allows the output of any table to a data set. Output of tables is accomplished via the ODS OUTPUT statement. See the "Using the Output Delivery System" chapter of the SAS/STAT User's Guide for more information.

In these statements:

     ods output lsmeans=lsm diff=lsdiff estimates=estdiffs;
     proc glm data=test;
       class row col;
       model y=row|col;
       lsmeans row / stderr tdiff pdiff;
       estimate 'r1 lsmean' intercept 3 row 3 0 0 col 1 1 1
                            row*col 1 1 1 0 0 0 0 0 0/ divisor=3;
       estimate 'r2 lsmean' intercept 3 row 0 3 0 col 1 1 1
                            row*col 0 0 0 1 1 1 0 0 0/ divisor=3;
       estimate 'r3 lsmean' intercept 3 row 0 0 3 col 1 1 1
                            row*col 0 0 0 0 0 0 1 1 1/ divisor=3;
       estimate 'diff r1 vs r2' row 1 -1 0;
       estimate 'diff r1 vs r3' row 1 0 -1;
       estimate 'diff r2 vs r3' row 0 1 -1;
     run;
     quit;

the LSMEANS for the ROW effect are reproduced using the ESTIMATE statement. The abbreviated output below first shows the results from the LSMEANS statement for the ROW effect followed by the output from the ESTIMATE statements. The ODS OUTPUT statement creates a SAS data set named LSM containing the LSMEANS, a data set named LSDIFF containing the t-statistics and p-values comparing the LSMEANS, and a data set named ESTDIFFS containing the results from the ESTIMATE statements.

                        General Linear Models Procedure
                              Least Squares Means

             ROW             Y       Std Err     Pr > |T|   LSMEAN
                        LSMEAN        LSMEAN   H0:LSMEAN=0   Number

             1      2.00000000    0.73960026        0.0181     1
             2      4.33333333    0.65806416        0.0001     2
             3      4.66666667    0.54433105        0.0001     3

                    T for H0: LSMEAN(i)=LSMEAN(j) / Pr > |T|

                          i/j       1         2         3
                          1     .      -2.35695  -2.90387
                                         0.0348    0.0123
                          2  2.356954     .      -0.39031
                               0.0348              0.7026
                          3  2.903865  0.390312     .
                               0.0123    0.7026

NOTE: To ensure overall protection level, only probabilities associated with
      pre-planned comparisons should be used.


                                        T for H0:    Pr > |T|   Std Error of
Parameter                  Estimate    Parameter=0                Estimate

r1 lsmean                2.00000000           2.70     0.0181     0.73960026
r2 lsmean                4.33333333           6.58     0.0001     0.65806416
r3 lsmean                4.66666667           8.57     0.0001     0.54433105

Note that the values reported in the Estimate, Pr > |T|, and Std Error of Estimate columns are identical to the preceding LSMEANS statement results which test the null hypothesis LSMEAN=0.

                                        T for H0:    Pr > |T|       Std Error of
Parameter                  Estimate    Parameter=0                    Estimate

diff r1 vs r2           -2.33333333       -2.36       0.0348       0.98997827
diff r1 vs r3           -2.66666667       -2.90       0.0123       0.91831631
diff r2 vs r3           -0.33333333       -0.39       0.7026       0.85401682

The values in the Estimate column are the differences of pairs of LSMEANS (indicated in the Parameter column). These values also represent the numerator in the t-statistics given by the TDIFF option.

The values in the T for H0: Parameter=0 and Pr > |T| columns are identical to the t- and p-values given by the TDIFF and PDIFF options in LSMEANS statement in the preceding table.

The values in the Std Error of Estimate column are the standard errors for the LSMEAN differences. However, in the LSMEANS output the Std Err LSMEAN column gives the standard error of each row's LSMEAN which is used for testing LSMEAN=0. It is not the standard error of the difference of two rows' LSMEANS used by the PDIFF option to compare LSMEANS.

For the following data set, suppose a model is fit including the row and column main effects and the row*column interaction.

2
(2)
3 1
(2)
2 2
(2)
1 2 3
(2)
5 2 8 5
(5)
6
(6)
1 2 2 3
(2)
1 3 5
(3)
9 9
(9)

Cell means in the above table appear within parentheses and represent the LSMEANS for the ROW*COL effect. The following calculations are shown below:

  • The LSMEANS for the main effects and interaction terms.
  • The t-statistic and associated p-value from the TDIFF and PDIFF options.
  • The standard errors of pairwise differences among LSMEANS for the main effects and interaction. These standard errors are not included in the LSMEANS output when the PDIFF option is specified.

The LSMEANS are computed as follows:

LSMEAN ROW1 = [(2) + (2) + (2)] / 3 = 2.00
LSMEAN ROW2 = [(2) + (5) + (6)] / 3 = 4.33
LSMEAN ROW3 = [(2) + (3) + (9)] / 3 = 4.67

LSMEAN COL1 = [(2) + (2) + (2)] / 3 = 2.00
LSMEAN COL2 = [(2) + (5) + (3)] / 3 = 3.33
LSMEAN COL3 = [(2) + (6) + (9)] / 3 = 5.66

LSMEANS ROW1*COL1 = 2/1 = 2.0
LSMEANS ROW1*COL2 = (3+1)/2 = 2.0
LSMEANS ROW1*COL3 = (2+2)/2 = 2.0
LSMEANS ROW2*COL1 = (1+2+3)/3 = 2.0
LSMEANS ROW2*COL2 = (5+2+8+5)/4 = 5.0
LSMEANS ROW2*COL3 = 6/1 = 6.0
LSMEANS ROW3*COL1 = (1+2+2+3)/4 = 2.0
LSMEANS ROW3*COL2 = (1+3+5)/3 = 3.0
LSMEANS ROW3*COL3 = (9+9)/2 = 9.0

The TDIFF option computes a t statistic as follows for the Row i vs. Row i` difference:

t = [LSMEANi - LSMEANi`] / sqrt(MSE)/nc * sqrt( Σj1/nijj1/ni`j ) ,

where nc= number of cells in an LSMEAN. The standard error of the difference of the Row i and i` LSMEANS is the denominator of the t-statistic:

STDERR = sqrt(MSE)/nc * sqrt( Σj1/nijj1/ni`j )

For these data, MSE=2.4615 and the error degrees of freedom (DF)=13. Then, for the Row1-Row2 difference:

t = [2 - 4.33] / sqrt(2.4615)/3*sqrt[(1/1+1/2+1/2)+(1/3+1/4+1/1)]
  = -2.33 / sqrt(.98)
  = -2.35

The p-value for t=-2.35 and 13 error DF is 0.034. The following DATA step code calculates the p-value for the t-statistic.

  data prob;
     r1r2=(1-probt(abs(-2.35695),13))*2;
     r1r3=(1-probt(abs(-2.90387),13))*2;
     r2r3=(1-probt(abs(-0.39031),13))*2;
     run;
  proc print;
     run;

The t statistic and associated p-value for the row1-row2 comparison appears in the 1,2 and 2,1 cells of the PDIFF table as shown below. The remaining parts of the PDIFF table can be calculated similarly.

                            Least Squares Means for Effect row
                         t for H0: LSMean(i)=LSMean(j) / Pr > |t|

                                  Dependent Variable: y

                      i/j              1             2             3

                         1                    -2.35695      -2.90387
                                                0.0348        0.0123
                         2      2.356954                    -0.39031
                                  0.0348                      0.7026
                         3      2.903865      0.390312
                                  0.0123        0.7026

For the example data set above, the following table shows the standard error computations for all differences of LSMEANS. Note that sqrt(MSE)=1.5689291.

 LSMEAN
Difference                  STDERR Computation
---------- -------------------------------------------------------------
ROW1-ROW2  .98997827  = (1.5689291/3) SQRT [(1/1+1/2+1/2)+(1/3+1/4+1/1)]
ROW1-ROW3  .91831631  = (1.5689291/3) SQRT [(1/1+1/2+1/2)+(1/4+1/3+1/2)]
ROW2-ROW3  .85401682  = (1.5689291/3) SQRT [(1/3+1/4+1/1)+(1/4+1/3+1/2)]

COL1-COL2  .85401682  = (1.5689291/3) SQRT [(1/1+1/3+1/4)+(1/2+1/4+1/3)]
COL1-COL3  .98997827  = (1.5689291/3) SQRT [(1/1+1/3+1/4)+(1/2+1/1+1/2)]
COL2-COL3  .91831631  = (1.5689291/3) SQRT [(1/2+1/4+1/3)+(1/2+1/1+1/2)]

R1C1-R1C2  1.92153785 = (1.5689291/1) SQRT [(1/1 + 1/2)]
R1C1-R1C3  1.92153785 = (1.5689291/1) SQRT [(1/1 + 1/2)]
R1C1-R2C1  1.81164325 = (1.5689291/1) SQRT [(1/1 + 1/3)]
R1C1-R2C2  1.75411604 = (1.5689291/1) SQRT [(1/1 + 1/4)]
R1C1-R2C3  2.21880078 = (1.5689291/1) SQRT [(1/1 + 1/1)]
R1C1-R3C1  1.75411604 = (1.5689291/1) SQRT [(1/1 + 1/4)]
R1C1-R3C2  1.81164325 = (1.5689291/1) SQRT [(1/1 + 1/3)]
R1C1-R3C3  1.92153785 = (1.5689291/1) SQRT [(1/1 + 1/2)]

R1C2-R1C3  1.56892908 = (1.5689291/1) SQRT [(1/2 + 1/2)]
R1C2-R2C1  1.43222975 = (1.5689291/1) SQRT [(1/2 + 1/3)]
R1C2-R2C2  1.35873244 = (1.5689291/1) SQRT [(1/2 + 1/4)]
R1C2-R2C3  1.92153785 = (1.5689291/1) SQRT [(1/2 + 1/1)]
R1C2-R3C1  1.35873244 = (1.5689291/1) SQRT [(1/2 + 1/4)]
R1C2-R3C2  1.43222975 = (1.5689291/1) SQRT [(1/2 + 1/3)]
R1C2-R3C3  1.56892908 = (1.5689291/1) SQRT [(1/2 + 1/2)]

R1C3-R2C1  1.43222975 = (1.5689291/1) SQRT [(1/2 + 1/3)]
R1C3-R2C2  1.35873244 = (1.5689291/1) SQRT [(1/2 + 1/4)]
R1C3-R2C3  1.92153785 = (1.5689291/1) SQRT [(1/2 + 1/1)]
R1C3-R3C1  1.35873244 = (1.5689291/1) SQRT [(1/2 + 1/4)]
R1C3-R3C2  1.43222975 = (1.5689291/1) SQRT [(1/2 + 1/3)]
R1C3-R3C3  1.56892908 = (1.5689291/1) SQRT [(1/2 + 1/2)]

R2C1-R2C2  1.19828938 = (1.5689291/1) SQRT [(1/3 + 1/4)]
R2C1-R2C3  1.81164325 = (1.5689291/1) SQRT [(1/3 + 1/1)]
R2C1-R3C1  1.19828938 = (1.5689291/1) SQRT [(1/3 + 1/4)]
R2C1-R3C2  1.28102523 = (1.5689291/1) SQRT [(1/3 + 1/3)]
R2C1-R3C3  1.43222975 = (1.5689291/1) SQRT [(1/3 + 1/2)]

R2C2-R2C3  1.75411604 = (1.5689291/1) SQRT [(1/4 + 1/1)]
R2C2-R3C1  1.10940039 = (1.5689291/1) SQRT [(1/4 + 1/4)]
R2C2-R3C2  1.19828938 = (1.5689291/1) SQRT [(1/4 + 1/3)]
R2C2-R3C3  1.35873244 = (1.5689291/1) SQRT [(1/4 + 1/2)]

R2C3-R3C1  1.75411604 = (1.5689291/1) SQRT [(1/1 + 1/4)]
R2C3-R3C2  1.81164325 = (1.5689291/1) SQRT [(1/1 + 1/3)]
R2C3-R3C3  1.92153785 = (1.5689291/1) SQRT [(1/1 + 1/2)]

R3C1-R3C2  1.19828938 = (1.5689291/1) SQRT [(1/4 + 1/3)]
R3C1-R3C3  1.35873244 = (1.5689291/1) SQRT [(1/4 + 1/2)]

R3C2-R3C3  1.43222975 = (1.5689291/1) SQRT [(1/3 + 1/2)]

While it is not possible to cover all possible models, this methodology can be extended to the analysis of more complex designs.




These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.