UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Stata Code Fragments
Anova

Please Note: Stata graph commands changed with version 8 and this page was developed before version 8 was released and uses Stata 7 graph commands.  Please see How do I use version 7 graph commands in Stata version 8? for information on how to either run these Stata 7 graph commands in Stata version 8, or how you can covert these commands to use Stata 8 syntax.

.use crf

* descriptive statistics by group or cell:

sort b
by b: summarize y 

sort a b
by a b: summarize y

tabulate a b,summarize(y)
table a b, contents(mean y sd y) row col

* graph interaction/plot means by cell:

anova y a b a*b
predict yhat
sort a b
graph yhat b,twoway ylabel xlabel c(L)
sort b a
graph yhat a,twoway ylabel xlabel c(L)

* graph interaction with error bars:

anova y a b a*b
predict mean
generate rmse=e(rmse)
sort a b
serrbar mean rmse b, ylabel xlabel c(L)
sort b a
serrbar mean rmse a, ylabel xlabel c(L)

* Levene's test of heterogeneity of variance:

egen m=mean y, by(b)
generate d = abs(y - m)
anova d b                 /* the F-ratio is Levene's test  */

* check homogeneity of variance in factorial design:

egen cell=group(a b)
oneway y cell            /* oneway includes Bartlett's test */

* check normality:

sort b
by b: graph y, normal
pnorm y if b==1    /* pnorm does not allow by option */
pnorm y if b==2    /* etc */

* oneway anova:

oneway y b, tabulate sidak   /* with post-hoc comparisons */
anova y b

* factorial anova:

anova y a b a*b
test b, error(a*b)        /*  using interaction as error term */
anova y a b / a*b /       /* alternate method for using different error term */

* nested anova:

anova y a b|a            /* using residual as error term for both a and b|a    */
anova y a / b|a /        /* using b|a as error term for a and residual for b|a */

* reshape wide to long:

reshape long y, i(s) j(b)

* repeated measures anova:
* data are long

anova y b s, repeated(b)            /* randomized block design */
reshape wide y, i(s) j(b)           /* reshape so data are wide */
corr y1 y2 y3 y4, covariance        /* check variance-covariance matrix */

anova y a / s|a b a*b, repeated(b)  /* split-plot factorial    */


* Tukeys test for additivity in randomized block designs: 

anova y a s 
predict yhat 
generate ystar = (yhat - 5.375)^2      /*  5.375 is the grand mean */
anova y a s ystar, continuous(ystar) 


* tests of simple main effects:
* sme.ado written by Phil Ender

anova y a b a*b
sme a b
sme b a

* pairwise comparisons:
* prcomp.ado written by John Gleason (sg-101)
* in STB-47 downloaded from STATA 

prcomp y b, test
prcomp y b, test tukey                    /* Tukey HSD */
prcomp y b, test tukey nu(24) sigma(.85)  /* using a different dfe and rmse */

* contrasts:
* contrast.ado written by Phil Ender

anova y a b a*b
contrast b, values(1 1 -1 -1) title(12 vs 34)


* analysis of covariance:

anova y b cov, continuous(cov)
adjust cov, by(b)                /* compute adjusted means */

anova y b cov b*cov, continuous(cov)  /* check homogeneity of slopes */

* sequence of commands to make pairwise tests among adjusted means
anova y b cov, continuous(cov)   /* save dfe and rmse */
anova y b
predict ybar
summarize cov
generate yhat = r(mean)
anova cov b
predict xbar
regress y cov                  /* save slope coefficient b */
generate adj = ybar - b*(xbar - xhat)
prcomp adj b,test tukey nu(dfe) sigma(rmse)


How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


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