
library(stringr)
library(tidyverse)
readme_topics_df <- read_csv("../text_analysis/readme_file_topic_distributions.csv")
colMeans(subset(readme_topics_df, select = -filename))
readme_df <- read_csv("../final_data/deb_readme_did.csv")
readme_pop_df <- read_csv("../final_data/deb_readme_pop_change.csv")

#get the readmeution count 
#some preprocessing and expansion 
col_order <- c("upstream_vcs_link", "age_in_days", "first_commit", "first_commit_dt", "event_gap", "event_date",  "event_hash", "before_all_ct", "after_all_ct", "before_mrg_ct", "after_mrg_ct", "before_auth_new", "after_auth_new", "before_commit_new",  "after_commit_new")
readme_df <- readme_df[,col_order]
readme_df$ct_before_all <- str_split(gsub("[][]","", readme_df$before_all_ct), ", ")
readme_df$ct_after_all <- str_split(gsub("[][]","", readme_df$after_all_ct), ", ")
readme_df$ct_before_mrg <- str_split(gsub("[][]","", readme_df$before_mrg_ct), ", ")
readme_df$ct_after_mrg <- str_split(gsub("[][]","", readme_df$after_mrg_ct), ", ")
drop <- c("before_all_ct", "before_mrg_ct", "after_all_ct", "after_mrg_ct")
readme_df = readme_df[,!(names(readme_df) %in% drop)]
# 2 some expansion needs to happens for each project 
expand_timeseries <- function(project_row) {
  longer <- project_row |> 
    pivot_longer(cols = starts_with("ct"),
                 names_to = "window",
                 values_to = "count") |>
    unnest(count) 
  longer$observation_type <- gsub("^.*_", "", longer$window)
  longer <- ddply(longer, "observation_type", transform, week=seq(from=0, by=1, length.out=length(observation_type)))
  longer$count <- as.numeric(longer$count)
  #longer <- longer[which(longer$observation_type == "all"),]
  return(longer)
}
expanded_data <- expand_timeseries(readme_df[1,])
for (i in 2:nrow(readme_df)){
  expanded_data <- rbind(expanded_data, expand_timeseries(readme_df[i,]))
}
#filter out the windows of time that we're looking at
window_num <- 8
windowed_data <- expanded_data |>
  filter(week >= (27 - window_num) & week <= (27 + window_num)) |>
  mutate(D = ifelse(week > 27, 1, 0))

summed_data <- windowed_data |>
  filter(D==1) |>
  group_by(upstream_vcs_link) |>
  summarise_at(vars(count), list(summed_count=sum))

#concat dataframes into central data
readme_pop_df <- readme_pop_df |>
  mutate(project_name = map_chr(upstream_vcs_link, ~ {
    parts <- str_split(.x, pattern = "/")[[1]]
    if (length(parts) >= 1) {
      parts[length(parts)]  
    } else {
      NA_character_
    }
  }))

readme_topics_df <- readme_topics_df |>
  mutate(project_name = map_chr(filename, ~ {
    parts <- str_split(.x, pattern = "_")[[1]]
    if (length(parts) >= 1) {
      paste(head(parts, -1), collapse="_")
    } else {
      NA_character_
    }
  }))

readme_total_df <- readme_pop_df |>
  join(readme_topics_df, by="project_name") 

readme_total_df <- readme_total_df|>
  join(summed_data, by="upstream_vcs_link")

#outcome variable that is number of commits by number of new readmeutors
readme_total_df$commit_by_contrib = readme_total_df$summed_count *readme_total_df$after_contrib_new
readme_total_df$logged_outcome = log1p(readme_total_df$commit_by_contrib)
readme_total_df$logged_contrib = log1p(readme_total_df$after_contrib_new)
readme_total_df$logged_commits = log1p(readme_total_df$summed_count)
#running regressions 
library(MASS)
lm1 <- glm.nb(logged_contrib~ t0+t1+t2+t7+t3 +t6 + t5, data = readme_total_df)
qqnorm(residuals(lm1))
summary(lm1)
#saveRDS(lm1, "0725_topic_contriboutcome_readme.rda")
contrib_ <- glm.nb(logged_contrib~ t0+t1+t2+t3+ t5 +t6 +t7, data = readme_total_df)
commit_ <- glm.nb(logged_commits~ t0+t1+t2+t3+ t5 +t6 +t7, data = readme_total_df)

library(texreg)

texreg(list(contrib_, commit_), stars=NULL, digits=3, use.packages=FALSE, 
       custom.model.names=c( 'Contributions','Commits'),
       custom.coef.names=c('(Intercept)', 'Topic 1', 'Topic 2', 'Topic 3', 'Topic 4', 'Topic 6', 'Topic 7', 'Topic 8'), 
       table=FALSE, ci.force = TRUE)
