-
Notifications
You must be signed in to change notification settings - Fork 6
/
Love-boost.R
70 lines (57 loc) · 2.23 KB
/
Love-boost.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
58
59
60
61
62
63
64
65
66
67
68
69
70
## Updated 2020-09-20 simplifying to four functions
`bootdif` <-
function(y, g, conf.level=0.95, B.reps = 2000) {
lowq = (1 - conf.level)/2
g <- as.factor(g)
a <- attr(Hmisc::smean.cl.boot(y[g==levels(g)[1]], B=B.reps, reps=TRUE),'reps')
b <- attr(Hmisc::smean.cl.boot(y[g==levels(g)[2]], B=B.reps, reps=TRUE),'reps')
meandif <- diff(tapply(y, g, mean, na.rm=TRUE))
a.b <- quantile(b-a, c(lowq,1-lowq))
res <- c(meandif, a.b)
names(res) <- c('Mean Difference',lowq, 1-lowq)
res
}
`saifs.ci` <-
function(x, n, conf.level=0.95, dig=3)
{
p.sample <- round(x/n, digits=dig)
p1 <- x / (n+1)
p2 <- (x+1) / (n+1)
var1 <- (p1*(1-p1))/n
se1 <- sqrt(var1)
var2 <- (p2*(1-p2))/n
se2 <- sqrt(var2)
lowq = (1 - conf.level)/2
tcut <- qt(lowq, df=n-1, lower.tail=FALSE)
lower.bound <- round(p1 - tcut*se1, digits=dig)
upper.bound <- round(p2 + tcut*se2, digits=dig)
res <- c(p.sample, lower.bound, upper.bound)
names(res) <- c('Sample Proportion',lowq, 1-lowq)
res
}
`twobytwo` <-
function(a,b,c,d, namer1 = "Row1", namer2 = "Row2", namec1 = "Col1", namec2 = "Col2",
conf.level = 0.95)
# build 2 by 2 table and run Epi library's twoby2 command to summarize
# from the row-by-row counts in a cross-tab
# upper left cell is a, upper right is b, lower left is c, lower right is d
# names are then given in order down the rows then across the columns
# use standard epidemiological format - outcomes in columns, treatments in rows
{
.Table <- matrix(c(a, b, c, d), 2, 2, byrow=T, dimnames=list(c(namer1, namer2), c(namec1, namec2)))
Epi::twoby2(.Table, alpha = 1 - conf.level)
}
# Code from Gelman and Carlin
`retrodesign` <- function(A, s, alpha=.05, df=Inf,
n.sims=10000){
z <- qt(1-alpha/2, df)
p.hi <- 1 - pt(z-A/s, df)
p.lo <- pt(-z-A/s, df)
power <- p.hi + p.lo
typeS <- p.lo/power
estimate <- A + s*rt(n.sims,df)
significant <- abs(estimate) > s*z
exaggeration <- mean(abs(estimate)[significant])/A
return(list(power=power, typeS=typeS,
exaggeration=exaggeration))
}