I'd like to perform a survival analysis on how fast companies achieve a certain Event after their foundation. I have a dataframe with several static and non static covariates over a 5 years observation period. The companies are getting founded within the first 2 Years of observation. I tried to create the input data for lifelines CoxTimeVaryingFitter using to_long_format and add_covariate_to_timeline. Here is some example df:
Year id time_to_event event CV_stat1 CV_stat2 CV_dyn1 CV_dyn2 foundingyear
2010 1 2 0 1 0 3 4 2011
2011 1 2 1 1 0 5 7 2011
2012 1 2 0 1 0 3 8 2011
2013 1 2 0 1 0 6 9 2011
2014 1 2 0 1 0 8 10 2011
Here is my code:
df['Year'] = df.Year - df.foundingyear
df = df[df.Year >= 0]
df['time_to_event'] = df.time_to_event - df.foundingyear +1
base_df = df[['id','time_to_event', 'event','CV_stat1', 'CV_stat2']]
base_df = to_long_format(base_df, duration_col="time_to_event")
cv = df[['id', 'Year', 'CV_dyn1']]
surv_df = add_covariate_to_timeline(base_df, cv, duration_col="Year", id_col="id", event_col="event")
Unfortunately that results in several NaN rows for most of the companies, which does not make sense
start CV_stat1 CV_stat2 CV_dyn1 stop id event
0 1 0 NaN 0 27 FALSE
0 1 0 NaN 0 27 FALSE
0 1 0 NaN 0 27 FALSE
0 1 0 NaN 1 27 FALSE
1 1 0 33,16667 2 27 FALSE
The event should be True for period 1 for that id. What am I doing wrong so that the NaN rows are included?