Say that I have two conditions, 'a' and 'b'. A neuron fires on average 40 spikes / second (Hz) in condition 'a' and 80 spikes / second in condition 'b'. The response to condition 'a' is presented 20 times and condition 'b' is presented 10 times, with each presentation for 1000 ms.
AB <- rbind(
ldply( 1:20,
function(trial) {
data.frame(
trial=trial,
cond=factor('a',c('a','b')),
spiketime = runif(40,0,1000))
}
), ldply(21:30,
function(trial) {
data.frame(
trial=trial,
cond=factor('b',c('a','b')),
spiketime = runif(80,0,1000))
}
)
)
A simple histogram can be plotted with:
qplot(spiketime, data=AB, geom='line',stat='bin',y=..count..,
xlim=c(0,1000), colour=cond, binwidth=100,xlab='Milliseconds')
However, this does not average over the presentations, and hence, the values on the y axis are about the same. I would like to plot the spiking rate (spikes/second) along the y-axis, which would show that condition 'b' elicits about twice as many spikes per second. The spiking rate does not increase as number of presentations increase, it just becomes less noisy. Is there a way to do this without pre-processing the dataframe AB?
In other words, can I do something along the lines of:
qplot(spiketime, data=AB, geom='line',stat='bin',
y=..count../num_presentations*1000/binwidth, ylab='Spikes per second',
xlim=c(0,1000), colour=cond, binwidth=100,xlab='Milliseconds')
where num_presentations would be 20 for condition 'a' and 10 for condition 'b' and 1000/binwidth would just be a constant to get the unit correct?