Dataset basics

# Author: Christian Brodbeck <christianbrodbeck@nyu.edu>
from eelbrain import *
import numpy

A dataset can be constructed column by column, by adding one variable after another:

# initialize an empty Dataset:
ds = Dataset()
# numeric values are added as Var object:
ds['y'] = Var(numpy.random.normal(0, 1, 6))
# categorical data as represented in Factors:
ds['a'] = Factor(['a', 'b', 'c'], repeat=2)
# A variable that's equal in all cases can be assigned quickly:
ds[:, 'z'] = 0.
# check the result:
ds
y a z
-0.79831 a 0
1.3289 a 0
-1.1348 b 0
-0.61174 b 0
0.87851 c 0
-0.78987 c 0


For larger datasets it can be more convenient to print only the first few cases…

y a z
-0.79831 a 0
1.3289 a 0
-1.1348 b 0
-0.61174 b 0
0.87851 c 0
-0.78987 c 0


… or a summary of variables:

Key Type Values
y Var -1.13478, -0.798306, -0.789865, -0.611745, 0.878511, 1.32892
a Factor a:2, b:2, c:2
z Var 0:6
Dataset: 6 cases


An alternative way of constructing a dataset is case by case (i.e., row by row):

rows = []
for i in range(6):
    subject = f'S{i}'
    y = numpy.random.normal(0, 1)
    a = 'abc'[i % 3]
    rows.append([subject, y, a])
ds = Dataset.from_caselist(['subject', 'y', 'a'], rows, random='subject')
ds
subject y a
S0 -0.073693 a
S1 0.057158 b
S2 0.42812 c
S3 0.88345 a
S4 -0.69126 b
S5 -0.76437 c


Example

Below is a simple example using data objects (for more, see the Examples):

y = numpy.empty(21)
y[:14] = numpy.random.normal(0, 1, 14)
y[14:] = numpy.random.normal(2, 1, 7)
ds = Dataset({
    'a': Factor('abc', 'A', repeat=7),
    'y': Var(y, 'Y'),
})
ds
a y
a -0.91354
a 0.13757
a -0.80733
a -1.7571
a -0.27913
a -2.2453
a -1.1887
b 0.37502
b -1.0636
b -1.2591
b 0.19883
b 1.1008
b -0.42098
b -1.2403
c 2.359
c 1.718
c 2.7439
c 1.513
c 3.6197
c 2.0336
c 3.3641


table.frequencies('a', data=ds)
a n
a 7
b 7
c 7


test.ANOVA('y', 'a', data=ds)
SS df MS F p
a 47.84 2 23.92 33.22*** < .001
Residuals 12.96 18 0.72
Total 60.80 20


test.pairwise('y', 'a', data=ds, corr='Hochberg')

Pairwise T-Tests (independent samples)

b c
a t12 = -1.46
p = .171
pc = .171
t12 = -8.04***
p < .001
pc < .001
b t12 = -6.09***
p < .001
pc < .001
(* Corrected after Hochberg, 1988)


t = test.pairwise('y', 'a', data=ds, corr='Hochberg')
print(t.get_tex())
\begin{center}
\begin{tabular}{lll}
\toprule
 & b & c \\
\midrule
a & $t_{12} = -1.46^{   \ \ \ }$\\
$p = .171$\\
$p_{c} = .171$ & $t_{12} = -8.04^{***}$\\
$p < .001$\\
$p_{c} < .001$ \\
b &  & $t_{12} = -6.09^{***}$\\
$p < .001$\\
$p_{c} < .001$ \\
\bottomrule
\end{tabular}
\end{center}
p = plot.Boxplot('y', 'a', data=ds, title="My Boxplot", ylabel="value", corr='Hochberg')
My Boxplot

Gallery generated by Sphinx-Gallery