计算R中函数生成的数据的相关性 [英] Calculate correlation of data generated by function in R

查看:244
本文介绍了计算R中函数生成的数据的相关性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 R 中创建了以下功能:

  timeseriesmodel<  -  function ,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个值之间的相关性。 / p>

作为没有经验的 R 用户,我没有看到如何有效地操纵数据,并计算/估计所请求的数据点的相关性。

解决方案

这是复制的工作

 > set.seed(1)
>系列< - replicate(100,timeseriesmodel(250,1,0,1.2))#重复100次`timeseriesmodel`
> dim(系列)#每个结果是列列存储
[1] 250 100
> cor(系列[249,],系列[250,])#这里是元素249和250之间的相关性
[1] 0.9975532


I have created the following function in 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)}

This function returns a vector 'x' of length 'N', representing the data points of a random walk with drift.

In my case:

timeseriesmodel(250,1,0,1.2)

Now I should repeat this function 100 times, ending up with 100 timeseries data sets of length 250. Then I have to estimate the correlation between the 249th and 250th value of the dataset 'x', using the 100 sets.

As an inexperienced user of R, I don't see how to manipulate the data effectively and calculate/estimate the correlation of the requested data points. Help is very much appreciated.

解决方案

This is a work for 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

这篇关于计算R中函数生成的数据的相关性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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