Skip to contents

Create an MPT model object from either a model file or model string. The model can be in the popular eqn format commonly used by MPT software or the easy format introduced by MPTinR.

Usage

make_mpt(
  file,
  type = c("easy", "eqn", "eqn2"),
  restrictions,
  trees,
  categories,
  text
)

Arguments

file

file name of model file.

type

model type. In the "easy" format, each line is assumed to correspond to the full equation for one response category. In the ".eqn" or "eqn2" format, each branch or path through the tree is given in one line (eqn is the default format for many other MPT tools). The default behaviour is to assume the model is in the "easy" format unless file ends with .eqn.

restrictions

list of parameter restrictions.

trees

For the "easy" format, the optional vector of tree names. Ignored otherwise.

categories

For the "easy" format, the optional vector of categories. Ignored otherwise.

text

alternative specification of model via text instead of a file. Ignored is file is not missing

Value

An object of class mpt_model which is a list with the following elements:

  1. model_df: A data.frame representation of the model (basically the .eqn format representation).

  2. list: The model in expression format, where each tree is one list.

  3. check: Vector of the summed probabilities in each tree (should each be 1 for a well constructed model).

  4. ns: Overview of model in numerical form.

  5. parameters: Vector with names of model parameters.

  6. pred: A function that generates predicted probabilities from the model.

Examples


## read-in model in easy format (with model specified as text)
u2htm <- "
# Old Items
Do + (1 - Do) * (1 - g1) * g2
(1 - Do) * g1
(1 - Do) * (1 - g1) * (1 - g2)

# New Items
(1 - Dn) * (1 - g1) * g2
(1 - Dn) * g1
Dn + (1 - Dn) * (1 - g1) * (1 - g2)
"

# need to specify tree names and category names
u2htsm_model <- make_mpt(text = u2htm,
                         trees = c("old", "new"),
                         categories = rep(c("old", "unsure", "new"), 2))
#> Warning: parameter names ending with a number amended with 'x'
u2htsm_model
#> 
#> MPT model with 4 independent categories (from 2 trees) and 4 parameters:
#>   Dn, Do, g1x, g2x
#> 
#> Tree 1: old
#>   Categories: old, unsure, new 
#>   Parameters: Do, g1x, g2x
#> Tree 2: new
#>   Categories: old, unsure, new 
#>   Parameters: Dn, g1x, g2x
#> 

# print model with eqn style
print(u2htsm_model, eqn = TRUE)
#> 
#> MPT model with 4 independent categories (from 2 trees) and 4 parameters:
#>   Dn, Do, g1x, g2x
#> 
#> Tree 1: old
#>   Categories: old, unsure, new 
#>   Parameters: Do, g1x, g2x
#> Tree 2: new
#>   Categories: old, unsure, new 
#>   Parameters: Dn, g1x, g2x
#> 
#> Model EQN:
#>   Tree Category               Equation
#> 1  old      old                     Do
#> 2  old      old     (1-Do)*(1-g1x)*g2x
#> 3  old   unsure             (1-Do)*g1x
#> 4  old      new (1-Do)*(1-g1x)*(1-g2x)
#> 5  new      old     (1-Dn)*(1-g1x)*g2x
#> 6  new   unsure             (1-Dn)*g1x
#> 7  new      new                     Dn
#> 8  new      new (1-Dn)*(1-g1x)*(1-g2x)
#> 

# brms family object
u2htsm_model$family
#> NULL

## write model as eqn file (add empty line on top):
if (FALSE) { # \dontrun{
write.table(u2htsm_model$df, file = "u2htm.eqn",
           quote = FALSE, row.names = FALSE, col.names = FALSE)
} # }

## create same model, but from EQN file
EQNFILE <- system.file("extdata", "u2htm.eqn", package = "mptstan")

u2htsm_model_b <- make_mpt(EQNFILE)
#> model type auto-detected as 'eqn'
#> Warning: parameter names ending with a number amended with 'x'
u2htsm_model_b
#> 
#> MPT model with 4 independent categories (from 2 trees) and 4 parameters:
#>   Dn, Do, g1x, g2x
#> 
#> Tree 1: old
#>   Categories: old, unsure, new 
#>   Parameters: Do, g1x, g2x
#> Tree 2: new
#>   Categories: old, unsure, new 
#>   Parameters: Dn, g1x, g2x
#>