knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Introduction

So far, we have passed particular data formats to dectree_expected_values, where we need to be explicit about which arguments they are associated with. Alternatively, we can define the model upfront and then pass this object to dectree_expected_values for it to dispatch to the appropriate function using S3.

Example

library(CEdecisiontree)
library(dplyr)
library(assertthat)

Let us create 3 input variables, in line with the 3 formats we have met already, using the define_model function.

model_transmat <- 
  define_model(transmat =
                 list(prob = matrix(data = c(NA, 0.5, 0.5), nrow = 1),
                      vals = matrix(data = c(NA, 1, 2), nrow = 1)
                 ))
model_transmat

model_tree <- 
  define_model(tree_dat =
                 list(child = list("1" = c(2, 3),
                                   "2" = NULL,
                                   "3" = NULL),
                      dat = data.frame(node = 1:3,
                                       prob = c(NA, 0.5, 0.5),
                                       vals = c(0, 1, 2))
                 ))
model_tree

model_long <- 
  define_model(dat_long = data.frame(from = c(NA,1, 1),
                                     to = c(1, 2, 3),
                                     prob = c(NA, 0.5, 0.5),
                                     vals = c(0, 1, 2)))
model_long

Now it is simply a case of passing this to main function call.

dectree_expected_values(model_transmat)
dectree_expected_values(model_tree)
dectree_expected_values(model_long)


n8thangreen/CEdecisiontree documentation built on Sept. 13, 2022, 5:25 a.m.