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

readme_count_data_filepath <- "/mmfs1/gscratch/comdata/users/mjilg/govdoc-cr-data/final_data/metadata/final_0207_README_weekly_count_data.csv"
readme_count_df = read.csv(readme_count_data_filepath, header = TRUE) 

readme_topic_dist_filepath <- "text_analysis/020725_README_file_topic_distributions.csv"
readme_topics_df = read.csv(readme_topic_dist_filepath, header = TRUE) 

readme_merged_manifest <- "text_analysis/0207_readme_merged_manifest.csv"
readme_manifest_df <- read.csv(readme_merged_manifest, header=TRUE)

merged_df <- inner_join(readme_manifest_df, readme_topics_df, by=c("new_filepath"= "filename"))

window_num <- 5
readme_count_df <- readme_count_df |>
  filter(relative_week >= (- window_num) & relative_week <= (window_num)) |>
  mutate(scaled_age = scale(age)) |>
  mutate(scaled_age_at_commit = scale(age_at_commit))|>
  mutate(log1p_count = log1p(commit_count))

summed_data <- readme_count_df |>
  filter(before_after == 1) |>
  group_by(project_id) |>
  summarise_at(vars(commit_count), list(summed_count=sum))


#loss of jaraco_keyring, though jaraco keyrings.alt is represented
merged_df <- inner_join(summed_data, merged_df, by=c("project_id" = "repo_id"))

merged_df$logged_commits <- log1p(merged_df$summed_count)

library(MASS)
commit_outcome_model <- glm.nb(logged_commits ~ 0 + t0 + t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8, data=merged_df)
qqnorm(residuals(commit_outcome_model))
summary(commit_outcome_model)

saveRDS(commit_outcome_model, "020725_README_commit_topic_model.rda")
