Create FTM object
create_FTM.RmdCreate FTM objects
FTM object can be created from lm, glm, and glmnet models using the
createFTM function. This is a wrapper that passes the model
to specific functions e.g. createFromLm to create FTM
object from a lm object. These functions can be called directly, but
will return an error if you pass in a model that is not supported.
Create FTM object from linear model
Here, we create a linear model (using lm), then convert it to a FTM object.
# Load the mtcars dataset
data(mtcars)
# Fit a linear model to the mtcars dataset, predicting mpg from cyl, hp, and wt
lm_model <- lm(mpg ~ cyl + hp + wt, data = mtcars)
# Create FTM object using createFTM
ftmlm_model <- createFTM(lm_model) # same as using createFromLm
# A quick look at the model (shows a ftmlm model)
show(ftmlm_model)
#> Flexible Transfer Model - Linear Model
#> ------------------------------------------------------
#> Number of predictors: 3
#> Optimal lambda: 0.000000
#>
#> Formula:
#> mpg ~ `(Intercept)` + cyl + hp + wt
#>
#> Coefficients:
#> (Intercept) cyl hp wt
#> 38.752 -0.942 -0.018 -3.167
#>
#> R-squared: 0.843150Create FTM object from a generalized linear model
Just like creating an FTM object from a linear model, we can create FTM objects from a generalized linear model (binomial family).
# Create a logistic model using the mtcars dataset
glm_model <- glm(am ~ hp + wt + cyl, data = mtcars, family = "binomial")
# Create FTM object using createFTM
ftmglm_model <- createFTM(glm_model)
# A quick look at the model (shows a ftmglm model)
show(ftmglm_model)
#> Flexible Transfer Model - Generalized Linear Model
#> ------------------------------------------------------
#> Number of predictors: 3
#>
#> Formula:
#> am ~ `(Intercept)` + hp + wt + cyl
#>
#> Coefficients:
#> (Intercept) hp wt cyl
#> 19.7029 0.0326 -9.1495 0.4876Create FTM object from glmnet model (both linear and binomial models)
Creating a FTM object from a glmnet model is just as easy. However,
due to the way glmnet models are stored, we need to manually pass in the
predictor and outcome data. It is recommended to use cv.glmnet in order
to get the optimal lambda (by default, the lambda.min will
be used).
# glmnet requires input data to be in matrices
predictors <- as.matrix(mtcars[, c("hp", "wt", "cyl")])
# Fitting a cv.glmnet to the mtcars dataset
glmnet_model <- glmnet::cv.glmnet(predictors, mtcars$am, family = "binomial", alpha = 0)
# Create FTM object using createFTM
ftmglmnet_model <- createFTM(glmnet_model, x = predictors, y = mtcars$am)
# A quick look at the model (shows a ftmglm model)
show(ftmglmnet_model)
#> Flexible Transfer Model - Generalized Linear Model
#> ------------------------------------------------------
#> Number of predictors: 3
#>
#> Formula:
#> am ~ `(Intercept)` + hp + wt + cyl
#>
#> Coefficients:
#> (Intercept) hp wt cyl
#> 6.2245 0.0116 -2.0526 -0.3230
# We can also fit a linear model using glmnet
glmnet_model <- glmnet::cv.glmnet(predictors, mtcars$mpg, family = "gaussian", alpha = 0)
# Create FTM object using createFTM
ftmglmnet_model <- createFTM(glmnet_model, x = predictors, y = mtcars$mpg)
# A quick look at the model (shows a ftmlm model)
show(ftmglmnet_model)
#> Flexible Transfer Model - Linear Model
#> ------------------------------------------------------
#> Number of predictors: 3
#> Optimal lambda: 0.514698
#>
#> Formula:
#> mpg ~ `(Intercept)` + hp + wt + cyl
#>
#> Coefficients:
#> (Intercept) hp wt cyl
#> 35.2179 -0.0191 -2.0949 -0.9021
#>
#> R-squared: 0.812460