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

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

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

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

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