1
0
govdoc-cr-analysis/mlm/contributing_did_model_fit.R

50 lines
1.8 KiB
R
Raw Permalink Normal View History

2025-02-02 20:16:42 +00:00
library(dplyr)
library(lubridate)
library(rdd)
contributing_df_filepath <- "/mmfs1/gscratch/comdata/users/mjilg/govdoc-cr-data/final_data/CONTRIBUTING_weekly_count_data.csv"
contributing_df = read.csv(contributing_df_filepath, header = TRUE)
window_num <- 5
contributing_df <- contributing_df |>
filter(week_index >= (- window_num) & week_index <= (window_num)) |>
mutate(scaled_age = scale(age)) |>
mutate(scaled_age_at_commit = scale(age_at_commit))|>
mutate(log1p_count = log1p(commit_count))
library(lme4)
library(optimx)
library(lattice)
2025-02-02 21:40:49 +00:00
#all_gmodel <- glmer.nb(log1p_count ~ before_after * week_index + scaled_age + (before_after * week_index | project_id),
# control=glmerControl(optimizer="bobyqa",
# optCtrl=list(maxfun=2e5)), nAGQ=0,
# data=contributing_df)
2025-02-03 04:06:35 +00:00
all_gmodel <- readRDS("mlm/models/020125_contributing_model.rda")
2025-02-02 20:16:42 +00:00
summary(all_gmodel)
2025-02-02 21:40:49 +00:00
#saveRDS(all_gmodel, "020125_contributing_model.rda")
model_residuals <- residuals(all_gmodel)
acf(model_residuals)
vif(all_gmodel)
#getting between-group variance
variance_components <- as.data.frame(VarCorr(all_gmodel))
2025-02-03 04:06:35 +00:00
library(broom.mixed)
library(ggplot2)
condvals <- broom.mixed::tidy(all_gmodel, effects = "ran_vals", conf.int = TRUE)
glmer_ranef_Dweek <- condvals [which(condvals $term == "before_after:week_index"),]
has_zero <- function(estimate, low, high){
return(ifelse((low < 0),ifelse((high > 0), 1, 0), 2))
}
glmer_ranef_Dweek <- glmer_ranef_Dweek |>
mutate(ranef_grouping = has_zero(estimate, conf.low, conf.high)) |>
mutate(rank = rank(estimate))
g <- glmer_ranef_Dweek |>
ggplot(aes(x=rank, y=estimate, col = as.factor(ranef_grouping))) +
geom_linerange(aes(ymin= conf.low, ymax= conf.high)) +
theme_bw()
g
write.csv(glmer_ranef_Dweek, "0201_contributing_dweek_ranefs.csv")