简体   繁体   English

R中的时间序列模拟

[英]Simulation of timeseries in R

I need to make a simulation in R of the following function: 我需要在R中对以下函数进行仿真:

y[t] = 4 + (9*x[t-1])*y[y-1]+r[t]

I also need to make a 3D plot. 我还需要绘制3D图。 I'm new to R, and I therefore find it extremely difficult and hope that someone in here can help me. 我是R的新手,因此我觉得这非常困难,希望这里的人能为我提供帮助。

This is what I've tried so far: 到目前为止,这是我尝试过的:

library(rgl)
n <- 3000
x <- runif(n,-1,1)
r <- rnorm(n)
y <- rep(NA,n)
y[1] <- r[1]
for(t in 2:n){
y[t] <- 4 + (9*x[t-1])*y[t-1]+r[t]
}
D <- data.frame(y=y[-1], x1=x[-n],y1=y[-n])
open3d()
points3d(D$x1, D$y1, D$y, size=3, col="red")
aspect3d()
axes3d()

But I don't know if the simulation is done correctly, and nothing is shown when I plot the data points. 但是我不知道模拟是否正确完成,在绘制数据点时什么也没显示。 I use the 我用

you have y <- 4 + 9*x[t-1])*y[t-1] its rise like geometric progression 10^t (" 9 " coefficient) ... The problem in "9" and "4" coefficients. 您有y <- 4 + 9*x[t-1])*y[t-1]它的上升像几何级数10^t (“ 9 ”系数)...“ 9”和“ 4”中的问题系数。 you would have " Inf " of " -Inf " most y. 您最多将拥有--Inf的Inf ”。 The rest 'y' would be huge... 其余的“ y”将是巨大的...

Your simulation works fine, it's just that the values get too large too quickly. 您的模拟工作正常,只是值变得太大,太快。 I've removed the infinite values and logged the large ones: 我删除了无限值并记录了大值:

D <- D[!is.infinite(D$y) & !is.infinite(D$y1),]
D$y <- log(D$y)
D$y1 <- log(D$y1)
plot3d(D)

The above code successfully produces a 3D plot. 上面的代码成功生成了3D图。

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

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