Chapter 1: histograms
Chapter 1: histograms
We will create histograms of simulated data from both of the examples described in the main text. The first is the easiest to quantify and describe: that of a movement reaction time experiment. Such experiments usually have a go-signal (auditory or visual) to signal that the movement should be made as quickly as possible, and whose timing is unpredictable. When the go-signal is given, the movement - most commonly a button-press or saccadic eye movement - is initiated. The timing data of interest here is the time elapsed between the go-signal and movement. A typical dataset in this experiment will often look skewed, with more data occurring to the right (longer reaction times) of the peak than to its left. This occurs because there is a physical limit to the fastest reaction time that can occur, owing to neural transduction and transmission speeds, muscle recruitment, and the impedance of the arm or eye. The same is not true for long reaction times, because there is no analogous limiting factor on the opposite side of the timing spectrum. In addition, because reaction times are intended to be fast relative to the minimum possible time, the peak of the histogram should be near the low end of the range of observed times. One can simulate reaction-time data with the rtrnd function:
dt=rtrnd(500);
where the function’s input is the desired number of simulated rt data. The output is a set of 500 (in this instance) numbers, given in seconds. We can get a sense of the distribution of simulated reaction times by plotting their histogram:
figure; subplot(2,1,1); hist(dt,31)
In the plot it is clear that the data do not occur symmetrically about the peak of the histogram, suggesting that they arise from a skewed distribution.
The second example above involved sleep/wake cycles. There are several ways to obtain information about sleep/wake cycles, including simply asking when one sleeps and wakes. However, in the scenario described above it would be necessary to obtain data without asking subjects to report times. One possible alternative would be to monitor their activity levels using accelerometers placed on limbs and torso. One could simply count the number of acceleration readings above a particular threshold. These would tend to display cycles that correspond to the sleep/wake times. Analysis of these cycles would allow us to determine whether the normal cycle was disturbed when external time signals were eliminated. First, we plot the histogram:
[times activitycount]=getactivitytimes(7); %7 days of activity recording
subplot(2,1,2); bar(times,activitycount,1); axis([times(1) times(end) 0 max(activitycount)+4])
Note the difference in the way the two histogram plots were created. In the first, the raw dataset was a list of times, and the ‘hist’ command sorts through those times to place them into a set of bins. The final histogram gives the counts representing the number of time-data whose values fall into each bin. In the second case, the raw data are the number of accelerometer readings exceeding a particular threshold, where the bins are still time-bins but are now simply a time-segmentation of the seven-day period shown in the plot.
In this case the data need not be sorted, because the times simply occur along with accelerometer data, and are counted as they occur. Because these data have both times and corresponding counts, they are plotted using the ‘bar’ function rather than ‘hist’. You can obtain more information on these commands by typing:
help hist
help bar
As a final note regarding the relationship between the hist and bar functions, we can ask the hist function for the set of bin-centers and counts that it used to create its histogram of times. This list of bin-centers and corresponding counts can then be used in the bar function to create the same histogram that was created originally by the hist function. The advantage to creating a histogram with the bar function in this indirect way is that the bar function allows greater control over the final look of the plot. In particular, we can easily change the widths of the bar (to make them overlap or to have a bit of space between them) or the colors of the bars (we have separate control over the edge and fill colors). Thus, we could make a nicer-looking version of our first plot by typing:
[counts, bincenters]=hist(dt,31);
Ecolor=[0 0 0]; %RGB coordinates of the color black
Fcolor=[.2 .2 .2]; %RBG coordinates of dark grey
barwidth=1; %width of 1 exactly fills the space between bins, .5 fills half the space, etc.
figure; bar(bincenters, counts,1,'FaceColor',Fcolor,'EdgeColor',Ecolor)
Or, if you prefer, a rather garish-looking version can be created with the commands:
Ecolor=[.8 .5 .4]; %RGB coordinates (which indicate the mix of Red, Green, and Blue light)
Fcolor=[.3 .3 .8]; %RBG coordinates
figure; bar(bincenters, counts,.8,'FaceColor',Fcolor,'EdgeColor',Ecolor,'LineWidth',1.1)