-
Notifications
You must be signed in to change notification settings - Fork 6
/
bruteSens.R
61 lines (51 loc) · 2.07 KB
/
bruteSens.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#' Given a vector of existing probabilities, produce odds, and then
#' adjust odds, and convert back to probabilities of assignment.
#' @param p is a scalar probability 0<=p<=1 (not sure the point of allowing 0 or 1)
#' @param z is a vector of 0 and 1 or FALSE and TRUE indicating treatment assignment
#' @param s is a strata factor like a matched set or blocking factor
#' @param G multiplies the odds of treatment assignment
#' @return an nx1 vector (where n is length(z) where each unit has a new probability of treatment.
probTC<-function(p,z,s,G){
odds<-p/(1-p) ## calculate the odds of treatment
newodds<-G*odds ## new odds to reflect the WhatIf thought experiment
if(identical(newodds,odds) & G==1){
return(p) ## control and treated all get p
} else {
newpt<-newodds/(1+newodds) ## convert odds back to probabilities
## Make sure that prob of treatment+prob control adds to 1 within strata
## so prob of treatment for controls must go down if prob of treatment for
## treated goes up
ptpc<-unsplit(lapply(split(data.frame(z=z,probt=newpt),s),
function(dat){
##stopifnot(length(unique(dat$probt))==1)
with(dat,
z*probt + (1-z)*(1-unique(probt))
)
}),
s)
return(ptpc)
}
}
#' Sample without replacement from a vector with fixed probabilities of
#' treatment
#' @param prob is the probability of treatment assignment. It should be n x 1.
#' @return A vector the same length as the vector prob with a draw from the
#' set of possible samples consistent with the vector of probabilities.
biasedsample<-function(prob){
## prob is the translation from odds to probabilities of treatment
require(sampling)
newz<-UPrandomsystematic(prob)
newz
}
#' Execute biased sampling across sets/blocks/strata
#' @param set a vector (ideally a factor) indicating set membership
#' @param prob a vector of probabilities of treatment assignment the same
#' length as set
#' @return a vector indicating new treatment status
biasedsampleStrat<-function(set,prob){
unsplit(
lapply(split(prob,set),function(ps){
biasedsample(ps)
}),
set)
}