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.83716 a 0
1.2414 a 0
1.053 b 0
-0.96077 b 0
-1.6302 c 0
-0.15546 c 0


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

y a z
-0.83716 a 0
1.2414 a 0
1.053 b 0
-0.96077 b 0
-1.6302 c 0
-0.15546 c 0


… or a summary of variables:

Key Type Values
y Var -1.63023, -0.960768, -0.837161, -0.155464, 1.05296, 1.24142
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.67705 a
S1 2.1915 b
S2 0.3462 c
S3 -0.8938 a
S4 1.2179 b
S5 -0.22032 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 1.0333
a 0.61647
a 0.65359
a -0.77592
a 0.2152
a -1.1321
a -0.10215
b -0.47641
b 0.77615
b -0.11093
b -1.1939
b -1.0134
b 0.39957
b -0.00026489
c 1.2807
c 0.2931
c 2.3684
c 1.5308
c 1.93
c 2.0482
c 0.34986


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 10.54 2 5.27 8.74** .002
Residuals 10.86 18 0.60
Total 21.39 20


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

Pairwise T-Tests (independent samples)

b c
a t12 = 0.75
p = .466
pc = .466
t12 = -3.09*
p = .009
pc = .028
b t12 = -3.97**
p = .002
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.75^{   \ \ \ }$\\
$p = .466$\\
$p_{c} = .466$ & $t_{12} = -3.09^{*  \ \ }$\\
$p = .009$\\
$p_{c} = .028$ \\
b &  & $t_{12} = -3.97^{** \ }$\\
$p = .002$\\
$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