The Same Analysis in R, SPSS, and Julia

This book runs on R, but the ideas are not R’s property. A correlation is a correlation, a regression is a regression, whatever software you type it into. This appendix shows the core procedures from the book side by side in three environments: R, SPSS, and Julia. Click the tabs to switch languages. The R code is live - it runs here and produces the output you see - and the SPSS and Julia versions produce the same result, written the way each language likes to write it.

We use one small dataset throughout: 80 people in two groups, with a predictor x and an outcome y.

set.seed(1)
d <- data.frame(
  group = factor(rep(c("A", "B"), each = 40)),
  x     = rnorm(80, mean = 50, sd = 10)
)
d$y <- 100 + 0.8 * d$x + rnorm(80, sd = 12)   # y depends on x
head(d, 3)
group x y
A 43.73546 128.1643
A 51.83643 139.8470
A 41.64371 147.4520

In SPSS, imagine this same table loaded as the active dataset with variables group, x, and y. In Julia, imagine it as a DataFrame called d.

Descriptive Statistics

sapply(d[c("x", "y")], function(v) c(mean = mean(v), sd = sd(v)))
             x         y
mean 51.061465 139.68786
sd    9.008166  11.32289
DESCRIPTIVES VARIABLES=x y
  /STATISTICS=MEAN STDDEV MIN MAX.
using Statistics, DataFrames
describe(d[:, [:x, :y]], :mean, :std, :min, :max)

Correlation

cor(d$x, d$y)
[1] 0.350339
CORRELATIONS
  /VARIABLES=x y.
using Statistics
cor(d.x, d.y)

Comparing Two Groups (t-test)

t.test(y ~ group, data = d)

    Welch Two Sample t-test

data:  y by group
t = 1.9707, df = 77.864, p-value = 0.05232
alternative hypothesis: true difference in means between group A and group B is not equal to 0
95 percent confidence interval:
 -0.05031191  9.85201585
sample estimates:
mean in group A mean in group B 
       142.1383        137.2374 
T-TEST GROUPS=group('A' 'B')
  /VARIABLES=y.
using HypothesisTests
UnequalVarianceTTest(d.y[d.group .== "A"], d.y[d.group .== "B"])

Linear Regression

summary(lm(y ~ x, data = d))$coefficients
               Estimate Std. Error   t value     Pr(>|t|)
(Intercept) 117.2023564  6.9104281 16.960216 6.875787e-28
x             0.4403615  0.1333025  3.303475 1.443429e-03
REGRESSION
  /DEPENDENT y
  /METHOD=ENTER x.
using GLM, DataFrames
coeftable(lm(@formula(y ~ x), d))

One-Way ANOVA

summary(aov(y ~ group, data = d))
            Df Sum Sq Mean Sq F value Pr(>F)  
group        1    480   480.4   3.884 0.0523 .
Residuals   78   9648   123.7                 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ONEWAY y BY group
  /STATISTICS DESCRIPTIVES.
using GLM
m = lm(@formula(y ~ group), d)
ftest(m.model)          # overall F test for the group effect

Principal Components

# a small item matrix for the reduction example
set.seed(2); it <- sapply(1:4, function(i) rnorm(80) + d$x/10)
round(prcomp(it, scale. = TRUE)$sdev^2, 2)   # eigenvalues
[1] 2.20 0.79 0.59 0.42
FACTOR
  /VARIABLES x1 x2 x3 x4
  /EXTRACTION PC
  /ROTATION VARIMAX.
using MultivariateStats
M = fit(PCA, Matrix(it)'; maxoutdim = 4)
principalvars(M)        # variance along each component

A Note on the Three Environments

  • R is free, open source, and the language this book runs in. Everything you see executes.
  • SPSS is menu-driven, but every menu click also writes syntax - the code shown above. Pasting that syntax into a Syntax window and running it is the reproducible way to use SPSS, and it reads much like the R.
  • Julia is free and open source, fast, and increasingly used for heavy Bayesian and simulation work. Its statistics live in packages (Statistics, DataFrames, GLM, HypothesisTests, MultivariateStats), loaded with using.

The point is not that one is best. It is that a procedure you understand in one language you can carry to any of them, because the statistics were never about the software.