24_deb_pkg_gov/R/readmeRDDAnalysis.R

145 lines
7.0 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 |>
filter(week >= (27 - window_num) & week <= (27 + window_num)) |>
mutate(D = ifelse(week > 27, 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)
windowed_data$week_offset <- windowed_data$week - 27
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
2024-04-24 17:59:07 +00:00
hist(log(all_actions_data$count))
2024-04-20 02:07:06 +00:00
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-24 17:59:07 +00:00
all_actions_data$logged_count <- log(all_actions_data$count)
all_actions_data$log1p_count <- log1p(all_actions_data$count)
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-24 21:55:40 +00:00
library(optimx)
2024-05-07 23:40:38 +00:00
library(lattice)
2024-04-24 21:55:40 +00:00
all_model <- lmer(log1p_count ~ D * I(week_offset)+ scaled_project_age + (D * I(week_offset)| upstream_vcs_link), data=all_actions_data, REML=FALSE, control = lmerControl(
optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
2024-05-08 23:58:43 +00:00
summary_of_all <- summary(all_model)
#identifying the quartiles of effect for D
2024-05-09 22:05:21 +00:00
mmcm = coef(all_model)$upstream_vcs_link
fixed_impacts = fixef(all_model)
summary(all_model)$coef[,2]
variance_components <- VarCorr(all_model)
2024-05-08 23:58:43 +00:00
all_model_ranef_condvar <- ranef(all_model, condVar = TRUE)
2024-05-09 22:05:21 +00:00
dotplot(all_model_ranef_condvar)
test <- broom.mixed::tidy(all_model, effects = "ran_vals", conf.int = TRUE)
2024-05-08 23:58:43 +00:00
attr(all_model_ranef_condvar$upstream_vcs_link, "postVar")
all_coefficients <- coef(all_model)
all_standard_errors <- sqrt(diag(vcov(all_model)))[1]
2024-05-09 22:05:21 +00:00
var(all_actions_data$log1p_count) # 1.125429
mean (all_actions_data$log1p_count) # 0.6426873
#all_gmodel <- glmer(count ~ D * I(week_offset)+ scaled_project_age + (D * I(week_offset)| upstream_vcs_link), data=all_actions_data, nAGQ=0, family = poisson)
#all_gmodel <- glmer.nb(count ~ D * I(week_offset)+ scaled_project_age + (D * I(week_offset) | upstream_vcs_link),
# control=glmerControl(optimizer="bobyqa",
# optCtrl=list(maxfun=2e5)), data=all_actions_data)
all_gmodel <- glmer.nb(count ~ D * I(week_offset)+ scaled_project_age + (D | upstream_vcs_link), data=all_actions_data)
summary(all_gmodel)
test_condvals <- broom.mixed::tidy(all_gmodel, effects = "ran_vals", conf.int = TRUE)
test_glmer_ranef_D <- test_condvals [which(test_condvals $term == "D"),]
has_zero <- function(estimate, low, high){
return(ifelse((low < 0),ifelse((high > 0), 1, 0), 2))
}
test_glmer_ranef_D <- test_glmer_ranef_D |>
mutate(ranef_grouping = has_zero(estimate, conf.low, conf.high)) |>
mutate(rank = rank(estimate))
test_glmer_ranef_D |>
ggplot(aes(x=rank, y=estimate, col = as.factor(ranef_grouping))) +
geom_linerange(aes(ymin= conf.low, ymax= conf.high)) +
theme_bw()
2024-05-08 14:33:03 +00:00
#below this groups the ranefs
2024-05-08 23:58:43 +00:00
"""
2024-05-08 14:33:03 +00:00
has_zero <- function(condval, condsd){
bounds <- condsd * 1.96
return(ifelse(((condval - bounds) < 0),ifelse(((condval + bounds) > 0), 1, 0), 2))
}
df_ranefs <- df_ranefs |>
mutate(ranef_grouping = has_zero(condval, condsd)) |>
mutate(rank = rank(condval))
2024-05-08 23:58:43 +00:00
D_df_ranef <- df_ranefs[which(df_ranefs$term == ),]
D_df_ranef <- D_df_ranef |>
mutate(rank = rank(condval))
2024-05-08 14:33:03 +00:00
hist(D_df_ranef$ranef_grouping)
#plot the ranefs
library(ggplot2)
D_df_ranef |>
ggplot(aes(x=rank, y=condval, col = as.factor(ranef_grouping))) +
geom_linerange(aes(ymin= condval - (1.96 * condsd), ymax= condval + (1.96 * condsd))) +
2024-05-08 23:58:43 +00:00
theme_bw()
"""
2024-05-07 23:40:38 +00:00
#d_effect_ranef_all <- all_model_ranef$upstream_vcs_link
#d_effect_ranef_all$quartile <- ntile(d_effect_ranef_all$condval, 4)
#model residuals
all_residuals <- residuals(all_model)
qqnorm(all_residuals)
# mrg behavior for this
mrg_model <- lmer(log1p_count ~ D * I(week_offset)+ scaled_project_age + (D * I(week_offset)| upstream_vcs_link), data=all_actions_data, REML=FALSE, control = lmerControl(
optimizer ='optimx', optCtrl=list(method='L-BFGS-B')))
summary(mrg_model)
#identifying the quartiles of effect for D
2024-05-07 23:40:38 +00:00
mrg_model_ranef <- ranef(mrg_model, condVar=TRUE)
2024-05-08 14:33:03 +00:00
df_mrg_ranefs <- as.data.frame(mrg_model_ranef)
2024-05-07 23:40:38 +00:00
dotplot(mrg_model_ranef)
d_effect_ranef_mrg <- mrg_model_ranef[mrg_model_ranef$term=="D",]
d_effect_ranef_mrg$quartile <- ntile(d_effect_ranef_mrg$condval, 4)
2024-05-08 14:33:03 +00:00
#doing similar random effect analysis for this
df_mrg_ranefs <- df_mrg_ranefs |>
mutate(ranef_grouping = has_zero(condval, condsd)) |>
mutate(rank = rank(condval))
D_df_mrg_ranefs <- df_mrg_ranefs[which(df_mrg_ranefs$term == "D"),]
D_df_mrg_ranefs |>
ggplot(aes(x=rank, y=condval, col = as.factor(ranef_grouping))) +
geom_linerange(aes(ymin= condval - (1.96 * condsd), ymax= condval + (1.96 * condsd)))
#merge model residuals
mrg_residuals <- residuals(mrg_model)
qqnorm(mrg_residuals)
2024-05-07 23:40:38 +00:00
# Performance: