简体   繁体   English

使用 r 模拟多变量时间序列数据

[英]Simulation of multivariate time series data using r

I want to simulate a multivariate time series data which fits to the year given in the table.我想模拟一个适合表中给出的年份的多元时间序列数据。 How can I do that?我怎样才能做到这一点?

I use the following code我使用以下代码

Z <- rnorm(100, mean = 0, sd = 1.5)

# process simulation
X <- c()
for (i in 1:length(Z)) {

# my table
my_table <- 1982:2008 

You will need the covariance or correlation of series.您将需要序列的协方差或相关性。 If there is not correlation it would be easy.如果没有相关性,那将很容易。

# No correlation: 
my_table <- 1982:2008
num.series <- 4

sdata=matrix(rnorm(length(my_table)*num.series,0,sd=1.5),ncol=num.series)
sdf=data.frame(my_table,sdata)
summary(sdf)

# with covariance
library(MASS)
Sigma <- matrix(c(10,3,3,3,3,5,3,3,3,3,8,3,3,3,3,15)/10,ncol=4)
Sigma
smdata=mvrnorm(n = length(my_table), rep(0, num.series), Sigma)
scdf=data.frame(my_table,smdata)
summary(scdf)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM