To work with many model types simultaneously, multimedia uses a model class with the necessary mediation model functionality that wraps any specific implementation. The slots below define the generally required functionality for any specific implementation.
Slots
estimator
A function that takes a formula, input data frame X, and an response data.frame $Y$ and returns a model. For example, for the random forest model, this is created by wrapping
parallelize()
on the ranger() function for random forest estimation function using the 'ranger' package.estimates
A list containing the estimated model.
sampler
A function that supports sampling new responses from the estimated model.
model_type
A string specifying the type of model associated with the class. For example, "rf_model()" denotes a random forest model.
predictor
A function that returns fitted predictions given new inputs. For example, this can be the original predict() method for a multivariate response model, or it can be a loop over predicts for each feature in the mediation or outcome model.
Examples
m <- lm_model()
estimator(m)(mpg ~ hp + wt, data = mtcars)
#> $mpg
#>
#> Call:
#> f(formula = fmla, data = ..1)
#>
#> Coefficients:
#> (Intercept) hp wt
#> 37.22727 -0.03177 -3.87783
#>
#>
m <- rf_model()
estimator(m)(mpg ~ hp + wt, data = mtcars)
#> $mpg
#> Ranger result
#>
#> Call:
#> ranger(fmla, data, ...)
#>
#> Type: Regression
#> Number of trees: 500
#> Sample size: 32
#> Number of independent variables: 2
#> Mtry: 1
#> Target node size: 5
#> Variable importance mode: none
#> Splitrule: variance
#> OOB prediction error (MSE): 6.166786
#> R squared (OOB): 0.8302288
#>