24_deb_pkg_gov/R/readmeRDDAnalysis.R

75 lines
3.8 KiB
R
Raw Normal View History

2024-04-15 14:38:41 +00:00
# this is the file with the lmer multi-level rddAnalysis
2024-04-16 00:36:11 +00:00
library(tidyverse)
2024-04-19 19:30:41 +00:00
library(plyr)
2024-04-15 14:38:41 +00:00
# 0 loading the readme data in
try(setwd(dirname(rstudioapi::getActiveDocumentContext()$path)))
readme_df <- read_csv("../final_data/deb_readme_did.csv")
# 1 preprocessing
2024-04-19 21:04:06 +00:00
#colnames(readme_df) <- c("upstream_vcs_link", "event_date", "event_hash", "before_all_ct", "before_mrg_ct", "after_all_ct", "after_mrg_ct", "before_auth_new", "after_commit_new", "after_auth_new", "before_commit_new")
col_order <- c("upstream_vcs_link", "age_of_project", "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")
2024-04-15 14:38:41 +00:00
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,]))
}
2024-04-16 16:43:32 +00:00
#filter out the windows of time that we're looking at
2024-04-15 14:38:41 +00:00
window_num <- 8
2024-04-21 03:13:13 +00:00
windowed_data <- expanded_data |>
2024-04-15 14:38:41 +00:00
filter(week >= (26 - window_num) & week <= (26 + window_num)) |>
2024-04-16 16:43:32 +00:00
mutate(D = ifelse(week > 26, 1, 0))
2024-04-20 16:09:35 +00:00
#scale the age numbers
2024-04-21 03:13:13 +00:00
windowed_data$scaled_project_age <- scale(windowed_data$age_of_project)
2024-04-19 21:04:06 +00:00
#separate out the cleaning d
2024-04-21 03:13:13 +00:00
all_actions_data <- windowed_data[which(windowed_data$observation_type == "all"),]
mrg_actions_data <- windowed_data[which(windowed_data$observation_type == "mrg"),]
2024-04-20 02:07:06 +00:00
#find some EDA to identify which types of models might be the best for this
mean(all_actions_data$count)
median(all_actions_data$count)
table(all_actions_data$count)
var(all_actions_data$count)
qqnorm(all_actions_data$count)
y <- qunif(ppoints(length(all_actions_data$count)))
qqplot(all_actions_data$count, y)
2024-04-15 14:38:41 +00:00
# 3 rdd in lmer analysis
2024-04-16 16:43:32 +00:00
# rdd: https://rpubs.com/phle/r_tutorial_regression_discontinuity_design
# lmer: https://www.youtube.com/watch?v=LzAwEKrn2Mc
2024-04-15 14:38:41 +00:00
library(lme4)
2024-04-22 15:41:14 +00:00
# https://www.bristol.ac.uk/cmm/learning/videos/random-intercepts.html#exvar
2024-04-23 14:55:37 +00:00
# (D |upstream_vcs_link) or (week | upstream_vcs_link)
poisson_all_model <- glmer(count ~ D + I(week - 26) + D:I(week - 26) + scaled_project_age + (week || upstream_vcs_link), data=all_actions_data, family = poisson(link = "log"))
2024-04-20 02:07:06 +00:00
summary(poisson_all_model)
poisson_residuals <- residuals(poisson_all_model)
qqnorm(poisson_residuals)
2024-04-22 17:54:47 +00:00
# for visualization, may have to run model for each project and then identify top 5 projects for RDD graphs
#
#
poisson_mrg_model <- glmer(count ~ D + I(week - 26) + D:I(week - 26) + scaled_project_age + (week |upstream_vcs_link), data=mrg_actions_data, family = poisson(link = "log"))
summary(poisson_mrg_model)
poisson_mrg_residuals <- residuals(poisson_mrg_model)
qqnorm(poisson_mrg_residuals)
2024-04-20 02:07:06 +00:00
# Performance:
2024-04-19 19:30:41 +00:00
library(merTools)
2024-04-19 21:04:06 +00:00
ICC(outcome="count", group="week", data=all_actions_data)
2024-04-19 19:30:41 +00:00
#testing for different types of models
2024-04-20 02:07:06 +00:00