Example Programs‎ > ‎

Statistics

Below is a comparison of the commands used to perform various statistical analyses in R, SAS, SPSS and Stata. For R functions that are not included in base R, the library() function loads the package that contains the function right before it is used. The variables gender and workshop are categorical factors and q1 to q4, pretest and posttest are considered continuous and normally distributed.

The practice data set is shown
here.
As with all examples, these program files and the data files they use are available here. Detailed step-by-step explanations are in the books along with the output of each analysis.


 Task R SAS SPSS Stata
Analysis of Variance
myModel <- aov(posttest ~ workshop,
  data = mydata100)
summary(myModel)
pairwise.t.test(posttest, workshop)
TukeyHSD(myModel, "workshop")
plot(TukeyHSD(myModel, "workshop"))
PROC GLM;
CLASS workshop;
MODEL posttest =
  workshop;
MEANS workshop /
  TUKEY;
UNIANOVA posttest  BY workshop
  /POSTHOC = workshop ( TUKEY )
  /PRINT = ETASQ HOMOGENEITY
  /DESIGN = workshop.
anova posttest workshop
Correlate,
  Pearson
cor( mydata[3:6],
  method = "pearson",
     use = "pairwise")
cor.test(mydata$q1,
  mydata$q2, use = "pairwise")

library("Rcmdr")
rcorr.adjust( mydata[3:6] )
PROC CORR; VAR q1-q4; RUN;

CORRELATIONS
  /VARIABLES=q1 TO q4.
correlate q*


Correlate,
  Spearman
cor( mydata[3:6],
  method = "spearman",
  use = "pairwise")
cor.test(mydata$q1,
  mydata$q2,
  use = "pairwise")

library("Rcmdr")
rcorr.adjust( mydata[3:6] )
PROC CORR SPEARMAN; VAR q1-q4;   
NONPAR CORR
  /VARIABLES=q1 to q4
  /PRINT=SPEARMAN.
spearman q*
Crosstabulation
 & Chi-squared
myWG <- table(workshop, gender)
chisq.test(myWG)

library("gmodels")
CrossTable(workshop, gender,
  chisq = TRUE,
 format = "SAS")
PROC FREQ;
TABLES workshop*gender /CHISQ;
CROSSTABS
  /TABLES=workshop BY
    gender
  /FORMAT= AVALUE TABLES
  /STATISTIC=CHISQ
  /CELLS= COUNT ROW
  /COUNT ROUND CELL.
tab gender workshop, row col exact
Descriptive Stats
summary(mydata)

library("Hmisc")
describe(mydata)
PROC MEANS;
VAR q1--posttest;

PROC UNIVARIATE; VAR q1--posttest;
DESCRIPTIVES VARIABLES=q1 to posttest
  /STATISTICS=MEAN STDDEV VARIANCE
   MIN MAX SEMEAN.

EXAMINE VARIABLES=q1 to posttest
  /PLOT BOXPLOT STEMLEAF NPPLOT
  /COMPARE GROUP
  /STATISTICS DESCRIPTIVES EXTREME
  /MISSING PAIRWISE.
summary q*

summary q*, detail
Frequencies   summary(mydata)

library("Deducer")
frequencies(mydata)
PROC FREQ;
TABLES workshop--q4;
FREQUENCIES VARIABLES=
  workshop TO q4.
tab1 workshop gender q*
Kruskal-Walliskruskal.test(posttest ~
  workshop)

pairwise.wilcox.test(posttest,
  workshop)
PROC npar1way;
  CLASS workshop;
  VAR posttest;
NPAR TESTS
  /K-W=posttest BY
   workshop(1 3).
kwallis q1, by(gender)
Regression, Linear
myModel <- lm(q4 ~ q1 + q2 + q3,
  data = mydata100)
summary(myModel)
plot(myModel)
PROC REG;
  MODEL q4=q1-q3;
REGRESSION
  /MISSING LISTWISE
  /STATISTICS COEFF OUTS R ANOVA
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN
  /DEPENDENT q4
  /METHOD=ENTER q1 q2 q3.
regress q4 q1-q3
lvr2plot
Sign Test
library("PASWR")
SIGN.test(posttest, pretest,  
  conf.level = .95)
myDiff=posttest-pretest;
PROC UNIVARIATE;
  VAR myDiff;

NPTESTS
  /RELATED TEST(q1 q2) SIGN
  /MISSING SCOPE=ANALYSIS USERMISSING=EXCLUDE
  /CRITERIA ALPHA=0.05 
   CILEVEL=95.
bitest posttest > pretest
t-Test, Independent
t.test(q1 ~ gender,
  data = mydata100)
PROC TTEST;
  CLASS gender;
  VAR q1;
T-TEST
  GROUPS = gender('m' 'f')
  /VARIABLES = q1.
ttest gender=q1, unpair unequ
t-Test, Paired
t.test(posttest, pretest,
  paired = TRUE)
PROC TTEST;
PAIRED pretest*
posttest;
T-TEST
  PAIRS=pretest WITH
  posttest (PAIRED).
 
 Variance Test
# Bartlett's
var.test(posttest ~ gender)

# Levene's
library("car")
levene.test(posttest, gender)
  robvar posttest, by(gender)

* Or...
sdtest posttest = gender
Wilcoxon Rank Sum (Mann-Whitney)
wilcox.test(q1 ~ gender,
  data = mydata100)
PROC NPAR1WAY;
  CLASS gender;
  VAR q1;
NPTESTS
  /RELATED TEST(pretest 
  posttest) SIGN WILCOXON.
ranksum posttest, by(gender)
Wilcoxon Signed Rank (Paired)
wilcox.test(posttest, pretest,
  paired = TRUE)
myDiff=posttest-pretest;
PROC UNIVARIATE;
  VAR myDiff;
NPTESTS
  /RELATED TEST(q1 q2) WILCOXON
  /MISSING SCOPE=ANALYSIS USERMISSING=EXCLUDE
  /CRITERIA ALPHA=0.05 CILEVEL=95.
signrank q1 = gender