Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simulate Autoregressive Process

    I wanna simulate an AR process like this (see section 2).


    I've tried this
    Code:
    clear
    
    
    set obs 200
    
    
    cls
    
    
    set seed 1
    
    
    egen id = seq(), f(1) t(20) b(10)
    
    expand 10
    
    bys id: g time = _n
    
    xtset id time, g
    
    
    // 20 units, 100 periods each
    
    
    // R CODE: x1 <- 100 + arima.sim(model = list(ar = 0.999), n = 100)
    
    
    bys id: g x = 100 +runiform()
    
    bys id: g y = 1.2 * x + runiform()
    
    
    
    cls
    
    line y time if id ==4
    But I don't know how to simulate an AR process in the x variable. How might I do this?

    Note that I want this (as my code implies) to be a panel dataset, where multiple units have their own AR process. At present, the one I've created is noisier than I'd like, and I suspect knowing how to create an AR(1) term might be the key here.

  • #2
    Presumably.... the answer lies, in part, here https://blog.stata.com/2016/06/21/un...ests-in-stata/

    Comment


    • #3
      Hi Jared,

      Would it help to see the R code for arima.sim()?

      Code:
      > arima.sim
      
      function (model, n, rand.gen = rnorm, innov = rand.gen(n, ...),
      
          n.start = NA, start.innov = rand.gen(n.start, ...), ...)
      
      {
      
          if (!is.list(model))
      
              stop("'model' must be list")
      
          if (n <= 0L)
      
              stop("'n' must be strictly positive")
      
          p <- length(model$ar)
      
          if (p) {
      
              minroots <- min(Mod(polyroot(c(1, -model$ar))))
      
              if (minroots <= 1)
      
                  stop("'ar' part of model is not stationary")
      
          }
      
          q <- length(model$ma)
      
          if (is.na(n.start))
      
              n.start <- p + q + ifelse(p > 0, ceiling(6/log(minroots)),
      
                  0)
      
          if (n.start < p + q)
      
              stop("burn-in 'n.start' must be as long as 'ar + ma'")
      
          d <- 0
      
          if (!is.null(ord <- model$order)) {
      
              if (length(ord) != 3L)
      
                  stop("'model$order' must be of length 3")
      
              if (p != ord[1L])
      
                  stop("inconsistent specification of 'ar' order")
      
              if (q != ord[3L])
      
                  stop("inconsistent specification of 'ma' order")
      
              d <- ord[2L]
      
              if (d != round(d) || d < 0)
      
                  stop("number of differences must be a positive integer")
      
          }
      
          if (!missing(start.innov) && length(start.innov) < n.start)
      
              stop(sprintf(ngettext(n.start, "'start.innov' is too short: need %d point",
      
                  "'start.innov' is too short: need %d points"), n.start),
      
                  domain = NA)
      
          x <- ts(c(start.innov[seq_len(n.start)], innov[1L:n]), start = 1 -
      
              n.start)
      
          if (length(model$ma)) {
      
              x <- filter(x, c(1, model$ma), sides = 1L)
      
              x[seq_along(model$ma)] <- 0
      
          }
      
          if (length(model$ar))
      
              x <- filter(x, model$ar, method = "recursive")
      
          if (n.start > 0)
      
              x <- x[-(seq_len(n.start))]
      
          if (d > 0)
      
              x <- diffinv(x, differences = d)
      
          as.ts(x)
      
      }
      
      <bytecode: 0x7fe3d564ae10>
      
      <environment: namespace:stats>

      Comment


      • #4
        Perhaps! I'll dissect this later, then.

        Comment

        Working...
        X