12. Pivot Tables

NeuroTask.js offers a 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 pivottable(/*zipped or unzipped data*/data,/*string*/rows,
2                     /*string*/columns,/*(optional) object with key:function pairs*/func)  \
3           

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. They 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 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 store() 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:

  • mean
    Average
  • median
    Median
  • stddev
    Standard deviation
  • stderr
    Standard error
  • variance
    Variance
  • sumsqerr
    Sum of squared errors (i.e., sume of squared deviations from mean)
  • min
    Lowest value
  • max
    Highest value
  • range
    Difference between highest and lowest value

One or more of these functions can be called as follows:

1 pivottable([{x:1,y:2},{x:1,y:4},{x:2,y:3},{x:2,y:6}],
2            "x","y",{average:mean,n:count}) 

setpivottable(/string/response_variable_name, /string/rows,/string/columns, /optional object with key: function pairs/func,/string/name)

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 stored data array with getdata(name), where name is 'default' by default. The table is stored in the response object as response_variable_name.
_