58 lines
2.2 KiB
R
58 lines
2.2 KiB
R
# Functions for creating samples of datasets
|
|
# Copyright (C) 2018 Nathan TeBlunthuis
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
sample.by.wiki <- function(dt,outcome,N.per.wiki=30){
|
|
set.seed(0)
|
|
sample.params <- dt[,.N,by=wiki.name]
|
|
sample.params[,group.sd := dt[,.(group.sd=sd(.SD[[outcome]])),by=wiki.name]$group.sd]
|
|
|
|
sample.params[,p.in.group := N / nrow(dt)]
|
|
|
|
sample.params[,min.N := min(N)]
|
|
sample.params[,n.from.group := pmin(min.N/(1-group.sd), N)]
|
|
|
|
sample.params[,p.sampled := n.from.group/N]
|
|
|
|
sample.params[,weight := 1/p.sampled]
|
|
dt[sample.params,":="(prob=p.sampled,weight=weight),on=.(wiki.name)]
|
|
sample.idx <- sample(nrow(dt),size=sum(sample.params$n.from.group,na.rm=TRUE),prob=dt$prob)
|
|
|
|
return(dt[sample.idx])
|
|
}
|
|
|
|
sample.newcomers <- function()
|
|
{
|
|
wikis.to.remove <- newcomers[,.N,by="wiki.name"][N<30]$wiki.name
|
|
remember(nrow(wikis.to.remove),"n.wikis.insufficient.newcomers")
|
|
newcomers.presample <- newcomers[!(wiki.name %in% wikis.to.remove)]
|
|
newcomers.sample <- sample.by.wiki(newcomers.presample,"survives")
|
|
return(newcomers.sample)
|
|
}
|
|
|
|
sample.ns4.edits <- function(){
|
|
wikis.to.keep <- ns4.reg.edits[,.(.N,N.reverts=sum(reverted)),by=wiki.name][(N>30)&(N.reverts > 30)]
|
|
ns4.reg.edits.sub <- ns4.reg.edits[wiki.name %in% wikis.to.keep$wiki.name]
|
|
ns4.reg.edits.sample <- sample.by.wiki(ns4.reg.edits.sub,"reverted")
|
|
return(ns4.reg.edits.sample)
|
|
}
|
|
|
|
sample.wiki.data <- function(){
|
|
## just choose 100 random wikis
|
|
wikis.to.keep <- sample(unique(wiki.data$wiki.name),100)
|
|
wiki.data.sample <- wiki.data[wiki.name %in% wikis.to.keep]
|
|
return(wiki.data.sample)
|
|
}
|