if __name__ == '__main__': # Bladder Example #raw_data = pd.read_csv('datas/bladder_recurrent.csv', sep=';') raw_data = pd.read_csv('datas/T45.csv', sep=';') raw_data.rename(columns={'Patient number': 'Sample', 'Event': 'Number', 'Censored': 'Event', 'Treatment group': 'Drug'}, inplace=True) raw_data['Drug'] = raw_data['Drug'].map({1: 'Placebo', 2: 'Pyridoxine', 3: 'Thiotepa'}) raw_data['Event'] = 1 - raw_data['Event'] tb = print_data(raw_data) print tb df = transform_data(raw_data) lt = mcf(df, robust=True, positive=False) covariate = 'Drug' cohort1, cohort2 = 'Placebo', 'Thiotepa' group_df = df.set_index(covariate) group_lt = df.groupby(covariate).apply(lambda sfr: mcf(sfr, robust=True, positive=False)) df1, df2 = group_df.ix[cohort1], group_df.ix[cohort2] lt1, lt2 = group_lt.ix[cohort1], group_lt.ix[cohort2] lt_diff = mcfdiff(lt1, lt2) # Plot, no stratification fig, (ax1, ax2) = plt.subplots(2, 1) plot_data(df, ax=ax1)
plot_data, plot_datas, plot_mcf, plot_mcfs, plot_mcfdiff, plot_mcfdiffs if __name__ == '__main__': # Childbirths Example raw_data = pd.read_csv('datas/childbirths_interval.csv', sep=';') #, usecols=[0,1,2]) cohort1, cohort2 = 'Male', 'Female' df1 = transform_population( raw_data[raw_data['Sex'] == cohort1]).fillna(cohort1) df2 = transform_population( raw_data[raw_data['Sex'] == cohort2]).fillna(cohort2) df = pd.concat((df1, df2)) lt1 = mcf(df1, robust=False, positive=False) lt2 = mcf(df2, robust=False, positive=False) lt_diff = mcfdiff(lt1, lt2) # Plot, stratified fig, (ax1, ax2, ax3) = plt.subplots(1, 3, sharey=False, sharex=True) plot_mcf(lt1, ax=ax1, interval=True, CI=True, label=cohort1) plot_mcfs([(cohort1, lt1), (cohort2, lt2)], ax=ax2, interval=True, CI=False) plot_mcfdiff(lt_diff, ax=ax3, label='%s vs. %s' % (cohort1, cohort2)) ax1.set_ylim([0, 2]) ax2.set_ylim([0, 2]) ax3.set_ylim([-1, 1]) plt.xlim([0, 60])
if __name__ == '__main__': # Cook (2008, pg. 299) Example 8.1: Field Repair data # This dataset (see Appendix D) gives simulated data on unscheduled repairs # for a fleet of m = 134 large utility vehicles operated by a city. The data were # collected over a three-year period on new vehicles which were purchased and # placed in service over the first two years of the study. Time is measured in # years from the start of the study, and costs are in hundreds of dollars. raw_data = generate_data() df = transform_data(raw_data) # Table D.4. # Marked point process (8.14) as mcf1 and cumulative cost process (8.12) as mcf2 lt1 = mcf(df, robust=True, positive=False) mcf1 = mcfcost(df, mcf_compound=lt1, robust=False, positive=False) mcf2 = mcfcost(df, robust=True, positive=False) # Plot data plot_data(df, alpha=0.5, marker='.', plot_costs=False) # Plot MCFs fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=False) plot_mcf(mcf1, ax=ax1, cost=True, label='Events * Cost') plot_mcf(mcf2, ax=ax2, cost=True, label='Cost') # Table 8.1. mcf1['E[C].Std'] = np.sqrt(mcf1['E[C].Var']) mcf2['E[C].Std'] = np.sqrt(mcf2['E[C].Var']) mcf1 = mcf1[['Time', 'E[C]', 'E[C].Std']]
'Patient number': 'Sample', 'Event': 'Number', 'Censored': 'Event', 'Treatment group': 'Drug' }, inplace=True) raw_data['Drug'] = raw_data['Drug'].map({ 1: 'Placebo', 2: 'Pyridoxine', 3: 'Thiotepa' }) raw_data['Event'] = 1 - raw_data['Event'] print raw_data.head() df = transform_data(raw_data) lt = mcf(df, robust=True, positive=False) covariate = 'Drug' cohort1, cohort2 = 'Placebo', 'Thiotepa' group_df = df.set_index(covariate) group_lt = df.groupby(covariate).apply( lambda sfr: mcf(sfr, robust=True, positive=False)) df1, df2 = group_df.ix[cohort1], group_df.ix[cohort2] lt1, lt2 = group_lt.ix[cohort1], group_lt.ix[cohort2] lt_diff = mcfdiff(lt1, lt2) # Plot, no stratification fig, (ax1, ax2) = plt.subplots(2, 1)
import numpy as np import pandas as pd import matplotlib.pyplot as plt from data_operations import transform_data, mcf, mcfdiff, mcfequal,\ plot_data, plot_datas, plot_mcf, plot_mcfs, plot_mcfdiff, plot_mcfdiffs if __name__ == '__main__': # Transmission Example data = pd.read_csv('datas/transmission_recurrent.csv', sep=';') df = transform_data(data, format='recurrent') lt = mcf(df, robust=True, positive=False) print lt[['Time', 'Y', 'N', 'dE[N]', 'E[N]', 'E[N].lcl', 'E[N].ucl']].head() covariate = 'Transmission' cohort1, cohort2 = 'Automatic', 'Manual' group_df = df.set_index(covariate) group_lt = df.groupby(covariate).apply( lambda sfr: mcf(sfr, robust=True, positive=True)) #, interval=True)) df1, df2 = group_df.ix[cohort1], group_df.ix[cohort2] lt1, lt2 = group_lt.ix[cohort1], group_lt.ix[cohort2] lt_diff = mcfdiff(lt1, lt2) # Plot, no stratification fig, (ax1, ax2) = plt.subplots(2, 1) plot_data(df, ax=ax1)
import numpy as np import pandas as pd import matplotlib.pyplot as plt from data_operations import transform_population, mcf, mcfdiff, mcfequal,\ plot_data, plot_datas, plot_mcf, plot_mcfs, plot_mcfdiff, plot_mcfdiffs, population_reverse if __name__ == '__main__': # Defrost Control Example raw_data = pd.read_csv('datas/defrost_interval.csv', sep=';')#, usecols=[0,1,2]) df = transform_population(raw_data) lt1 = mcf(df, robust=False, positive=False, interval=True) print df.head() fr = population_reverse(df) plot_data(fr, alpha=0.005) fig, (ax1) = plt.subplots(1, 1, sharex=True, sharey=True) plot_mcf(lt1, ax=ax1, interval=True) plt.ylim([0.0, 0.1]) plt.show() #lt_cost = mcf_cost(fr, mcf_compound=lt) #print lt_cost.head() #plot_cost(mcf_cost)
import numpy as np import pandas as pd import matplotlib.pyplot as plt from data_operations import transform_population, mcf, mcfcost, mcfdiff, mcfequal,\ plot_data, plot_datas, plot_mcf, plot_mcfs, plot_mcfdiff, plot_mcfdiffs, population_reverse, population_reverse_costs if __name__ == '__main__': # Gearboxes Example raw_data = pd.read_csv('datas/gearbox_recurrent.csv', sep=';') #, usecols=[0,1,2]) raw_data = raw_data[raw_data['SaleMonth'].isin([1, 2, 3, 4])] df = transform_population(raw_data) lt = mcf(df, robust=False, positive=False, interval=True) #ct = mcfcost(df, robust=False, positive=False, interval=True) #lt_cost = mcf_cost(fr, mcf_compound=lt) #print lt_cost.head() #plot_cost(mcf_cost) covariate = 'SaleMonth' group_df = raw_data.groupby(covariate).apply( lambda sfr: transform_population(sfr)) dfs = list(group_df.groupby(level=0)) lts = [(cohort, mcf(cohort_df, robust=False, positive=False)) for cohort, cohort_df in dfs] cts = [(cohort, mcfcost(cohort_df, mcf_compound=cohort_mcf, robust=False, positive=False)) for\ (cohort, cohort_df), (cohort, cohort_mcf) in zip(dfs,lts)] datas = [(cohort, population_reverse_costs(cohort_df)) for cohort, cohort_df in dfs]
import numpy as np import pandas as pd import matplotlib.pyplot as plt from data_operations import transform_data, mcf, mcfdiff, mcfequal,\ plot_data, plot_datas, plot_mcf, plot_mcfs, plot_mcfdiff, plot_mcfdiffs if __name__ == '__main__': # CGD example raw_data = pd.read_csv('datas/CGD_recurrent.csv', sep=';') df = transform_data(raw_data) lt = mcf(df, robust=True, positive=True) print lt.head() covariate = 'Treatment' cohort1, cohort2 = 'Placebo', 'Gamma Interferon' group_df = df.set_index(covariate) group_lt = df.groupby(covariate).apply( lambda sfr: mcf(sfr, robust=True, positive=True)) df1, df2 = group_df.ix[cohort1], group_df.ix[cohort2] lt1, lt2 = group_lt.ix[cohort1], group_lt.ix[cohort2] lt_diff = mcfdiff(lt1, lt2) # Plot, no stratification fig, (ax1, ax2) = plt.subplots(2, 1) plot_data(df, ax=ax1) plot_mcf(lt, ax=ax2) plt.tight_layout()
import numpy as np import pandas as pd import matplotlib.pyplot as plt from data_operations import transform_data, mcf, mcfdiff, mcfequal,\ plot_data, plot_datas, plot_mcf, plot_mcfs, plot_mcfdiff, plot_mcfdiffs if __name__ == '__main__': # Valve Seats (Nelson 1995) Example data = pd.read_csv('datas/nelson1995_recurrent.csv', sep=';') df = transform_data(data, format='recurrent') lt = mcf(df, robust=True, positive=True) print lt[['Time', 'Y', 'N', 'dE[N]', 'E[N]', 'E[N].lcl', 'E[N].ucl']].head() # Plot, no stratification fig, (ax1, ax2) = plt.subplots(2, 1) plot_data(df, ax=ax1) plot_mcf(lt, ax=ax2) plt.tight_layout() plt.show()
import matplotlib.pyplot as plt from data_operations import transform_population, mcf, mcfdiff, mcfequal,\ plot_data, plot_datas, plot_mcf, plot_mcfs, plot_mcfdiff, plot_mcfdiffs, population_reverse if __name__ == '__main__': # Compressor Example raw_data = pd.read_csv('datas/compressor_population.csv', sep=';') #, usecols=[0,1,2]) #raw_data = raw_data.groupby('Time', as_index=False).sum() df = transform_population(raw_data) print population_reverse(df) group_df = raw_data.groupby('Building').apply( lambda sdata: population_reverse(transform_population(sdata))) dfs = list(group_df.groupby(level=0)) lt1 = mcf(df, robust=False, positive=False) lt2 = mcf(df, robust=False, positive=True) plot_datas(dfs, alpha=0.1) fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True) plot_mcf(lt1, ax=ax1, label='Normal limits') plot_mcf(lt2, ax=ax2, label='Normal limits (positive)') plt.ylim([-0.005, 0.1]) plt.show() #lt_cost = mcf_cost(fr, mcf_compound=lt) #print lt_cost.head() #plot_cost(mcf_cost)