R Functions

advertisement
D.G. Bonett (12/2015)
R Functions
Part I: Sample Size for Desired Precision
Function 1: Sample size to estimate one mean
sizeCImean1 <- function(alpha, var, w) {
# Computes sample size required to estimate a mean with desired precision
# in a 1-group design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
var:
planning value of DV variance
#
w:
desired confidence interval width
# Returns:
#
required sample size
z <- qnorm(1 - alpha/2)
n <- ceiling(4*var*(z/w)^2 + z^2/2)
return(n)
}
Example:
sizeCImean1(.05, 264.4, 10)
[1] 43
Function 2: Sample size to estimate a mean difference (2-group design)
sizeCImean2 <- function(alpha, var, w) {
# Computes sample size required to estimate a mean difference
# with desired precision in a 2-group design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
var:
planning value of average within-group DV variance
#
w:
desired confidence interval width
# Returns:
#
required sample size per group
z <- qnorm(1 - alpha/2)
n <- ceiling(8*var*(z/w)^2 + z^2/4)
return(n)
}
Example:
sizeCImean2(.05, 37.1, 5)
[1] 47
D.G. Bonett (12/2015)
Function 3: Sample size to estimate standardized mean difference (2-group design)
sizeCIstdmean2 <- function(alpha, d, w) {
# Computes sample size required to estimate a standardized mean difference
# with desired precision in a between-subjects design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
d:
planning value of standardized mean difference
#
w:
desired confidence interval width
# Returns:
#
required sample size per group
z <- qnorm(1 - alpha/2)
n <- ceiling((d^2 + 8)*(z/w)^2)
return(n)
}
Example:
sizeCIstdmean2(.05, .75, .5)
[1] 132
Function 4: Sample size to estimate mean ratio (2-group design)
sizeCImeanRatioBS <- function(alpha, var, m1, m2, r) {
# Computes sample size required to estimate a mean ratio
# with desired precision in a 2-group design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
var:
planning value of average within-group DV variance
#
m1:
planning value of mean for group 1
#
m2:
planning value of mean for group 2
#
r:
desired upper to lower confidence interval endpoint ratio
# Returns:
#
required sample size per group
z <- qnorm(1 - alpha/2)
n <- ceiling(8*var*(1/m1^2 + 1/m2^2)*(z/log(r))^2 + z^2/4)
return(n)
}
> sizeCImeanRatioBS(.05, .4, 3.5, 3.1, 1.2)
[1] 70
D.G. Bonett (12/2015)
Function 5: Sample size to estimate linear contrast of means (between-subjects design)
sizeCImeanBS <- function(alpha, var, w, c) {
# Computes sample size required to estimate a linear contrast of means
# with desired precision in a between-subjects design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
var:
planning value of average within-group DV variance
#
w:
desired confidence interval width
#
c:
vector of contrast coefficients
# Returns:
#
required sample size per group
z <- qnorm(1 - alpha/2)
m <- length(c) - sum(c == 0)
n <- ceiling(4*var*(t(c)%*%c)*(z/w)^2 + z^2/(2*m))
return(n)
}
Example:
c = c(.5, .5, -1)
sizeCImeanBS(.05, 5.62, 2.0, c)
[,1]
[1,]
34
Function 6: Sample size to estimate mean difference (within-subjects design)
sizeCImean2WS <- function(alpha, var, cor, w) {
# Computes sample size required to estimate a mean difference
# with desired precision in a within-subjects design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
var:
planning value of average within-group DV variance
#
cor:
planning value of correlation
#
w:
desired confidence interval width
# Returns:
#
required sample size
z <- qnorm(1 - alpha/2)
n <- ceiling(8*(1 - cor)*var*(z/w)^2 + z^2/2)
return(n)
}
Example:
sizeCImean2WS(.05, 265, .8, 10)
[1] 19
D.G. Bonett (12/2015)
Function 7: Sample size to estimate standardized mean difference (within-subjects design)
sizeCIstdmean2WS <- function(alpha, d, cor, w) {
# Computes sample size required to estimate a standardized mean difference
# with desired precision in a within-subjects design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
d:
planning value of standardized mean difference
#
cor:
planning value of correlation
#
w:
desired confidence interval width
# Returns:
#
required sample size
z <- qnorm(1 - alpha/2)
n <- ceiling(4*(d^2*(1+cor^2)/4 + 2*(1 - cor))*(z/w)^2)
return(n)
}
Example:
sizeCIstdmean2WS(.05, 1, .65, .6)
[,1]
[1,]
46
Function 8: Sample size to estimate a mean ratio (within--subjects design)
sizeCImeanRatioWS <- function(alpha, var, m1, m2, cor, r) {
# Computes sample size required to estimate a mean ratio
# with desired precision in a within-subjects design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
var:
planning value of average within-group DV variance
#
m1:
planning value of mean for measurement 1
#
m2:
planning value of mean for measurement 2
#
cor:
planning value for measurement correlation
#
r:
desired upper to lower confidence interval endpoint ratio
# Returns:
#
required sample size
z <- qnorm(1 - alpha/2)
n <- ceiling(8*var*(1/m1^2 + 1/m2^2 - 2*cor/(m1*m2))*(z/log(r))^2 + z^2/2)
return(n)
}
Example:
> sizeCImeanRatioWS(.05, 400, 150, 100, .7, 1.2)
[1] 21
D.G. Bonett (12/2015)
Function 9: Sample size to estimate linear contrast of means (within-subjects design)
sizeCImeanWS <- function(alpha, var, cor, w, h) {
# Computes sample size required to estimate a linear contrast of means
# with desired precision in a within-subjects design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
var:
planning value of largest DV variance
#
cor: planning value of smallest correlation
#
w:
desired confidence interval width
#
h:
vector of contrast coefficients
# Returns:
#
required sample size
z <- qnorm(1 - alpha/2)
k <- length(h)
n <- ceiling(4*(1 - cor)*var*(t(h)%*%h)*(z/w)^2 + z^2/2)
return(n)
}
Example:
h = c(.5, .5, -.5, -.5)
sizeCImeanWS(.05, 265, .8, 10, h)
[,1]
[1,]
11
Function 10: Sample size to estimate standardized linear contrast of means (between-subjects
design)
sizeCIstdmeanBS <- function(alpha, d, w, c) {
# Computes sample size required to estimate a standardized linear contrast of means
# with desired precision in a between-subjects design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
d:
planning value of standardized linear contrast
#
w:
desired confidence interval width
#
c:
vector of contrast coefficients
# Returns:
#
required sample size per group
z <- qnorm(1 - alpha/2)
a <- length(c)
n <- ceiling((2*d^2/a) + 4*(t(c)%*%c))*(z/w)^2)
return(n)
}
Example:
c = c(.5, .5, -.5, -.5)
sizeCIstdmeanBS(.05, 1, .6, c)
[,1]
[1,]
49
D.G. Bonett (12/2015)
Function 11: Sample size to estimate standardized linear contrast of means (within-subjects design)
sizeCIstdmeanWS <- function(alpha, d, cor, w, h) {
# Computes sample size required to estimate a standardized linear contrast of means
# with desired precision in a within-subjects design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
d:
planning value of standardized linear contrast
#
cor:
planning value of smallest correlation
#
w:
desired confidence interval width
#
h:
vector of contrast coefficients
# Returns:
#
required sample size
z <- qnorm(1 - alpha/2)
a <- length(h)
n <- ceiling(4*(d^2*(1 + (a - 1)*cor^2)/(2*a) + (1 - cor)*(t(h)%*%h))*(z/w)^2)
return(n)
}
Example:
h = c(.5, .5, -.5, -.5)
sizeCIstdmeanWS(.05, 1, .7, .6, h)
[,1]
[1,]
26
Function 12 Second-stage sample size requirement
sizeCIsecond <- function(n0, w0, w) {
# Computes second-stage sample size required to obtain desired precision
# Arguments:
#
n0:
first-stage sample size
#
w0:
CI width in first-stage sample
#
w:
desired confidence interval width
# Returns:
#
required second-stage sample size
n <- ceiling(((w0/w)^2 - 1)*n0)
return(n)
}
Example:
sizeCIsecond(20, 5.3, 2.5)
[1] 70
D.G. Bonett (12/2015)
Part II – Sample Size for Desired Power
Function 13: Sample size to test a mean
sizePOWmean1 <- function(alpha, var, pow, es) {
# Computes sample size required to test a mean with desired power
# in a 1-group design
# Arguments:
#
alpha: alpha level for test
#
var:
planning value of DV variance
#
pow:
desired power
#
es:
planning value of mean minus null hypothesis value
# Returns:
#
required sample size
za <- qnorm(1 - alpha/2)
zb <- qnorm(pow)
n <- ceiling(var*(za + zb)^2/es^2 + za^2/2)
return(n)
}
Example:
sizePOWmean1(.05, 80.5, .9, 7)
[1] 20
Function 14: Sample size to test a mean difference (2-group design)
sizePOWmean2 <- function(alpha, var, pow, es) {
# Computes sample size required to test a mean difference with desired power
# in a 2-group design
# Arguments:
#
alpha: alpha level for test
#
var:
planning value of average within-group DV variance
#
pow:
desired power
#
es:
planning value of mean difference
# Returns:
#
required sample size per group
za <- qnorm(1 - alpha/2)
zb <- qnorm(pow)
n <- ceiling(2*var*(za + zb)^2/es^2 + za^2/4)
return(n)
}
Example:
sizePOWmean2(.05, 100, .95, 10)
[1] 27
D.G. Bonett (12/2015)
Function 15: Sample size to test a linear contrast of means (between-subjects design)
sizePOWmeanBS <- function(alpha, var, pow, es, c) {
# Computes sample size required to test a linear contrast of means with desired power
# in a between-subjects design
# Arguments:
#
alpha: alpha level for test
#
var:
planning value of average within-group DV variance
#
pow:
desired power
#
es:
planning value of linear contrast of means
#
c:
vector of contrast coefficients
# Returns:
#
required sample size per group
za <- qnorm(1 - alpha/2)
zb <- qnorm(pow)
m <- length(c) - sum(c == 0)
n <- ceiling(var*(t(c)%*%c)*(za + zb)^2/es^2 + za^2/(2*m))
return(n)
}
Example:
c = c(1, -1, -1, 1)
sizePOWmeanBS(.05, 27.5, .90, 5, c)
[,1]
[1,]
47
Function 16: Sample size to test a mean difference (within-subjects design)
sizePOWmean2WS <- function(alpha, var, pow, es, cor) {
# Computes sample size required to test a mean difference with desired power
# in a within-subjects design
# Arguments:
#
alpha: alpha level for test
#
var:
planning value of average within-group DV variance
#
pow:
desired power
#
es:
planning value of mean difference
#
cor:
planning value of correlation
# Returns:
#
required sample size
za <- qnorm(1 - alpha/2)
zb <- qnorm(pow)
n <- ceiling(2*var*(1 - cor)*(za + zb)^2/es^2 + za^2/2)
return(n)
}
Example:
sizePOWmean2WS(.05, 1.25, .80, .5, .75)
[1] 22
D.G. Bonett (12/2015)
Function 17: Sample size to test a linear contrast of means (within-subjects design)
sizePOWmeanWS <- function(alpha, var, pow, es, cor, h) {
# Computes sample size required to test a linear contrast of means with desired power
# in a within-subjects design
# Arguments:
#
alpha: alpha level for test
#
var:
planning value of largest DV variance
#
pow:
desired power
#
es:
planning value of linear contrast of means
#
cor:
planning value of smallest correlation
#
h:
vector of contrast coefficients
# Returns:
#
required sample size
za <- qnorm(1 - alpha/2)
zb <- qnorm(pow)
n <- ceiling(var*(1 - cor)*(t(h)%*%h)*(za + zb)^2/es^2 + za^2/2)
return(n)
}
Example:
h = c(.5, .5, -.5, -.5)
sizePOWmeanWS(.05, 50.7, .90, 2, .8, h)
[,1]
[1,]
29
Function 18: Sample size for Sign test (1-group design)
sizePOWsign1 <- function(alpha, p, pow) {
# Computes sample size required for Sign test with desired power
# in a 1-sample design
# Arguments:
#
alpha: alpha level for test
#
p:
planning value of proportion of cases with scores greater
#
than hypothesized value of median
#
pow:
desired power
# Returns:
#
required sample size
za <- qnorm(1 - alpha/2)
zb <- qnorm(pow)
es <- p - .5;
n <- ceiling(p*(1 - p)*(za + zb)^2/es^2)
return(n)
}
Example:
sizePOWsign1(.05, .3, .9)
[1] 56
D.G. Bonett (12/2015)
Function 19: Sample size for Mann-Whitney test
sizePOWmann <- function(alpha, p, pow) {
# Computes sample size required for Mann-Whitney test with desired power
# Arguments:
#
alpha: alpha level for test
#
p:
planning value of proportion of cases with scores that would be
#
higher under treatment 1 than treatment 2
#
pow:
desired power
# Returns:
#
required sample size per group
za <- qnorm(1 - alpha/2)
zb <- qnorm(pow)
es <- p - .5;
n <- ceiling((za + zb)^2/(6*es^2))
return(n)
}
Example:
sizePOWmann(.05, .3, .9)
[1] 44
Function 20: Sample size for Sign test (within-subjects design)
sizePOWsignWS <- function(alpha, p, pow) {
# Computes sample size required for Sign test with desired power
# in a within-subjects design
# Arguments:
#
alpha: alpha level for test
#
p:
planning value of proportion of cases with scores that would be
#
higher under treatment 1 than treatment 2
#
pow:
desired power
# Returns:
#
required sample size
za <- qnorm(1 - alpha/2)
zb <- qnorm(pow)
es <- p - .5;
n <- ceiling(p*(1 - p)*(za + zb)^2/es^2)
return(n)
}
Example:
sizePOWsignWS(.05, .75, .9)
[1] 32
D.G. Bonett (12/2015)
Part III – Confidence Intervals
Function 21: Confidence interval for a standardized mean difference (2-group design)
CIstdmean2 <- function(alpha, m1, m2, sd1, sd2, n1, n2) {
# Computes confidence interval for a standardized mean difference
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
mj:
sample mean in group j
#
sdj:
sample standard deviation in group j
#
nj:
sample size in group j
# Returns:
#
estimate, SE, lower limit, and upper limit for equal variance and
#
unequal variance methods plus single group standardizer
z <- qnorm(1 - alpha/2)
v1 <- sd1^2
v2 <- sd2^2
s <- sqrt((v1 + v2)/2)
sp <- sqrt(((n1 - 1)*v1 + (n2 - 1)*v2)/(n1 + n2 - 2))
est1 <- (m1 - m2)/s
se1 <- sqrt(est1^2*(v1^2/(n1-1) + v2^2/(n2-1))/(8*s^4) + (v1/(n1-1) + v2/(n2-1))/s^2)
ll1 <- est1 - z*se1
ul1 <- est1 + z*se1
est2 <- (m1 - m2)/sp
se2 <- sqrt(est2^2*(1/(n1 - 1) + 1/(n2 - 1))/8 + 1/n1 + 1/n2)
ll2 <- est2 - z*se2
ul2 <- est2 + z*se2
est3 <- (m1 - m2)/sd1
se3 <- sqrt(est3^2/(2*(n1 - 1)) + 1/(n1 - 1) + v2/((n2 - 1)*v1))
ll3 <- est3 - z*se3
ul3 <- est3 + z*se3
est4 <- (m1 - m2)/sd2
se4 <- sqrt(est4^2/(2*(n2 - 1)) + 1/(n2 - 1) + v1/((n1 - 1)*v2))
ll4 <- est4 - z*se4
ul4 <- est4 + z*se4
out1 <- t(c(est1, se1, ll1, ul1))
out2 <- t(c(est2, se2, ll2, ul2))
out3 <- t(c(est3, se3, ll3, ul3))
out4 <- t(c(est4, se4, ll4, ul4))
out <- rbind(out1, out2, out3, out4)
colnames(out) <- c("Estimate", "SE", "LL", "UL")
rownames1 <- c("Equal Variances Not Assumed", "Equal Variances Assumed:")
rownames2 <- c("Group 1 Standardizer:", "Group 2 Standardizer:")
rownames(out) <- c(rownames1, rownames2)
return(out)
}
Example:
CIstdmean2(.05, 35.1, 26.7, 7.32, 6.98, 30, 30)
Equal
Equal
Group
Group
Variances Not Assumed
Variances Assumed:
1 Standardizer:
2 Standardizer:
Estimate
1.174493
1.174493
1.147541
1.203438
SE
0.2844012
0.2802826
0.3084007
0.3013414
LL
0.6170771
0.6251494
0.5430867
0.6128200
UL
1.731909
1.723837
1.751995
1.794057
D.G. Bonett (12/2015)
Function 22: Confidence interval for a standardized mean difference (within-subjects design)
CIstdmean2WS <- function(alpha, m1, m2, sd1, sd2, n, cor) {
# Computes confidence interval for a standardized mean difference
# in a within-subjects design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
mj:
sample mean in condition j
#
sdj:
sample standard deviation in condition j
#
n:
sample size
#
cor:
sample correlation
# Returns:
#
estimate, SE, lower limit, and upper limit for equal variance and
#
unequal variance methods plus single condition standardizer
z <- qnorm(1 - alpha/2)
s <- sqrt((sd1^2 + sd2^2)/2)
df <- n - 1
v1 <- sd1^2
v2 <- sd2^2
vd <- v1 + v2 - 2*cor*sd1*sd2
est1 <- (m1 - m2)/s
se1 <- sqrt(est1^2*(v1^2 + v2^2 + 2*cor^2*v1*v2)/(8*df*s^4) + vd/(df*s^2))
ll1 <- est1 - z*se1
ul1 <- est1 + z*se1
se2 <- sqrt(est1^2*(1 + cor^2)/(4*df) + 2*(1 - r)/n)
ll2 <- est1 - z*se2
ul2 <- est1 + z*se2
est3 <- (m1 - m2)/sd1
se3 <- sqrt(est3^2/(2*df) + vd/(df*v1))
ll3 <- est3 - z*se3
ul3 <- est3 + z*se3
est4 <- (m1 - m2)/sd2
se4 <- sqrt(est4^2/(2*df) + vd/(df*v2))
ll4 <- est4 - z*se4
ul4 <- est4 + z*se4
out1 <- t(c(est1, se1, ll1, ul1))
out2 <- t(c(est1, se2, ll2, ul2))
out3 <- t(c(est3, se3, ll3, ul3))
out4 <- t(c(est4, se4, ll4, ul4))
out <- rbind(out1, out2, out3, out4)
colnames(out) <- c("Estimate", "SE", "LL", "UL")
rownames1 <- c("Equal Variances Not Assumed:", "Equal Variances Assumed:")
rownames2 <- c("Condition 1 Standardizer:", "Condition 2 Standardizer:")
rownames(out) <- c(rownames1, rownames2)
return(out)
}
Example:
CIstdmean2WS(.05, 110.4, 102.1, 15.3, 14.6, 25, .75)
Equal Variances Not Assumed:
Equal Variances Assumed:
Condition 1 Standardizer:
Condition 2 Standardizer:
Estimate
0.5550319
0.5550319
0.5424837
0.5684932
SE
0.1609934
0.1581582
0.1615500
0.1692955
LL
0.2394905
0.2450476
0.2258515
0.2366800
UL
0.8705732
0.8650162
0.8591158
0.9003063
D.G. Bonett (12/2015)
Function 23: Confidence interval for a linear contrast of means (between-subjects design)
CImeanBS <- function(alpha, m, sd, n, c) {
# Computes confidence interval and test statistic for a linear contrast of means
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
m:
vector of sample means
#
sd:
vector of sample standard deviations
#
n:
vector of sample sizes
#
c:
vector of contrast coefficients
# Returns:
#
estimate, SE, df, t-value, p-value, lower limit, upper limit
#
for both equal variance and unequal variance methods
est <- t(c)%*%m
k <- length(m)
df1 <- sum(n) - k
v1 <- sum((n - 1)*sd^2)/df1
se1 <- sqrt(v1*t(c)%*%solve(diag(n))%*%c)
t1 <- est/se1
p1 <- 2*(1 - pt(abs(t1),df1))
tcrit1 <- qt(1 - alpha/2, df1)
ll1 <- est - tcrit1*se1
ul1 <- est + tcrit1*se1
v2 <- diag(sd^2)%*%(solve(diag(n)))
se2 <- sqrt(t(c)%*%v2%*%c)
t2 <- est/se2
df2 = (se2^4)/sum(((c^4)*(sd^4)/(n^2*(n - 1))))
p2 <- 2*(1 - pt(abs(t2),df2))
tcrit2 <- qt(1 - alpha/2, df2)
ll2 <- est - tcrit2*se2
ul2 <- est + tcrit2*se2
out1 <- t(c(est, se1, t1, df1, p1, ll1, ul1))
out2 <- t(c(est, se2, t2, df2, p2, ll2, ul2))
out <- rbind(out1, out2)
colnames(out) <- c("Estimate", "SE", "t", "df", "p-value", "LL", "UL")
rownames(out) <- c("Equal Variances Assumed:", "Equal Variances Not Assumed:")
return(out)
}
Example:
m = c(33.5, 37.9, 38.0, 44.1)
sd = c(3.84, 3.84, 3.65, 4.98)
n = c(10,10,10,10)
c = c(.5, .5, -.5, -.5)
CImeanBS(.05, m, sd, n, c)
Equal Variances Assumed:
Equal Variances Not Assumed:
Estimate
SE
t
df
p-value
LL
UL
-5.35 1.300136 -4.114955 36.00000 0.0002152581 -7.986797 -2.713203
-5.35 1.300136 -4.114955 33.52169 0.0002372436 -7.993583 -2.706417
D.G. Bonett (12/2015)
Function 24: Confidence interval for a standardized linear contrast of means (between-subjects
design)
CIstdmeanBS <- function(alpha, m, sd, n, c) {
# Computes confidence interval for a standardized mean difference
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
m:
vector of sample means
#
sd:
vector of sample standard deviation
#
n:
vector of sample sizes
#
c:
vector of contrast coefficients
# Returns:
#
estimate, SE, lower limit, and upper limit for equal variance and
#
unequal variance methods plus single group standardizer
z <- qnorm(1 - alpha/2)
v <- sd^2
a <- length(m)
s <- sqrt(sum(v)/a)
df <- sum(n) - a
sp <- sqrt(sum((n-1)*v)/df)
est1 <- (t(c)%*%m)/s
est2 <- (t(c)%*%m)/sp
a1 <- est1^2/(a^2*s^4)
a2 <- a1*sum((v^2/(2*(n - 1))))
a3 <- sum((c^2*v/(n - 1)))/s^2
se1 <- sqrt(a2 + a3)
ll1 <- est1 - z*se1
ul1 <- est1 + z*se1
a1 <- est2^2/a^2
a2 <- a1*sum(1/(2*(n - 1)))
a3 <- sum(c^2/n)
se2 <- sqrt(a2 + a3)
ll2 <- est2 - z*se2
ul2 <- est2 + z*se2
out1 <- t(c(est1, se1, ll1, ul1))
out2 <- t(c(est2, se2, ll2, ul2))
out <- rbind(out1, out2)
colnames(out) <- c("Estimate", "SE", "LL", "UL")
rownames(out) <- c("Equal Variances Not Assumed", "Equal Variances Assumed:")
return(out)
}
Example:
m = c(33.5, 37.9, 38.0, 44.1)
sd = c(3.84, 3.84, 3.65, 4.98)
n = c(10,10,10,10)
c = c(.5, .5, -.5, -.5)
CIstdmeanBS(.05, m, sd, n, c)
Estimate
SE
LL
UL
Equal Variances Not Assumed -1.301263 0.3692800 -2.025039 -0.5774878
Equal Variances Assumed:
-1.301263 0.3514511 -1.990095 -0.6124317
D.G. Bonett (12/2015)
Function 25: Confidence interval for one median
CImedian <- function(alpha, y) {
# Computes confidence interval for a median
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
y:
vector of sample data
# Returns:
#
confidence interval
n <- length(y)
y <- sort(y)
z <- qnorm(1 - alpha/2)
c1 <- round((n - z*sqrt(n))/2)
if (c1 < 1) {c1 = 1}
if (round(n/2) == n/2)
{median = (y[n/2] + y[n/2 + 1])/2}
else
{median = y[floor(n/2) + 1]}
L <- y[c1]
U <- y[n - c1 + 1]
a <- round((n + 1)/2 - sqrt(n))
if (a < 1) {a = 1}
L1 <- y[a]
U1 <- y[n - a + 1]
p <- pbinom(a - 1, size = n, prob = .5)
z0 <- qnorm(1 - p)
se <- (U1 - L1)/(2*z0)
out <- t(c(median, se, L, U))
colnames(out) <- c("Median", "SE", "LL", "UL")
return(out)
}
Example:
dollars = c(30, 20, 15, 10, 10, 60, 20, 25, 20, 30, 10, 5, 50, 40, 20, 10, 0, 20, 50)
CImedian(.05, dollars)
Median
SE LL UL
[1,]
20 5.390263 10 30
D.G. Bonett (12/2015)
Function 26: Confidence interval for median difference (between-subjects design)
CImedian2 <- function(alpha, y1, y2) {
# Computes confidence interval for a median difference
# in a between-subjects design
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
y1:
vector of sample data for group 1
#
y2:
vector of sample data for group 2
# Returns:
#
confidence interval
z <- qnorm(1 - alpha/2)
n1 <- length(y1)
y1 <- sort(y1)
n2 <- length(y2)
y2 <- sort(y2)
if (round(n1/2) == n1/2)
{median1 = (y1[n1/2] + y1[n1/2 + 1])/2}
else
{median1 = y1[floor(n1/2) + 1]}
if (round(n2/2) == n2/2)
{median2 = (y2[n2/2] + y2[n2/2 + 1])/2}
else
{median2 = y2[floor(n2/2) + 1]}
a1 <- round((n1 + 1)/2 - sqrt(n1))
if (a1 < 1) {a1 = 1}
L1 <- y1[a1]
U1 <- y1[n1 - a1 + 1]
p = pbinom(a1 - 1, size = n1, prob = .5)
z0 = qnorm(1 - p)
se1 <- (U1 - L1)/(2*z0)
a2 <- round((n2 + 1)/2 - sqrt(n2))
if (a2 < 1) {a2 = 1}
L2 <- y2[a2]
U2 <- y2[n2 - a2 + 1]
p = pbinom(a2 - 1, size = n2, prob = .5)
z0 = qnorm(1 - p)
se2 <- (U2 - L2)/(2*z0)
diff = median1 - median2
se = sqrt(se1^2 + se2^2)
L = diff - z*se
U = diff + z*se
out <- t(c(diff, se, L, U))
colnames(out) <- c("Median1-Median2", "SE", "LL", "UL")
return(out)
}
Example:
group1 = c(32, 39, 26, 35, 43, 27, 40, 37, 34, 29)
group2 = c(36, 44, 47, 42, 49, 39, 46, 31, 33, 48)
CImedian2(.05,group1, group2)
Median1-Median2
SE
LL
UL
[1,]
-8.5 4.316291 -16.95977 -0.04022524
D.G. Bonett (12/2015)
Function 27: Confidence interval for Mann-Whitney parameter
CIMann <- function(alpha, y1, y2){
# Compute confidence interval for probability of
# scoring higher in condition 1 than condition 2
# Arguments:
#
alpha: alpha level for 1-alpha confidence
#
y1:
vector of scores for group 1
#
y2:
vector of scores for group 2
# Returns:
#
confidence interval
z <- qnorm(1 - .05/2)
y <- c(y1,y2)
n1 <- length(y1)
n2 <- length(y2)
n <- n1 + n2
r <- rank(y)
r1 <- r[1:n1]
r2 <- r[(n1 + 1):n]
m1 <- mean(r1)
m2 <- mean(r2)
seq1 <- seq(1,n1,1)
seq2 <- seq(1,n2,1)
a1 <- sum((r1 - seq1)^2)
a2 <- sum((r2 - seq2)^2)
v1 <- (a1 - n1*(m1 - (n1 + 1)/2)^2)/((n1 - 1)*n^2)
v2 <- (a2 - n2*(m2 - (n2 + 1)/2)^2)/((n2 - 1)*n^2)
U <- sum(r2) - n2*(n2 + 1)/2
est <- U/(n1*n2)
se <- sqrt((n2*v1 + n1*v2)/(n1*n2))
LL <- est - z*se
UL <- est + z*se
if (UL > 1) {UL = 1}
if (LL < 0) {LL = 0}
out <- c(LL, UL)
return(out)
}
Example:
group1 = c(32, 39, 26, 35, 43, 27, 40, 37, 34, 29)
group2 = c(36, 44, 47, 42, 49, 39, 46, 31, 33, 48)
CIMann(.05, group1, group2)
[1] 0.5202456 1.000000
D.G. Bonett (12/2015)
Function 28: Confidence interval for ratio of means (between-subjects design)
CIMeanRatioBS <- function(alpha, y1, y2){
# Compute confidence interval for ratio of two
# population means of ratio-scale measurements.
# Equal variances is not assumed
# Args:
#
alpha: alpha level for 1-alpha confidence
#
y1:
vector of scores for group 1
#
y2:
vector of scores for group 2
# Returns:
#
confidence interval
n1 <- length(y1)
n2 <- length(y2)
m1 <- mean(y1)
m2 <- mean(y2)
v1 <- var(y1)
v2 <- var(y2)
var <- v1/(n2*m1^2) + v2/(n2*m2^2)
df <- var^2/(v1^2/(m1^4*(n1^3 - n1^2)) + v2^2/(m2^4*(n2^3 - n2^2)))
tcrit <- qt(1 - alpha/2, df)
est <- log(m1/m2)
se <- sqrt(var)
LL <- exp(est - tcrit*se)
UL <- exp(est + tcrit*se)
out <- c(LL, UL)
return(out)
}
Example:
> y2 = c(32, 39, 26, 35, 43, 27, 40, 37, 34, 29, 49, 42, 40)
> y1 = c(36, 44, 47, 42, 49, 39, 46, 31, 33, 48)
> CIMeanRatioBS(.05, y1, y2)
[1] 1.042725 1.412129
D.G. Bonett (12/2015)
Function 29: Confidence interval for ratio of means (within-subjects design)
CIMeanRatioWS <- function(alpha, y1, y2){
# Compute confidence interval for ratio of two
# population means of ratio-scale measurements.
# Equal variances is not assumed.
# Args:
#
alpha: alpha level for 1-alpha confidence
#
y1:
vector of scores for group 1
#
y2:
vector of scores for group 2
# Returns:
#
confidence interval
n <- length(y1)
m1 <- mean(y1)
m2 <- mean(y2)
v1 <- var(y1)
v2 <- var(y2)
cor <- cor(y1,y2)
var <- (v1/m1^2 + v2/m2^2 - 2*cor*sqrt(v1*v2)/(m1*m2))/n
df <- n - 1
tcrit <- qt(1 - alpha/2, df)
est <- log(m1/m2)
se <- sqrt(var)
LL <- exp(est - tcrit*se)
UL <- exp(est + tcrit*se)
out <- c(LL, UL)
return(out)
}
Example:
> y1 = c(3.3, 3.6, 3.0, 3.1, 3.9, 4.2, 3.5, 3.3)
> y2 = c(3.0, 3.1, 2.7, 2.6, 3.2, 3.8, 3.2, 3.0)
> CIMeanRatioWS(.05, y1, y2)
[1] 1.094170 1.175583
D.G. Bonett (12/2015)
Part V – Miscellaneous Procedures
Generate Random Sample
Example: Randomly select 25 participants from a study population of 3000 people
sort(sample(3000, 25, replace = FALSE, prob = NULL))
[1] 170 259 280 559 637 681 815 826 894 1028 1069 1211 1285 1292 1313
[16] 1463 1492 1585 1617 1634 1749 1871 1929 2194 2839
Randomize Sample into Groups
Example: Randomly assign a sample of 34 participants into three groups with 11 in
group 1, 11 in group 2, and 12 in group 3
k = 3; n1 = 11; n2 = 11; n3 = 12
sample(rep(1:k, c(n1, n2, n3)))
[1] 3 2 1 1 3 1 1 2 3 1 2 3 2 1 2 1 2 3 3 2 3 1 2 3 3 1 3 3 3 2 2 1 1 2
Create Bar Chart with CI Lines
Example 1: 3 group bar chart – specify the mean, LL, and UL for each group
library(ggplot2)
data <- read.table(text = "group mean LL UL
Treatment_A 7.43 5.03 9.83
Treatment_B 2.72 0.62 4.82
Treatment_C 5.27 2.97 7.57
", header = T)
ggplot(data, aes(x = group, y = mean)) +
geom_bar(stat = "identity", position = position_dodge()) +
geom_errorbar(aes(ymin = LL, ymax = UL), width = .25)
D.G. Bonett (12/2015)
Example 2: Clustered bar chart for 2x2 design – specify the mean, LL, and UL for each
group
library(ggplot2)
data <- read.table(text = "Gender Treatment Mean LL UL
Male
A
7.43 5.03 9.83
Female A
2.72 0.62 4.82
Male
B
4.27 1.97 6.57
Female B
5.27 2.97 7.57
", header = T)
dodge <- position_dodge(width = 0.9)
ggplot(data, aes(fill = Gender, y = Mean, x = Treatment)) +
geom_bar(stat = "identity", position = dodge) +
geom_errorbar(aes(ymin = LL, ymax = UL), position = dodge, width = .25)
Compute a Confidence Interval for 𝜼𝟐 (or partial eta-squared)
Install the MBESS package and call the ci.R2 function (see below). Set R2 equal to
sample eta-squared or sample partial eta-squared, set N equal to the total sample
size, and set K equal to the numerator df (levels of factor minus 1).
Example: One-factor design with 3 groups, a total sample size of 36, and a sample
eta-squared value of .569. The 95% confidence interval for the population eta-squared
is [.30, .69].
library(MBESS)
ci.R2(R2 = .569, N = 36, K = 2, conf.level = .95, Random.Predictors = F)
$Lower.Conf.Limit.R2
[1] 0.301424
$Upper.Conf.Limit.R2
[1] 0.6937411
D.G. Bonett (12/2015)
Compute Upper Limit for Variance Planning Value
UpperVar <- function(alpha, var, n) {
# Computes an upper limit for a population variance using
# a sample variance from a sample of size n
# Args:
#
alpha: alpha value for 1-alpha confidence
#
var:
sample variance
#
n:
sample size
# Returns:
#
upper limit variance planning value
c <- qchisq(alpha, (n - 1))
UL <- (n - 1)*var/c
return(UL)
}
Example: Compute a 75% upper limit for the population variance using a sample
variance of 15.0 that was obtained in a sample of size 60. The upper limit could be
used as a variance planning value.
UpperVar(.25, 15, 60)
[1] 17.23264
Download