简体   繁体   中英

Simulation of timeseries in R

I need to make a simulation in R of the following function:

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

I also need to make a 3D plot. I'm new to R, and I therefore find it extremely difficult and hope that someone in here can help me.

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. you would have " Inf " of " -Inf " most y. The rest 'y' would be huge...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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