def EstimateMarriageSurvival(dados, limiar, label=""): complete = dados[dados >= limiar].dropna() ongoing = dados[dados < limiar] hf = survival.EstimateHazardFunction(complete, ongoing, label=label) sf = hf.MakeSurvival() return hf, sf
def testSurvival(self): complete = [1, 2, 3, 4, 5] ongoing = [3, 4, 5] hf = survival.EstimateHazardFunction(complete, ongoing) self.assertAlmostEqual(hf[3], 1/6.0) self.assertAlmostEqual(hf[5], 0.5) sf = hf.MakeSurvival() self.assertAlmostEqual(sf[3], 0.625) self.assertAlmostEqual(sf[5], 0.234375)
def EstimateSurvival(resp): """Estimates the survival curve. resp: DataFrame of respondents returns: pair of HazardFunction, SurvivalFunction """ complete = resp[resp.notdivorced == 0].duration.dropna() ongoing = resp[resp.notdivorced == 1].durationsofar.dropna() hf = survival.EstimateHazardFunction(complete, ongoing) sf = hf.MakeSurvival() return hf, sf
def EstimateDivorceSurvival(resp): """Estimates the survival curve of marriages that end in divorce. resp: DataFrame of respondents return: pair of HazardFunction, SurvivalFunction """ # NOTE: filling in missing values would be better than dropping them complete = resp[resp.notdivorced == 0].duration.dropna() ongoing = resp[resp.notdivorced == 1].durationsofar.dropna() hf = survival.EstimateHazardFunction(complete, ongoing) sf = hf.MakeSurvival() return hf, sf
def EstimateMarriageSurvival(resp): """Estimates the survival curve. resp: DataFrame of respondents return: pair of HazardFunction, SurvivalFunction """ # NOTE: filling in missing values would be better than dropping them complete = resp6[resp6.evrmarry == 1].agemarry.dropna() ongoing = resp6[resp6.evrmarry == 0].age hf = survival.EstimateHazardFunction(complete, ongoing) sf = hf.MakeSurvival() return hf, sf
def SurvivalHaz(introductions, lifetimes): haz = survival.EstimateHazardFunction(lifetimes, introductions) sf = haz.MakeSurvival() # thinkplot.plot(sf,color='Grey') # plt.xlabel("Age (books)") # plt.ylabel("Probability of Surviving") # plt.title('Survial Function') # thinkplot.show() # thinkplot.plot(haz,color='Grey') # plt.title('Hazard Function') # plt.xlabel("Age (books)") # plt.ylabel("Percent of Lives That End") # thinkplot.show() return sf, haz
def EstSurvival(df): """ Survival curve estimate @param: df - dataframe of respondent data return: @param: hf - hazard function @param: sf - hazard function """ sad = df[df.notdivorced == 0].duration.dropna() happy = df[df.notdivorced == 1].duration.dropna() # married hf = survival.EstimateHazardFunction(sad, happy) sf = hf.MakeSurvival() return hf, sf
def EstimateSurvival(resp, cutoff=None): """Estimates the survival curve. resp: DataFrame of respondents cutoff: where to truncate the estimated functions returns: pair of HazardFunction, SurvivalFunction """ complete = resp.loc[resp.complete, 'complete_var'].dropna() ongoing = resp.loc[~resp.complete, 'ongoing_var'].dropna() hf = survival.EstimateHazardFunction(complete, ongoing) if cutoff: hf.Truncate(cutoff) sf = hf.MakeSurvival() return hf, sf
def SurvivalHaz(introductions, lifetimes, plot=False): """Given lists of ages and lifespans, this function calculates the Hazard and Survial curves. If plot is set True, it will plot them.""" haz = survival.EstimateHazardFunction(lifetimes, introductions) sf = haz.MakeSurvival() if plot: thinkplot.plot(sf, color='Grey') plt.xlabel("Age (books)") plt.ylabel("Probability of Surviving") plt.title('Survial Function') thinkplot.show() thinkplot.plot(haz, color='Grey') plt.title('Hazard Function') plt.xlabel("Age (books)") plt.ylabel("Percent of Lives That End") thinkplot.show() return sf, haz
def EstimateSurvival(resp, cutoff=None): """Estimates the survival curve. resp: DataFrame of respondents cutoff: where to truncate the estimated functions returns: pair of HazardFunction, SurvivalFunction """ complete = resp[resp.evrmarry].agemarry_index ongoing = resp[~resp.evrmarry].age_index hf = survival.EstimateHazardFunction(complete, ongoing) if cutoff: hf.Truncate(cutoff) sf = hf.MakeSurvival() return hf, sf
######################################### ## Age at first marriage ######################################### # clean dataframe and extract sub-groups we need resp6 = nsfg.ReadFemResp() resp6.cmmarrhx.replace([9997, 9998, 9999], np.nan, inplace=True) resp6['agemarry'] = (resp6.cmmarrhx - resp6.cmbirth) / 12.0 resp6['age'] = (resp6.cmintvw - resp6.cmbirth) / 12.0 complete = resp6[resp6.evrmarry == 1].agemarry.dropna() ongoing = resp6[resp6.evrmarry == 0].age ## estimate hazard function hf = survival.EstimateHazardFunction(complete, ongoing) thinkplot.Plot(hf) thinkplot.Show(xlabel='Age (years)', ylabel='Hazard') ## make survival function from hazard function sf = hf.MakeSurvival() thinkplot.Plot(sf) thinkplot.Show(xlabel='Age (years)', ylabel='Prob unmarried', ylim=[0, 1]) ## compute resampling survival with weights ResampleSurvival(resp6) ## show more data resp5 = survival.ReadFemResp1995() resp6 = survival.ReadFemResp2002() resp7 = survival.ReadFemResp2010()
def EstimateSurvival(resp): complete = resp[resp.notdivorced == 0].duration ongoing = resp[resp.notdivorced == 1].durationsofar hf = survival.EstimateHazardFunction(complete, ongoing) sf = hf.MakeSurvival() return hf, sf