Stata Textbook Examples
Applied Regression Analysis
Chapter 9: Statistical Theory for Linear Models

Linear Contrasts on page 209 using the data file friendly. Stata does not have a built-in contrast command; however, ATS has developed a program that will do ANOVA contrasts. You can download contrast from within Stata by typing findit contrast (see How can I use the findit command to search for programs and get additional help? for more information about using findit).
use http://www.ats.ucla.edu/stat/stata/examples/ara/friendly, clear
(From Fox, Applied Regression Analysis.  Use 'notes' command for source of data)

encode(cond), gen(mycond)
codebook mycond

mycond ------------------------------------------------- Experimental Condition
                  type:  numeric (long)
                 label:  mycond

                 range:  [1,3]                        units:  1
         unique values:  3                    coded missing:  0 / 30

            tabulation:  Freq.   Numeric  Label
                            10         1  Before
                            10         2  Meshed
                            10         3  SFR

sort mycond
by mycond: summarize correct

-> mycond=  Before  
Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
 correct |      10        36.6   5.337498         24         40  

-> mycond=  Meshed  
Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
 correct |      10        36.6   3.025815         30         40  

-> mycond=     SFR  
Variable |     Obs        Mean   Std. Dev.       Min        Max
---------+-----------------------------------------------------
 correct |      10        30.3   7.334091         21         39  

anova correct mycond, class(mycond)

                           Number of obs =      30     R-squared     =  0.2433
                           Root MSE      = 5.52067     Adj R-squared =  0.1873

                  Source |  Partial SS    df       MS           F     Prob > F
              -----------+----------------------------------------------------
                   Model |      264.60     2      132.30       4.34     0.0232
                         |
                  mycond |      264.60     2      132.30       4.34     0.0232
                         |
                Residual |      822.90    27  30.4777778   
              -----------+----------------------------------------------------
                   Total |     1087.50    29       37.50   

contrast mycond, values( -0.5 -0.5 1)

Contrast variable mycond (-.5 -.5 1)
source           SS          df      MS        Dep Var  =  correct
---------+---------------------------------    N of obs =       30
contrast |      264.6         1    264.6000    F        =     8.68
error    |      822.9        27     30.4778    Prob > F =   0.0065
---------+---------------------------------

contrast mycond, values( 1 -1 0)

Contrast variable mycond (1 -1 0)
source           SS          df      MS        Dep Var  =  correct
---------+---------------------------------    N of obs =       30
contrast |          0         1      0.0000    F        =     0.00
error    |      822.9        27     30.4778    Prob > F =   1.0000
---------+---------------------------------
Calculation on page 214 and on page 222 using data file duncan.
use http://www.ats.ucla.edu/stat/stata/examples/ara/duncan.dta, clear
(From Fox, Applied Regression Analysis.  Use 'notes' command for source of data)

set matsize 50
gen c=1
mkmat c income educ, matrix(X)
mkmat prestige, matrix(y)
matrix b=inv(X'*X)*X'*y
matrix list b

b[3,1]
          prestige
     c  -6.0646629
income   .59873282
  educ   .54583391
Calculation on page 222.
matrix r=y-X*b
matrix v=(r'*r)/42
matrix V2=v*inv(X'*X)
matrix d=diag(vecdiag(V2))
matrix a=cholesky(d)
matrix list a

symmetric a[3,3]
                c     income       educ
     c  4.2719412
income          0  .11966735
  educ          0          0  .09825264

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.