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.21153 a 0
0.50351 a 0
2.3472 b 0
-0.74684 b 0
0.43753 c 0
0.14149 c 0


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

y a z
0.21153 a 0
0.50351 a 0
2.3472 b 0
-0.74684 b 0
0.43753 c 0
0.14149 c 0


… or a summary of variables:

Key Type Values
y Var -0.746844, 0.141487, 0.211534, 0.437533, 0.503505, 2.34721
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.12166 a
S1 1.8879 b
S2 -0.25106 c
S3 0.18723 a
S4 0.5883 b
S5 -1.1111 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.71708
a -0.20205
a 0.67982
a -0.047205
a 0.75833
a 0.13167
a -0.05084
b 0.26379
b 0.32878
b 0.74503
b 0.16783
b 1.3955
b 1.8905
b -1.1074
c 2.7768
c 1.3085
c 1.5854
c 2.6146
c 1.6215
c 3.2714
c 3.7174


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 19.03 2 9.52 14.56*** < .001
Residuals 11.76 18 0.65
Total 30.80 20


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

Pairwise T-Tests (independent samples)

b c
a t12 = -0.61
p = .553
pc = .553
t12 = -5.55***
p < .001
pc < .001
b t12 = -3.74**
p = .003
pc = .009
(* 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} = -0.61^{   \ \ \ }$\\
$p = .553$\\
$p_{c} = .553$ & $t_{12} = -5.55^{***}$\\
$p < .001$\\
$p_{c} < .001$ \\
b &  & $t_{12} = -3.74^{** \ }$\\
$p = .003$\\
$p_{c} = .009$ \\
\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