Learn R Programming

mfp2

Overview

mfp2 implements multivariable fractional polynomial (MFP) models and various extensions. It allows the selection of variables and functional forms when modelling the relationship of a data matrix x and some outcome y. Currently, it supports generalized linear models and Cox proportional hazards models. Additionally, it has the ability to model a sigmoid relationship between covariate x and an outcome variable y using approximate cumulative distribution (ACD) transformation- a feature that a standard fractional polynomial function cannot achieve.

Compatibility with existing software packages

mfp2 closely emulates the functionality of the mfp and mfpa package in Stata.

It augments the functionality of the existing mfp package in R by:

  • a matrix and a formula interface for input
  • sigmoid transformations via the ACD transformation
  • estimation and plotting of contrasts and partial linear predictors to investigate and visualize non-linear effects
  • various optimizations to increase speed and user friendliness

Installation

# Install the development version from GitHub
# install.packages("pak")
pak::pak("EdwinKipruto/mfp2")

# or 
# install.packages("remotes")
remotes::install_github("EdwinKipruto/mfp2")

References

To learn more about the MFP algorithm, a good place to start is the book by Royston, P. and Sauerbrei, W., 2008. Multivariable Model - Building: A Pragmatic Approach to Regression Analysis based on Fractional Polynomials for Modelling Continuous Variables. John Wiley & Sons.

For insights into the ACD transformation, please refer to Royston (2014). A smooth covariate rank transformation for use in regression models with a sigmoid dose–response function. The Stata Journal

Copy Link

Version

Install

install.packages('mfp2')

Monthly Downloads

159

Version

1.0.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Edwin Kipruto

Last Published

November 14th, 2023

Functions in mfp2 (1.0.0)

coef.mfp2

Extract coefficients from object of class mfp2
calculate_standard_error

Helper function to compute standard error of a partial predictor
find_best_fp_step

Function to estimate the best FP functions for a single variable
generate_combinations_with_replacement

Helper function to generate combinations with replacement
print_mfp_step

Function for verbose printing of function selection procedure (FSP)
fit_glm

Function that fits generalized linear models
fit_cox

Function that fits Cox proportional hazards models
fracplot

Plot response functions from a fitted mfp2 object
find_best_fp_cycle

Helper to run cycles of the mfp algorithm
prepare_newdata_for_predict

Helper function to prepare newdata for predict function
prostate

Prostate cancer dataset used in the Royston and Sauerbrei (2008) book.
print.mfp2

Print method for objects of class mfp2
fp

Helper to assign attributes to a variable undergoing FP-transformation
deviance_gaussian

Deviance computations as used in mfp in stata
summary.mfp2

Summarizing mfp2 model fits
select_ic

Function selection procedure based on information criteria
reset_acd

Helper to reset acd transformation for variables with few values
select_ra2_acd

Function selection procedure for ACD based on closed testing procedure
gbsg

Breast cancer dataset used in the Royston and Sauerbrei (2008) book.
find_shift_factor

Function that calculates a value used to shift predictor
fit_acd

Function to estimate approximate cumulative distribution (ACD)
pima

Pima Indians dataset used in the Royston and Sauerbrei (2008) book.
order_variables

Helper to order variables for mfp2 algorithm
name_transformed_variables

Helper function to name transformed variables
predict.mfp2

Predict Method for mfp2 Fits
transform_vector_fp

Functions to transform a variable using fractional polynomial powers or acd
fit_mfp

Function for fitting a model using the MFP or MFPA algorithm
generate_powers_fp

Function that generates a matrix of FP powers for any degree
fit_linear_step

Function to fit linear model for variable of interest
transform_vector_power

Simple function to transform vector by a single power
find_scale_factor

Function that calculates an integer used to scale predictor
transform_data_step

Function to extract and transform adjustment variables
select_ra2

Function selection procedure based on closed testing procedure
find_best_fpm_step

Function to find the best FP functions of given degree for a single variable
transform_matrix

Function to transform each column of matrix using final FP powers or acd
mfp2

Multivariable Fractional Polynomial Models with Extensions
get_selected_variable_names

Helper function to extract selected variables from fitted mfp2 object
select_linear

Helper to select between null and linear term for a single variable
generate_transformations_fp

Function to generate all requested FP transformations for a single variable
calculate_model_metrics

Function to compute model metrics to be used within mfp2
calculate_lr_test

Function to calculate p-values for likelihood-ratio test
calculate_f_test

Function to compute F-statistic and p-value from deviances
apply_acd

Function to apply Approximate Cumulative Distribution (ACD)
art

Artificial dataset with continuous response
calculate_df

Helper to calculates the final degrees of freedom for the selected model
apply_shift_scale

Shift and scale vector x
calculate_number_fp_powers

Calculates the total number of fractional polynomial powers in adjustment variables.
assign_df

Helper to assign degrees of freedom
convert_powers_list_to_matrix

Helper to convert a nested list with same or different length into a matrix
center_matrix

Simple function to center data
ensure_length

Helper function to ensure vectors have a specified length
create_dummy_variables

Simple function to create dummy variables for ordinal and nominal variables
create_fp_terms

Helper to create overview table of fp terms
find_best_fp1_for_acd

Function to fit univariable FP1 models for acd transformation
fit_model

Function that fits models supported by mfp2
fit_null_step

Function to fit null model excluding variable of interest