如何在R中的多个试验中的特定时间获得随机观察点? [英] How to get a random observation point at a specific time over multiple trials in R?

查看:122
本文介绍了如何在R中的多个试验中的特定时间获得随机观察点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spike火车和我的代码来获得这样的火车:

I am working on Spike Trains and my code to get a spike train like this:

为20试验如下。该图像具有5个试验的代表性。

for 20 trials is written below. The image is representational for 5 trials.

fr = 100
dt = 1/1000 #dt in milisecond
duration = 2 #no of duration in s
nBins = 2000 #10msSpikeTrain
nTrials = 20 #NumberOfSimulations
MyPoissonSpikeTrain = function(p, fr= 100) {
 p = runif(nBins)
 q = ifelse(p < fr*dt, 1, 0)
 return(q)
  }

set.seed(1)
SpikeMat <- t(replicate(nTrials, MyPoissonSpikeTrain()))

plot(x=-1,y=-1, xlab="time (s)", ylab="Trial",
    main="Spike trains",
    ylim=c(0.5, nTrials+1), xlim=c(0, duration))
for (i in 1: nTrials)
 {
   clip(x1 = 0, x2= duration, y1= (i-0.2), y2= (i+0.4))
   abline(h=i, lwd= 1/4)
   abline(v= dt*which( SpikeMat[i,]== 1))
  }

每次试用在随机时间点出现峰值。现在,我要努力的工作是,获得一个随机样本时间点,该时间点适用于所有20个试验,并且我想获得针对每个试验的矢量,该矢量包括该点落入的间隔的长度。获取尖峰发生点的时间向量的代码是

Each trial has spikes occuring at random time points. Now what I am trying to work towards, is getting a random sample time point that works for all 20 trials and I want to get the vector consisting of length of the intervals this point falls into, for each trial. The code to get the time vector for the points where the spikes occur is,

A <- numeric()
for (i in 1: nTrials)
 {
  ISI <- function(i){
   spike_times <- c(dt*which( SpikeMat[i, ]==1))
   ISI1vec <- c(diff(spike_times))
   A <- c(A, ISI1vec)
   return(A)}
    }

然后,对于希望查看其间钉间隔矢量的任何试验,您都调用ISI(i)。我想要的内容的直观表示是:

Then you call ISI(i) for whichever trial you wish to see the Interspike interval vector for. A visual representation of what I want is:

我要对于每个试验,获得一个向量,该向量具有该点所在的间隔的长度。我也想弄清楚它的分布,但这将在以后提供。有人可以帮我弄清楚该如何编码吗?感谢您提供任何帮助,即使只是关于如何开始/在哪里寻找。

I want to get a vector that has the lengths of the interval where this points fall into, for each trial. I want to figure out it's distribution as well, but that's for later. Can anybody help me figure out how to code my way to this? Any help is appreciated, even if it's just about how to start/where to look.

推荐答案

您的数据

set.seed(1)
SpikeMat <- t(replicate(nTrials, MyPoissonSpikeTrain()))

我建议将稀疏矩阵数据转换为出现尖峰的索引列表

I suggest transforming your sparse matrix data into a list of indices where spikes occur

L <- lapply(seq_len(nrow(SpikeMat)), function(i) setNames(which(SpikeMat[i, ] == 1), seq_along(which(SpikeMat[i, ] == 1))))

随机时间点

set.seed(1)
RT <- round(runif(1) * ncol(SpikeMat)) 
# 531

结果

距离包含到两个最近尖峰的距离-每个元素列表中的是一个命名向量,其中值是距离(到 RT ),其名称是它们在向量中的位置。 nearest_columns 显示 SpikeMat 中每个峰值的原始时间点(列号)。

distances contains the distances to the 2 nearest spikes - each element of the list is a named vector where the values are the distances (to RT) and their names are their positions in the vector. nearest_columns shows the original timepoint (column number) of each spike in SpikeMat.

bookend_values <- function(vec) {
    lower_val <- head(sort(vec[sign(vec) == 1]), 1)
    upper_val <- head(sort(abs(vec[sign(vec) == -1])), 1)
    return(c(lower_val, upper_val))
}

distances <- lapply(L, function(i) bookend_values(RT-i))
nearest_columns <- lapply(seq_along(distances), function(i) L[[i]][names(distances[[i]])])

请注意,可以使用

sapply(distances, sum)

这篇关于如何在R中的多个试验中的特定时间获得随机观察点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆