【问题标题】:Calculate correlation of data generated by function in R计算R中函数生成的数据的相关性
【发布时间】:2013-10-03 14:54:55
【问题描述】:

我在 R 中创建了以下函数:

timeseriesmodel <- function(N, x0, delta, variance) {
      z<-cumsum(rnorm(n=N, mean=0, sd=sqrt(variance)))
      t<-1:N
      x<-x0+t*delta+z
      return(x)}

此函数返回一个长度为“N”的向量“x”,表示带有漂移的随机游走的数据点。

就我而言:

timeseriesmodel(250,1,0,1.2)

现在我应该重复这个函数 100 次,最终得到 100 个长度为 250 的时间序列数据集。然后我必须使用这 100 个数据集来估计数据集“x”的第 249 和第 250 个值之间的相关性。

作为一个没有经验的 R 用户,我不知道如何有效地操作数据并计算/估计所请求数据点的相关性。非常感谢您的帮助。

【问题讨论】:

    标签: r dataset time-series correlation random-walk


    【解决方案1】:

    这是replicate的作品

    > set.seed(1)
    > Series <- replicate(100, timeseriesmodel(250,1,0,1.2) )  # repeating 100 times `timeseriesmodel`
    > dim(Series)   # each result is store column-wise
    [1] 250 100
    > cor(Series[249,], Series[250,] ) # here's the correlation between element 249 and 250
    [1] 0.9975532
    

    【讨论】:

      猜你喜欢
      • 2020-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 2018-06-11
      相关资源
      最近更新 更多