library(tidyverse)
library(plyr) 
library(stringr)
try(setwd(dirname(rstudioapi::getActiveDocumentContext()$path)))
#load in data
full_df <- read_csv("../final_data/deb_full_data.csv")
contrib_df <- read_csv("../final_data/deb_contrib_pop_change.csv")
readme_df <- read_csv("../final_data/deb_readme_pop_change.csv")
contrib_df <- merge(full_df, contrib_df, by="upstream_vcs_link")
readme_df <- merge(full_df, readme_df, by="upstream_vcs_link")
# age is calculated against December 11, 2023
#some expansion needs to happens for each project 
expand_timeseries <- function(project_row) {
  longer <- project_row |> 
    pivot_longer(cols = ends_with("new"),
                 names_to = "window",
                 values_to = "count") |>
    unnest(count) |>
    mutate(after_doc = as.numeric(str_detect(window, "after"))) |>
    mutate(is_collab = as.numeric(str_detect(window, "collab")))
  return(longer)
}
expanded_readme_data <- expand_timeseries(readme_df[1,])
for (i in 2:nrow(readme_df)){
  expanded_readme_data <- rbind(expanded_readme_data, expand_timeseries(readme_df[i,]))
}
expanded_contrib_data <- expand_timeseries(contrib_df[1,])
for (i in 2:nrow(contrib_df)){
  expanded_contrib_data <- rbind(expanded_contrib_data, expand_timeseries(contrib_df[i,]))
}
expanded_readme_data$log1pcount <- log1p(expanded_readme_data$count)
expanded_contrib_data$log1pcount <- log1p(expanded_contrib_data$count)
expanded_readme_data$logcount <- log(expanded_readme_data$count)
expanded_contrib_data$logcount <- log(expanded_contrib_data$count)
#scale age
expanded_readme_data$scaled_age <- scale(expanded_readme_data$age_in_days)
expanded_contrib_data$scaled_age <- scale(expanded_contrib_data$age_in_days)
#breaking out the types of population counts
collab_pop_readme <- expanded_readme_data[which(expanded_readme_data$is_collab == 1),]
contrib_pop_readme <- expanded_readme_data[which(expanded_readme_data$is_collab == 0),]
collab_pop_contrib <- expanded_contrib_data[which(expanded_contrib_data$is_collab == 1),]
contrib_pop_contrib <- expanded_contrib_data[which(expanded_contrib_data$is_collab == 0),]
#import models 
library(lme4)
library(optimx)
library(MASS)
#readme docs
simple_collab_readme_model <- glm.nb(log1pcount ~ as.factor(after_doc) + scale(age_in_days), data=collab_pop_readme)
summary(simple_collab_readme_model)
qqnorm(residuals(simple_collab_readme_model))
simple_contrib_readme_model <- glm.nb(log1pcount ~ as.factor(after_doc) + scale(age_in_days), data=collab_pop_readme)
summary(simple_contrib_readme_model)
qqnorm(residuals(simple_contrib_readme_model))
# I don't think MLM is the right one
collab_readme_model <- glmer.nb(log1pcount ~ as.factor(after_doc) + scaled_age + (after_doc| upstream_vcs_link), data=collab_pop_readme)
summary(collab_readme_model)
saveRDS(collab_readme_model, "final_models/0624_pop_rm_collab_better.rda")
contrib_readme_model <- glmer.nb(log1pcount ~ as.factor(after_doc) + scaled_age + (after_doc| upstream_vcs_link), data=contrib_pop_readme)
summary(contrib_readme_model)
saveRDS(contrib_readme_model, "final_models/0624_pop_rm_contrib.rda")
#contrib_readme_model <- readRDS("final_models/0623_pop_rm_contrib.rda")
#contributing models are not statistically significant``
library(texreg)

texreg(list(collab_readme_model, contrib_readme_model), stars=NULL, digits=2,
       custom.model.names=c( 'collab','contrib.'  ), 
       custom.coef.names=c('(Intercept)', 'after_introduction', 'etc'), 
       use.packages=FALSE, table=FALSE, ci.force = TRUE)

library(ggplot2)
contrib_pop_readme |>
  ggplot(aes(x = after_doc, y = log1pcount, col = as.factor(after_doc))) +
  geom_violin()

expanded_readme_data |>
  ggplot(aes(x = after_doc, y = count, col = as.factor(after_doc))) +
  geom_violin()

expanded_contrib_data |>
  ggplot(aes(x = after_doc, y = count, col = as.factor(after_doc))) +
  geom_violin()
