Two-dimensional Monte Carlo: Difference between revisions

From Opasnet
Jump to navigation Jump to search
(→‎Answer: a suggestion for non-marginal problem)
Line 23: Line 23:
== Rationale ==
== Rationale ==


<rcode name="mc2d" embed=1>
* 22.11.2017 Version where run2d==FALSE does nothing. [http://en.opasnet.org/en-opwiki/index.php?title=Special:RTools&id=QUs11wbaRAVtNLgs]
 
<rcode name="mc2d" embed=0>
#This is code Op_en7805/mc2d on page [[Two-dimensional Monte Carlo]]
#This is code Op_en7805/mc2d on page [[Two-dimensional Monte Carlo]]
library(OpasnetUtils)
library(OpasnetUtils)
Line 39: Line 41:
# Boostrap-versio:
# Boostrap-versio:


# Paramter list. Note: this is not stored, you have to define it in the model code.
# Parameter list. Note: this is not stored, you have to define it in the model code.


mc2dparam<- list(
mc2dparam<- list(
Line 52: Line 54:
mc2d <- function(ova) {
mc2d <- function(ova) {
   if(!exists("mc2dparam")) stop("Parameter list mc2dparam missing!\n")
   if(!exists("mc2dparam")) stop("Parameter list mc2dparam missing!\n")
   ova <- ova * mc2dparam$info
   if(mc2dparam$run2d) {
  require(reshape2)
    ova <- ova * mc2dparam$info
  marg <- setdiff(c(colnames(ova@output)[ova@marginal], mc2dparam$newmarginals), "Iter")
    require(reshape2)
  out <- aggregate(
    marg <- setdiff(c(colnames(ova@output)[ova@marginal], mc2dparam$newmarginals), "Iter")
    result(ova),
    out <- aggregate(
    by = ova@output[colnames(ova@output) %in% marg],
      result(ova),
    FUN = function(x) {  
      by = ova@output[colnames(ova@output) %in% marg],
      apply(
      FUN = function(x) {  
        array(
        apply(
          as.numeric(sample(as.character(x), length(x)*mc2dparam$N2, replace=TRUE)),
          array(
          #Numeric conversion is needed to prevent x from being interpreted as number of choices.
            as.numeric(sample(as.character(x), length(x)*mc2dparam$N2, replace=TRUE)),
          dim = c(length(x),mc2dparam$N)
            #Numeric conversion is needed to prevent x from being interpreted as number of choices.
        ),
            dim = c(length(x),mc2dparam$N)
        MARGIN=2, FUN = mc2dparam$fun
          ),
      )
          MARGIN=2, FUN = mc2dparam$fun
    }
        )
  )
      }
  temp <- melt(out[[length(out)]])
    )
  out[[length(out)]] <- 1:nrow(out)
    temp <- melt(out[[length(out)]])
  colnames(temp) <- c("Nrow","Iter","Result")
    out[[length(out)]] <- 1:nrow(out)
  out <- merge(out, temp, by.x = "x", by.y="Nrow")
    colnames(temp) <- c("Nrow","Iter","Result")
  out$x <- NULL
    out <- merge(out, temp, by.x = "x", by.y="Nrow")
  out <- Ovariable(
    out$x <- NULL
    output = out,  
    out <- Ovariable(
    marginal = colnames(out) %in% c(marg, "Iter")
      output = out,  
   )
      marginal = colnames(out) %in% c(marg, "Iter")
    )
  } else {
    out <- ova
   }
   return(out)
   return(out)
}
}

Revision as of 12:36, 22 November 2017



Question

How to perform two-dimensional Monte Carlo in Opasnet?

Answer

Use function mc2d to perform two-dimensional Monte Carlo. The function samples the current ovariable results by bootstrapping, applies an aggregate function to the samples, and then produces a new Iter index location for each sample. The function requires a parameter list mc2dparam, which contains the following parameters (with some example values):

  • N2 = 1000, # Number of iterations in the new Iter
  • run2d = TRUE, # Should the mc2d function be used or not?
  • info = 1, # An ovariable that may add new indices to the ovariable to be converted. If none, use 1.
  • newmarginals = c("Gender", "Ages", "Country"), # Names of columns that are non-marginals but will be sampled enough to become marginals. The function will produce an ovariable that correctly has these indices as marginals. However, if the function is used within an ovariable formula (which is typically the case), the marginal status is in the end inherited from parents and they may be re-converted to non-marginals. If this happens, the marginal status has to be updated in the assessment model code on case by case basis. Any automatic solution would violate the inheritance rules.
  • method = "bootstrap", # which method to use for 2D Monte Carlo? Currently bootsrap is the only option.
  • fun = mean # Function for aggregating the first Iter dimension.

You can call the function by using code

objects.latest("Op_en7805", code_name = "mc2d")

Rationale

  • 22.11.2017 Version where run2d==FALSE does nothing. [1]

+ Show code

See also