# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .r
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: R 3.6
#     language: R
#     name: ir36
# ---

sessionInfo()

library(ggplot2)
library(gridExtra)
library(grid)
library(data.table)
library(sjPlot)
source("helper.R")
source("ob_util.R")
source("threshhold_analysis.R")

library(bayesplot)

cairo_pdf("model_1_slopes_rank.pdf",height=90,width=12)
mcmc_rank_hist(model.1.slopes)
dev.off()

cairo_pdf("model_1_kinks_rank.pdf",height=90,width=12)
mcmc_rank_hist(model.1.kinks)
dev.off()

cairo_pdf("model_1_morekinks_rank.pdf",height=90,width=12)
mcmc_rank_hist(model.1.more.kinks)
dev.off()

plot(model.1.slopes)

model.1.pre

## make synthetic data 
prob.damaging <- 0:1000/1000
syn.likelybad.t <- df[wiki.db=='enwiki']


# +
#plot_model(model.1,type='eff',terms=c('prob.damaging','pred.damaging.likelybad','pred.damaging.verylikelybad'))
# -

cutoffs <- load.cutoffs()

cutoff.vars <- c("damaging.maybebad.min.value",
                 "damaging.maybebad.max.value", 
                "damaging.likelybad.min.value",
                "damaging.likelybad.max.value",
                "damaging.verylikelybad.min.value",
                "damaging.verylikelybad.max.value")

syn.cutoffs <- list()
for(var in cutoff.vars){
    syn.cutoffs[[var]] <- first(cutoffs[!is.na(as.numeric(cutoffs[[var]])), var, with=F][[var]])
}

syndata <- data.table(prob.damaging = 0:1000/1000)


model.1.post$formula

syndata[,":="(pred.damaging.likelybad=as.factor(prob.damaging >= syn.cutoffs$damaging.likelybad.min.value),
              pred.damaging.verylikelybad=as.factor(prob.damaging >= syn.cutoffs$damaging.verylikelybad.min.value), 
              pred.damaging.maybebad=as.factor(prob.damaging >= syn.cutoffs$damaging.maybebad.min.value), 
              has.rcfilters.watchlist=FALSE,
              wiki.db='enwiki'
             )]

syndata2 <- copy(syndata)

syndata2[,has.rcfilters.watchlist:=TRUE]

syndata <- rbindlist(list(syndata,syndata2))

syndata <- syndata[,":="(has.rcfilters.watchlist = as.factor(has.rcfilters.watchlist),
                        post.cutoff = has.rcfilters.watchlist,
                        post.cutoff.fact = as.factor(has.rcfilters.watchlist))]

predicted <- posterior_predict(model.1.kinks,newdata=syndata)

predicted.interval <- predictive_interval(model.1.kinks,newdata=syndata)

head(predicted$se.fit)

syn.z

p <- ggplot(syndata) 
p <- p + geom_ribbon(aes(ymax=prob.reverted.upper,ymin=prob.reverted.lower,x=prob.damaging, group=pre.cutoff.fact,fill=pre.cutoff.fact),alpha=0.5)
p <- p + geom_line(aes(y=prob.reverted, x=prob.damaging,color=pre.cutoff.fact,group=pre.cutoff.fact))
p

model.1

cutoffs[wiki.db=='arwiki',c("damaging.likelybad.max","damaging.likelybad.min","damaging.likelybad.min.value","damaging.likelybad.max.value","deploy.dt","has.ores","has.rcfilters.watchlist",'has.rcfilters.watchlist'),with=F]

old.cutoffs <- fread(file.path(data.dir,'ores_rcfilters_cutoffs.csv'))

# ###### old.cutoffs[wiki_db=='arwiki']

dt.enwiki <- dt.all[ (date==parse.date.iso("2018-07-17 22:24:00")) & (wiki.db=='enwiki')]

dt.all[,.(.N),by=.(date,wiki.db)][N>1]



dt.enwiki

dt.all[rcfilters.watchlist.enabled == T, max(date),by=.(wiki.db)]

# look just at the cutoff that enabled RCFilters on watchlist
dt <- dt.all[date==



dt <- dt[, pre_cutoff_1 := factor(pre_cutoff_1, levels = c("Before design change", "After design change"))]

dt [ (event_user_isbot1 | event_user_isbot2) & revision_is_identity_reverted, revert_tool := 'bot']

ores_cutoffs <- cutoffs[,.(date=first(.SD[has_ores==T]$deploy_dt)),by=c("wiki_db")]

ores_sample <- dt[ores_cutoffs,.SD,on=c('date','wiki_db')]

duration_x_names <- c('1 second','5 seconds','1 minute','5 minutes','1 hour','24 hours','1 week','1 month')
duration_x_values <- c(1,5,60,60*5,60*60,60*60*24, 60*60*24*7, 60*60*24*30)

p <- ggplot(ores_sample, aes(x=time_to_revert,group=revert_tool,color=revert_tool, y=..count..)) + geom_density() + facet_grid(wiki_db~pre_cutoff_1, scales='free_y') + scale_x_log10(breaks=duration_x_values,labels=duration_x_names) + theme(axis.text.x = element_text(angle=90),legend.position='top')

ggsave('../ores_bias_plots/time_to_revert_by_tool_ores_cuttoff.pdf', p,width=14, height=300, units='cm',limitsize=F)

enwiki_ores_cutoff <- first(cutoffs[(wiki_db == 'enwiki') & (has_ores=T)]$deploy_dt)

enwiki_ores_sample <- dt[ (wiki_db == 'enwiki') & (date == enwiki_ores_cutoff)]

enwiki_ores_sample_reverts <- enwiki_ores_sample[revision_is_identity_reverted == T]

ggplot(enwiki_ores_sample_reverts, aes(x=time_to_revert,group=revert_tool,color=revert_tool, y=..count..)) + geom_density() + facet_grid(pre_cutoff_1~wiki_db) + scale_x_log10(breaks=duration_x_values,labels=duration_x_names) + theme(axis.text.x = element_text(angle=90), legend.position='top')

watchlist_cutoffs <- cutoffs[,.(date=first(.SD[has_rcfilters_watchlist==T]$deploy_dt)),by=c("wiki_db")]

watchlist_sample <- dt[watchlist_cutoffs,.SD,on=c('date','wiki_db')]

watchlist_cutoffs[wiki_db =='fawiki']

dt[ (wiki_db=='fawiki') & (event_timestamp > watchlist_cutoffs[wiki_db=='fawiki']$date)]

p <- ggplot(watchlist_sample, aes(x=time_to_revert,group=revert_tool,color=revert_tool,y=..count..)) + geom_density() + facet_grid(wiki_db~pre_cutoff_1, scales='free_y') + scale_x_log10(breaks=duration_x_values,labels=duration_x_names) + theme(axis.text.x = element_text(angle=90), legend.position='top')

ggsave("../ores_bias_plots/time_to_revert_by_tool_watchlist_cuttoff.pdf",p,width=14,height=300,units='cm',limitsize=F)

enwiki_watchlist_cutoff <- first(cutoffs[(wiki_db == 'enwiki') & (has_rcfilters_watchlist == T)]$deploy_dt)

enwiki_watchlist_sample <- dt[ (wiki_db == 'enwiki') & (date == enwiki_watchlist_cutoff)]

ggplot(enwiki_watchlist_sample, aes(x=time_to_revert,group=revert_tool,color=revert_tool,y=..count..)) + geom_density() + facet_grid(pre_cutoff_1~wiki_db)  + scale_x_log10(breaks=duration_x_values,labels=duration_x_names) + theme(axis.text.x = element_text(angle=90))

sample <- watchlist_sample
wiki <- 'enwiki'
x <- 'time_to_revert'
group <- 'revert_tool'
y <- "..count.."
breaks <- duration_x_values
labels <- duration_x_names
facet_row <- 'pre_cutoff_1'
facet_col <- 'revert_tool'
bins <- 100

wikis <- unique(sample$wiki_db)
tools <- unique(sample$revert_tool)[2:6]

tool <- 'undo'
wiki <- 'cswiki'
ttr_grid_plot(sample,wiki,tool,100)

# +
# aliasing might be misleading so we can do it again using kernels: ..density..
# -

hist_plots <- list()
density_plots <- list()
combined.plots <- list()
i <- 1
for(wiki in wikis){
    for(tool in tools){
        hist_plots[[i]] <- ttr_histogram_plot(sample,wiki,tool)
        density_plots[[i]] <- ttr_density_plot(sample,wiki,tool)
        combined.plots[[i]] <- ttr_grid_plot(sample,wiki,tool)
        i <- i + 1
    }
}

hist_p <- marrangeGrob(grobs=hist_plots,nrow=1,ncol=length(tools))

dens_p <- marrangeGrob(grobs=density_plots,nrow=1,ncol=length(tools))

combin_p <- marrangeGrob(grobs=combined.plots, nrow=1, ncol=length(tools))

ggsave('../ores_bias_plots/time_to_revert_hist_grid.pdf',width=12,height=8,plot=hist_p)
ggsave('../ores_bias_plots/time_to_revert_density_grid.pdf',width=12,height=8,plot=dens_p)

ggsave('../ores_bias_plots/time_to_revert_grid.pdf',width=12,height=8,plot=combin_p)

# +
wikis <- unique(dt$wiki_db)
tools <- unique(dt$revert_tool)
n.wikis <- length(wikis)
n.tools <- length(tools)

watchlist.utest.grid <- data.table(expand.grid(wiki=wikis,tool=tools),median.pre=NaN, median.post = NaN,U.p.value =NaN,mean.pre=NaN, mean.post=NaN, T.p.value=NaN)


# -

i <- 1
for(iwiki in wikis){
    for(itool in tools){
        data.pre <- watchlist_sample[(wiki_db==iwiki) & (revert_tool==itool) & (pre_cutoff==TRUE)]$time_to_revert
        data.post <- watchlist_sample[(wiki_db==iwiki) & (revert_tool==itool) & (pre_cutoff==FALSE)]$time_to_revert

        # the hypothesis is that time to reverts will decrease for undo, but may increase or decrease for rollback
        alt='two.sided'

        if(tool == 'undo'){
            alt = 'greater'
        }
        if(tool == 'rollback'){
            alt= 'less'
        }
        if( (length(data.pre) > 1) & (length(data.post) > 1)){
            UT <- wilcox.test(data.pre, data.post, correct=T, alternative=alt)
            TT <- t.test(data.pre, data.post, alternative=alt)
            watchlist.utest.grid[(wiki==iwiki) & (tool==itool),
                                 ":="(
                                     median.pre=median(data.pre),
                                     median.post=median(data.post),
                                     U.p.value = UT$p.value,
                                     mean.pre = mean(data.pre),
                                     mean.post = mean(data.post),
                                     T.p.value = TT$p.value
                                 )
                                ]
        } 
    }
}

watchlist.utest.grid[tool=='rollback']

watchlist.utest.grid[tool=='undo']

watchlist.utest.grid[wiki == 'enwiki']



watchlist.deltas <- dt[has_rcfilters_watchlist.y == T,.(p_reverted = mean(revision_is_identity_reverted),
                                                        p_undo = mean(revert_tool == 'undo'),
                                                        p_rollback = mean(revert_tool == 'rollback'),
                                                        p_bot = mean(revert_tool == 'bot'), 
                                                        p_huggle = mean(revert_tool == 'huggle'), 
                                                        p_other = mean(revert_tool == 'other'),
                                                        n_edits_in_subperiod = .N
                                                       ), by=.(wiki_db, pre_cutoff, date)]









StarcoderdataJupyterScriptsDedupFiltered