Adding a Predictor
The addPredictor function allows you to update the original model with a new predictor. However note that it does an immutable update i.e. the original model is not updated, a new model is built from the original and returned.
Predictor Types
The new predictor is an object that can take one of two forms:
- A Categorical Predictor - Predictor that can only take a set of values
{ type: 'categorical'; name: string; betaCoefficent: number; referencePoint: number | undefined; categories: Array<{ category: string; //The human readable version of this category value: string; // The value of the category when passed into the engine }> }
- A Continuous Predictor - Predictor that can take a range of values
{ type: "continuous"; name: string; betaCoefficent: number; referencePoint: number | undefined; min: number | undefined; max: number | undefined; }
The following example shows how to add a new continuous predictor to the MPoRT model.
/*Assume this is initialized. Go to the Building Models section for information
on how to do this */
const mportSurvivalFunctions;
/* The new predictor we will add */
const airPollutionPredictor = {
type: 'continuous',
name: 'AirPollution';
betaCoefficent: Math.random();
referencePoint: undefined;
min: 1
max: undefined;
}
/*Modify the original mport model with the new predictor. Notice how we save
the result in a new variable. This is because we immutably update the old model */
const modifiedMportModel = mportSurvivalFunctions
.addPredictor(airPollutionPredictor);
/* Data we will use to calculate risk. For more information look at the
Evaluating Models section */
const data = [
{ name: 'age', coefficent: 21 },
{ name: 'sex', coefficent: 'male'},
{ name: 'AirPollution', coefficent: 5 }
];
const originalRiskToTime = mportSurvivalFunctions.getRiskToTime(data);
const newRiskToTime = modifiedMportModel.getRiskToTime(data);
/* This should log false since they are different models and hence their
risks will be different */
console.log(originalRiskToTime === newRiskToTime);