r - non-numeric argument to binary operator, AR(1) model -
i have exercise have run following ar(1) model:
xi =c+φxi−1+ηi (i=1,...,t)
i know ni ~ n(0,1) ; x0 ~ n(c/(1-φ),1/(1-φˆ2)); c= 2 ; φ = 0.6 trying loop. code following:
n <- rnorm(t, 0, 1) c <- 2 phi <- 0.6 x_0 <- rnorm(1,c/(1-phi), 1/(1-phi**2)) v <- vector("numeric", 0) #for (i in 2:t){ name <- paste("x", i, sep="_") v <- c(v,name) v[1] <- c + phi*x_0 + n[1] v[i] <- c + phi*v[i-1] + n[i] }
however, keep getting error:
error in phi * v[i - 1] : non-numeric argument binary operator
i understand error is, can't find solutions solve it. please enlighten me? how assign numeric values name vector?
thank you!
you're defining v
numeric vector, v <- c(v, name)
turns v
character vector since name
character. that's what's causing error.
if i'm not mistaken, intent assign names values in numeric vector. that's fine, need different approach.
n <- rnorm(t) c <- 2 phi <- 0.6 x_0 <- rnorm(1, c/(1-phi), 1/(1-phi^2)) v <- c + phi*x_0 + n[1] (i in 2:t) { v[i] <- c + phi*v[i-1] + n[i] } names(v) <- paste("x", 1:t, sep="_")
vectors in r don't have static size; they're dynamically resized needed. though we're initializing v
scalar value, grows fit each new value in loop.
the final step give v
list of names. can accomplished using names(v) <-
. take @ v
now--it has names!
and aside, since t
synonym true
in r, it's best not use t
variable name. i've used t
here instead.
Comments
Post a Comment