'I have made this function which returns a tibble of 100 * 2. its working absolutely fine but I need to set two constants for the 1st two observations

Here's my code below.

 ar2 <- function(n , c ,phi1, phi2, sigma){
  
  if( (phi2 > -1) & (phi2 < 1) & (phi1 + phi2 < 1) & (phi2 - phi1 < 1)){
  y <- numeric(n)
  e <- rnorm(n , sigma)
  for(i in 3:n)
    y[i] <- phi1*y[i-1] + phi2*y[i-1] + e[i]
  sim <- tsibble(idx = seq_len(n), y = y, index = idx)}
  
  else{

    warning("Phis do not satisfy stationary constraints")
    
    y <- numeric(n)
    e <- rnorm(n , sigma)
    for(i in 3:n)
      y[i] <- phi1*y[i-1] + phi2*y[i-1] + e[i]
    sim <- tsibble(idx = seq_len(n), y = y, index = idx)
    }
  sim
}

If I run this function with these values below, it runs perfectly, I just don't want 1st two values to be zero in the output, instead I want them to be a constant, lets say for observation 1 = 7 & observation 2 = 12.

ar2(100 , 8 , 0.6 , 0.2 , 1)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source