vendredi 19 mars 2021

R: how to generate deviates that are autocorrelated with previously generated deviates

I have an R6 object generator like this:

library(R6)
library(MASS)

myClass <- R6Class("myClass",
                   private = list(
                     ..vals = vector(mode = "numeric", 2L),
                     ..estimate_vals = vector(mode = "numeric", 2L),
                     ..previous_estimate_vals = list()
                   ),
                   public = list(
                    initialize = function() {
                      private$..vals <- MASS::mvrnorm(1,
                                                     c(0, 0),
                                                     matrix(c(2, -1, -1, 2), 
                                                                       ncol = 2, 
                                                                       byrow = TRUE,
                                                                       dimnames = list(c("first", "second"),
                                                                                       c("first", "second"))))
                    }),
                   active = list(
                     update_estimate = function() {
                       
                       if (length(private$..previous_estimate_vals) < 1) {
                         private$..estimate_vals <- rnorm(n = length(private$..vals),
                                                          mean = private$..vals,
                                                          sd = 1.414214)
                       } else {
                         ####
                         ## private$..estimate_vals <- 
                         ## GENERATE DEVIATES THAT ARE AUTOCORRELATED WITH private$..previous_estimate_vals 
                         #### 
                       }
                       private$..previous_estimate_vals <- append(list(private$..estimate_vals),
                                                                  private$..previous_estimate_vals)
                       
                     }
                   )
)

When a new object is initialised, the private field ..vals is populated with a set of deviates drawn from a multivariate normal distribution. Once set, they are never changed:

obj <- myClass$new()

I then estimate the values in private$..vals using the active binding update_estimate:

obj$update_estimate

This function firstly checks if the values have been estimated previously by looking at the length of the private field ..previous_estimate_vals. If they haven't, it will estimate them, using rnorm and setting the mean as the values in private$..vals. If they have previously been estimated, I would like a new estimate to be drawn from the same distribution, but for the deviates to be autocorrelated with the private$previous_estimate_vals. In either case, the new estimates are appended to the list of previous estimates.

The idea is that the new deviates are biased by the previous deviates (I think I'm right in referring to this as autocorrelation...). But I don't know how I can do it.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire