Esempio n. 1
0
 def get_stroke_probability(self, person):
     model_spec = load_model_spec("StrokeMIPartitionModel")
     strokePartitionModel = StatsModelLinearRiskFactorModel(
         RegressionModel(**model_spec))
     strokeProbability = scipySpecial.expit(
         strokePartitionModel.estimate_next_risk(person))
     return strokeProbability
    def __init__(
        self,
        mi_case_fatality=default_mi_case_fatality,
        stroke_case_fatality=default_stroke_case_fatality,
        mi_secondary_case_fatality=default_secondary_mi_case_fatality,
        stroke_secondary_case_fatality=default_secondary_stroke_case_fatality,
        secondary_prevention_multiplier=default_secondary_prevention_multiplier,
    ):
        self.mi_case_fatality = mi_case_fatality
        self.mi_secondary_case_fatality = (mi_secondary_case_fatality,)
        self.stroke_case_fatality = stroke_case_fatality
        self.stroke_secondary_case_fatality = stroke_secondary_case_fatality
        self.secondary_prevention_multiplier = secondary_prevention_multiplier

        model_spec = load_model_spec("StrokeMIPartitionModel")
        self.strokePartitionModel = StatsModelLinearRiskFactorModel(RegressionModel(**model_spec))
Esempio n. 3
0
    def setUp(self):
        popSize = 100
        age = np.random.normal(loc=70, scale=20, size=popSize)
        sbp = age * 1.05 + np.random.normal(loc=40, scale=30, size=popSize)
        df = pd.DataFrame({"age": age, "sbp": sbp})
        simpleModel = statsmodel.ols(formula="sbp ~ age", data=df)
        self.simpleModelResultSM = simpleModel.fit()
        self.simpleModelResult = RegressionModel(
            self.simpleModelResultSM.params.to_dict(),
            self.simpleModelResultSM.bse.to_dict(),
            self.simpleModelResultSM.resid.mean(),
            self.simpleModelResultSM.resid.std(),
        )

        self.person = Person(
            age=80,
            gender=NHANESGender.MALE,
            raceEthnicity=NHANESRaceEthnicity.NON_HISPANIC_WHITE,
            sbp=120,
            dbp=80,
            a1c=5.5,
            hdl=50,
            totChol=200,
            bmi=27,
            ldl=90,
            trig=150,
            waist=70,
            anyPhysicalActivity=0,
            education=Education.COLLEGEGRADUATE,
            smokingStatus=SmokingStatus.NEVER,
            alcohol=AlcoholCategory.NONE,
            antiHypertensiveCount=0,
            statin=0,
            otherLipidLoweringMedicationCount=0,
            creatinine=0.0,
            initializeAfib=initializeAfib,
        )

        self.people = [
            Person(
                age=80,
                gender=NHANESGender.MALE,
                raceEthnicity=NHANESRaceEthnicity.NON_HISPANIC_WHITE,
                sbp=bpinstance,
                dbp=80,
                a1c=5.5,
                hdl=50,
                totChol=200,
                bmi=27,
                ldl=90,
                trig=150,
                waist=70,
                anyPhysicalActivity=0,
                education=Education.COLLEGEGRADUATE,
                smokingStatus=SmokingStatus.NEVER,
                alcohol=AlcoholCategory.NONE,
                antiHypertensiveCount=0,
                statin=0,
                otherLipidLoweringMedicationCount=0,
                creatinine=0.0,
                initializeAfib=initializeAfib,
            ) for bpinstance in sbp
        ]
        for person in self.people:
            self.advancePerson(person)
        self.population_dataframe = init_vectorized_population_dataframe(
            self.people,
            with_base_gcp=True,
        )

        df2 = pd.DataFrame({
            "age":
            age,
            "sbp": [person._sbp[-1] for person in self.people],
            "meanSbp":
            [np.array(person._sbp).mean() for person in self.people],
        })

        self.meanModelResultSM = statsmodel.ols(formula="sbp ~ age + meanSbp",
                                                data=df2).fit()
        self.meanModelResult = RegressionModel(
            self.meanModelResultSM.params.to_dict(),
            self.meanModelResultSM.bse.to_dict(),
            self.meanModelResultSM.resid.mean(),
            self.meanModelResultSM.resid.std(),
        )

        df3 = pd.DataFrame({
            "age":
            age,
            "sbp": [person._sbp[-1] for person in self.people],
            "logMeanSbp":
            [np.log(np.array(person._sbp).mean()) for person in self.people],
        })

        self.logMeanModelResultSM = statsmodel.ols(
            formula="sbp ~ age + logMeanSbp", data=df3).fit()
        self.logMeanModelResult = RegressionModel(
            self.logMeanModelResultSM.params.to_dict(),
            self.logMeanModelResultSM.bse.to_dict(),
            self.logMeanModelResultSM.resid.mean(),
            self.logMeanModelResultSM.resid.std(),
        )

        race = np.random.randint(1, 5, size=popSize)
        df4 = pd.DataFrame({"age": age, "sbp": sbp, "raceEthnicity": race})
        df4.raceEthnicity = df4.raceEthnicity.astype("category")
        self.raceModelResultSM = statsmodel.ols(
            formula="sbp ~ age + raceEthnicity", data=df4).fit()
        self.raceModelResult = RegressionModel(
            self.raceModelResultSM.params.to_dict(),
            self.raceModelResultSM.bse.to_dict(),
            self.raceModelResultSM.resid.mean(),
            self.raceModelResultSM.resid.std(),
        )

        dfMeanAndLag = pd.DataFrame({
            "age":
            age,
            "sbp": [person._sbp[-1] for person in self.people],
            "meanSbp":
            [np.array(person._sbp).mean() for person in self.people],
            "lagSbp": [person._sbp[-1] for person in self.people],
        })

        self.meanLagModelResultSM = statsmodel.ols(
            formula="sbp ~ age + meanSbp + lagSbp", data=dfMeanAndLag).fit()
        self.meanLagModelResult = RegressionModel(
            self.meanLagModelResultSM.params.to_dict(),
            self.meanLagModelResultSM.bse.to_dict(),
            self.meanLagModelResultSM.resid.mean(),
            self.meanLagModelResultSM.resid.std(),
        )

        self.ageSbpInteractionCoeff = 0.02
        self.sbpInteractionCoeff = 0.5
        self.interactionModel = RegressionModel(
            {
                "meanSbp#age": self.ageSbpInteractionCoeff,
                "meanSbp": self.sbpInteractionCoeff,
                "Intercept": 0,
            },
            {
                "meanSbp#age": 0.0,
                "meanSbp": 0.0
            },
            0,
            0,
        )
Esempio n. 4
0
def load_regression_model(modelname):
    model_spec = load_model_spec(modelname)
    return RegressionModel(**model_spec)
    def __init__(self):
        self.mi_case_fatality = CVOutcomeDetermination.default_mi_case_fatality
        self.secondary_mi_case_fatality = CVOutcomeDetermination.default_secondary_mi_case_fatality
        self.stroke_case_fatality = CVOutcomeDetermination.default_stroke_case_fatality
        self.secondary_stroke_case_fatality = (
            CVOutcomeDetermination.default_secondary_stroke_case_fatality)
        self.secondary_prevention_multiplier = (
            CVOutcomeDetermination.default_secondary_prevention_multiplier)

        self.outcomeDet = CVOutcomeDetermination(
            self.mi_case_fatality,
            self.stroke_case_fatality,
            self.secondary_mi_case_fatality,
            self.secondary_stroke_case_fatality,
            self.secondary_prevention_multiplier,
        )

        # variable used in testing to control whether a patient will have a stroke or mi
        self.manualStrokeMIProbability = None

        self._models = {}
        femaleCVCoefficients = {
            "lagAge": 0.106501,
            "black": 0.432440,
            "lagSbp#lagSbp": 0.000056,
            "lagSbp": 0.017666,
            "current_bp_treatment": 0.731678,
            "current_diabetes": 0.943970,
            "current_smoker": 1.009790,
            "lagAge#black": -0.008580,
            "lagSbp#current_bp_treatment": -0.003647,
            "lagSbp#black": 0.006208,
            "black#current_bp_treatment": 0.152968,
            "lagAge#lagSbp": -0.000153,
            "black#current_diabetes": 0.115232,
            "black#current_smoker": -0.092231,
            "lagSbp#black#current_bp_treatment": -0.000173,
            "lagAge#lagSbp#black": -0.000094,
            "Intercept": -12.823110,
        }

        maleCVCoefficients = {
            "lagAge": 0.064200,
            "black": 0.482835,
            "lagSbp#lagSbp": -0.000061,
            "lagSbp": 0.038950,
            "current_bp_treatment": 2.055533,
            "current_diabetes": 0.842209,
            "current_smoker": 0.895589,
            "lagAge#black": 0,
            "lagSbp#current_bp_treatment": -0.014207,
            "lagSbp#black": 0.011609,
            "black#current_bp_treatment": -0.119460,
            "lagAge#lagSbp": 0.000025,
            "black#current_diabetes": -0.077214,
            "black#current_smoker": -0.226771,
            "lagSbp#black#current_bp_treatment": 0.004190,
            "lagAge#lagSbp#black": -0.000199,
            "Intercept": -11.679980,
        }

        self._models[OutcomeModelType.CARDIOVASCULAR] = {
            "female":
            ASCVDOutcomeModel(
                RegressionModel(
                    coefficients=femaleCVCoefficients,
                    coefficient_standard_errors={
                        key: 0
                        for key in femaleCVCoefficients
                    },
                    residual_mean=0,
                    residual_standard_deviation=0,
                ),
                tot_chol_hdl_ratio=0.151318,
                black_race_x_tot_chol_hdl_ratio=0.070498,
            ),
            "male":
            ASCVDOutcomeModel(
                RegressionModel(
                    coefficients=maleCVCoefficients,
                    coefficient_standard_errors={
                        key: 0
                        for key in maleCVCoefficients
                    },
                    residual_mean=0,
                    residual_standard_deviation=0,
                ),
                tot_chol_hdl_ratio=0.193307,
                black_race_x_tot_chol_hdl_ratio=-0.117749,
            ),
        }
        self._models[
            OutcomeModelType.GLOBAL_COGNITIVE_PERFORMANCE] = GCPModel()
        self._models[OutcomeModelType.DEMENTIA] = DementiaModel()

        # This represents non-cardiovascular mortality..
        nonCVModelSpec = load_model_spec("nhanesMortalityModelLogit")
        mortModel = StatsModelLogisticRiskFactorModel(
            RegressionModel(**nonCVModelSpec), False)
        # Recalibrate mortalitly model to align with life table data, as explored in notebook
        # buildNHANESMortalityModel
        mortModel.non_intercept_params[
            "age"] = mortModel.non_intercept_params["age"] * -1
        mortModel.non_intercept_params["squareAge"] = (
            mortModel.non_intercept_params["squareAge"] * 4)
        self._models[OutcomeModelType.NON_CV_MORTALITY] = mortModel
Esempio n. 6
0
    def __init__(self):
        self.mi_case_fatality = CVOutcomeDetermination.default_mi_case_fatality
        self.secondary_mi_case_fatality = CVOutcomeDetermination.default_secondary_mi_case_fatality
        self.stroke_case_fatality = CVOutcomeDetermination.default_stroke_case_fatality
        self.secondary_stroke_case_fatality = CVOutcomeDetermination.default_secondary_stroke_case_fatality
        self.secondary_prevention_multiplier = CVOutcomeDetermination.default_secondary_prevention_multiplier

        # variable used in testing to control whether a patient will have a stroke or mi
        self.manualStrokeMIProbability = None

        self._models = {}
        femaleCVCoefficients = {
            'lagAge': 0.106501,
            'black': 0.432440,
            'lagSbp#lagSbp': 0.000056,
            'lagSbp': 0.017666,
            'current_bp_treatment': 0.731678,
            'current_diabetes': 0.943970,
            'current_smoker': 1.009790,
            'lagAge#black': -0.008580,
            'lagSbp#current_bp_treatment': -0.003647,
            'lagSbp#black': 0.006208,
            'black#current_bp_treatment': 0.152968,
            'lagAge#lagSbp': -0.000153,
            'black#current_diabetes': 0.115232,
            'black#current_smoker': -0.092231,
            'lagSbp#black#current_bp_treatment': -0.000173,
            'lagAge#lagSbp#black': -0.000094,
            'Intercept': -12.823110
        }

        maleCVCoefficients = {
            'lagAge': 0.064200,
            'black': 0.482835,
            'lagSbp#lagSbp': -0.000061,
            'lagSbp': 0.038950,
            'current_bp_treatment': 2.055533,
            'current_diabetes': 0.842209,
            'current_smoker': 0.895589,
            'lagAge#black': 0,
            'lagSbp#current_bp_treatment': -0.014207,
            'lagSbp#black': 0.011609,
            'black#current_bp_treatment': -0.119460,
            'lagAge#lagSbp': 0.000025,
            'black#current_diabetes': -0.077214,
            'black#current_smoker': -0.226771,
            'lagSbp#black#current_bp_treatment': 0.004190,
            'lagAge#lagSbp#black': -0.000199,
            'Intercept': -11.679980
        }

        self._models[OutcomeModelType.CARDIOVASCULAR] = {
            "female":
            ASCVDOutcomeModel(RegressionModel(
                coefficients=femaleCVCoefficients,
                coefficient_standard_errors={
                    key: 0
                    for key in femaleCVCoefficients
                },
                residual_mean=0,
                residual_standard_deviation=0),
                              tot_chol_hdl_ratio=0.151318,
                              black_race_x_tot_chol_hdl_ratio=0.070498),
            "male":
            ASCVDOutcomeModel(RegressionModel(coefficients=maleCVCoefficients,
                                              coefficient_standard_errors={
                                                  key: 0
                                                  for key in maleCVCoefficients
                                              },
                                              residual_mean=0,
                                              residual_standard_deviation=0),
                              tot_chol_hdl_ratio=0.193307,
                              black_race_x_tot_chol_hdl_ratio=-0.117749)
        }
        self._models[
            OutcomeModelType.GLOBAL_COGNITIVE_PERFORMANCE] = GCPModel()

        # This represents non-cardiovascular mortality..
        self._models[
            OutcomeModelType.NON_CV_MORTALITY] = self.initialize_cox_model(
                "nhanesMortalityModel")