12 Pivot Tables
NeuroTask.js offers a exec.pivottable() command to summarize data, which is useful to give feedback to participants or to keep an eye on the progress of an experiment. It is also possible to use it as a component in more complex user interfaces and as preprocessing for charting (see below).
1 function exec.pivottable(/*zipped or unzipped data*/data,/*string*/rows,
2 /*string*/columns,/*(optional) object with key:function
3 pairs*/func)
A pivottable takes data from data and puts this in a table with rows and columns. The values are calculated with func, which is an object with key:function pairs. The keys are used to name the variable in which the result is stored, e.g., ‘mean’ or ‘variance’. Because ‘mean’ is the default value it does not have to be provided.
1 exec.pivottable([{x:1,y:2},{x:1,y:4},{x:2,y:3},{x:2,y:6}],"x","y")
returns
1 [{x:1,mean:3},{x:2,mean:4.5}]
The mean is taken over the y-values, which are collapsed by the pivot table. E.g., x could be trials in condition 1 and2, and y could be measurements, such as number of words recognized correctly, then the data would be stored as with the log() command as:
| x | y |
|---|---|
| 1 | 2 |
| 1 | 4 |
| 2 | 3 |
| 2 | 6 |
With a pivot table we can take the mean value over all repeated measurements in conditions 1 and 2, which
would give us this:
| x | mean |
|---|---|
| 1 | 3 |
| 2 | 4.5 |
Other operations can be used as well. Any function that takes an array as input and returns a single value can be used, including user-defined functions.
Several predefined functions are offered in stat.js that take a single array:
- stat.mean (Average)
- stat.median (Median)
- stat.stddev (Standard deviation)
- stat.stderr (Standard error)
- stat.variance (Variance)
- stat.sumsqerr (Sum of squared errors,i.e., sum of squared deviations from mean)
- stat.min (Lowest value)
- stat.max (Highest value)
- stat.range (Difference between highest and lowest value)
- stat.count (Number of items in the array that are not
undefinedornull)
There are also other useful functions in the stat package, not taking a single array:
* stat.Z (gives z-score from a p value, as in var z = stat.Z(0.05) )
* stat.dprime (gives the d’ value based on the number of Hits and False Alarms, as in var d = stat.dprime(20,3) )
* stat.tdist (gives one-tailed t distribution p-value, based on t-score value and degrees of freedom, as stat.tdist(.80,6) )
* stat.ttest (t-test for two samples returning p-value, one-tailed and non-paired, as in stat.ttest([1,2,3,4],[2,3,4,5]) )
You could use these functions to give feedback to subjects.
One or more of the single-array-taking functions can be called with a pivot table as follows:
exec.pivottable([{x:1,y:2},{x:1,y:4},{x:2,y:3},{x:2,y:6}],
"x","y",{average:stat.mean,n:stat.count})
The can also be used directly in a script. For example:
var v = stat.stddev([1,2,3,4,5,6]);
console.log("SD = ",v);
This is the run-time variant of pivottable() that will add this statement to the execution flow of the experiment and puts the result in the response object. Data is taken from logged data array with getdata(name), where name is 'default' by default. The table is stored in the response object as response_variable_name.