library(dplyr)
library(lubridate)
library(rdd)

contributing_df_filepath <- "/mmfs1/gscratch/comdata/users/mjilg/govdoc-cr-data/final_data/CONTRIBUTING_weekly_count_data.csv"
contributing_df = read.csv(contributing_df_filepath, header = TRUE) 

window_num <- 5
contributing_df <- contributing_df |>
  filter(week_index >= (- window_num) & week_index <= (window_num)) |>
  mutate(scaled_age = scale(age)) |>
  mutate(scaled_age_at_commit = scale(age_at_commit))|>
  mutate(log1p_count = log1p(commit_count)) |>
  mutate(C = ifelse(week_index > -4 & week_index <= 0, 1,0))

library(lme4)
library(optimx)
library(lattice)

all_gmodel <- glmer.nb(log1p_count ~ C * week_index + scaled_age + scaled_age_at_commit + (C * week_index | project_id),
                       control=glmerControl(optimizer="bobyqa",
                                            optCtrl=list(maxfun=2e5)), nAGQ=0, data=contributing_df)
summary(all_gmodel)
saveRDS(all_gmodel, "020425_contributing_crescendo_model.rda")
