· Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to...

47
D.G. Bonett (1/2018) 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 population # 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, r) { # Computes sample size required to estimate a population 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 # r: desired n2/n1 ratio # Returns: # required sample size per group (or n1 when r not equal to 1) z <- qnorm(1 - alpha/2) n <- ceiling(4*var*(1 + 1/r)*(z/w)^2 + z^2/4) return(n)

Transcript of · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to...

Page 1: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

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 population # 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, r) { # Computes sample size required to estimate a population 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 # r: desired n2/n1 ratio # Returns: # required sample size per group (or n1 when r not equal to 1) z <- qnorm(1 - alpha/2) n <- ceiling(4*var*(1 + 1/r)*(z/w)^2 + z^2/4) return(n)}

Example:

sizeCImean2(.05, 37.1, 5, 1)[1] 47

Page 2: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 3: Sample size to estimate a standardized mean difference (2-group design)

sizeCIstdmean2 <- function(alpha, d, w, r) { # Computes sample size required to estimate a population standardized # mean difference with desired precision in a 2-group design # Arguments: # alpha: alpha level for 1-alpha confidence # d: planning value of standardized mean difference # w: desired confidence interval width # r: desired n2/n1 ratio # Returns: # required sample size per group (or n1 if r not equal to 1) z <- qnorm(1 - alpha/2) n <- ceiling((d^2*(1 + r)/(2*r) + 4*(1 + r)/r)*(z/w)^2) return(n)}

Example:

sizeCIstdmean2(.05, .75, .5, 1)[1] 132

Function 4: Sample size to estimate a ratio of means (2-group design)

sizeCImeanRatioBS <- function(alpha, var, m1, m2, r) { # Computes sample size required to estimate a ratio of # population means 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)}

Example:

> sizeCImeanRatioBS(.05, .4, 3.5, 3.1, 1.2)[1] 70

Page 3: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 5: Sample size to estimate a linear contrast of means (between-subjects design)

sizeCImeanBS <- function(alpha, var, w, c) { # Computes sample size required to estimate a linear contrast of population # mean 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 a mean difference (within-subjects design)

sizeCImean2WS <- function(alpha, var, cor, w) { # Computes sample size required to estimate a difference in # population means 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

Page 4: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 7: Sample size to estimate a standardized mean difference (within-subjects design)

sizeCIstdmean2WS <- function(alpha, d, cor, w) { # Computes sample size required to estimate a population 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 ratio of means (within--subjects design)

sizeCImeanRatioWS <- function(alpha, var, m1, m2, cor, r) { # Computes sample size required to estimate a ratio of population # means with desired precision in a within-subjects design # Arguments: # alpha: alpha level for 1-alpha confidence # var: planning value of measurement 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

Page 5: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 9: Sample size to estimate a linear contrast of means (within-subjects design)

sizeCImeanWS <- function(alpha, var, cor, w, h) { # Computes sample size required to estimate a linear contrast of population # 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 a 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 # population 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

Page 6: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 11: Sample size to estimate a 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 # population 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 to be combined with # first-stage sample n <- ceiling(((w0/w)^2 - 1)*n0)return(n)}

Example:

sizeCIsecond(20, 5.3, 2.5)[1] 70

Page 7: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

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 population 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, r) { # Computes sample size required to test a difference in population # means 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 # r: desired n2/n1 ratio # Returns: # required sample size per group (or n1 when r not equal to 1) za <- qnorm(1 - alpha/2) zb <- qnorm(pow) n <- ceiling(var*(1 + 1/r)*(za + zb)^2/es^2 + za^2/4) return(n)}

Example:

sizePOWmean2(.05, 100, .95, 10, 1) [1] 27

Page 8: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

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 population # 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 difference in population # means 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

Page 9: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

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 population # 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

Page 10: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

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

Page 11: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

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 population standardized mean # difference in a 2-group design # 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)

Estimate SE LL ULEqual Variances Not Assumed 1.174493 0.2844012 0.6170771 1.731909Equal Variances Assumed: 1.174493 0.2802826 0.6251494 1.723837Group 1 Standardizer: 1.147541 0.3084007 0.5430867 1.751995Group 2 Standardizer: 1.203438 0.3013414 0.6128200 1.794057

Page 12: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

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 population 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)

Estimate SE LL ULEqual Variances Not Assumed: 0.5550319 0.1609934 0.2394905 0.8705732Equal Variances Assumed: 0.5550319 0.1581582 0.2450476 0.8650162Condition 1 Standardizer: 0.5424837 0.1615500 0.2258515 0.8591158Condition 2 Standardizer: 0.5684932 0.1692955 0.2366800 0.9003063

Page 13: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

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 population means in a between-subjects design # 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)

Estimate SE t df p-value LL ULEqual Variances Assumed: -5.35 1.300136 -4.114955 36.00000 0.0002152581 -7.986797 -2.713203Equal Variances Not Assumed: -5.35 1.300136 -4.114955 33.52169 0.0002372436 -7.993583 -2.706417

Page 14: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

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 population standardized linear # contrast of means in a between-subjects design # 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, confidence interval 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 ULEqual Variances Not Assumed: -1.301263 0.3692800 -2.025039 -0.5774878Equal Variances Assumed: -1.301263 0.3514511 -1.990095 -0.6124317

Page 15: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 25: Confidence interval for a standardized linear contrast of means (within-subjects design)

CIstdmeanWS <- function(alpha, m, sd, cor, n, h) { # Computes confidence interval for a population standardized linear # contrast of means in a within-subjects design (assumes equal # correlations) # Arguments: # alpha: alpha level for 1-alpha confidence # m: vector of sample means # sd: vector of sample standard deviations # cor: average of sample correlations # n: sample size # h: vector of contrast coefficients # Returns: # estimate, SE, confidence interval z <- qnorm(1 - alpha/2) a <- length(m) df <- n - 1 s <- sqrt(sum(sd^2)/a) est <- (t(h)%*%m)/s se1 <- sqrt(est^2*(1 + (a - 1)*cor^2)/(2*a*df) + (1 - cor)*t(h)%*%h/n) v1 <- est^2/(2*a^2*s^4*df) v2 <- sum(sd^4) v3 <- cor^2*t(sd^2)%*%sd^2 v4 <- sum(h^2*sd^2) v5 <- cor*t(h*sd)%*%(h*sd) se2 <- sqrt(v1*(v2 + v3) + (v4 - v5)/(df*s^2)) ll1 <- est - z*se1 ul1 <- est + z*se1 ll2 <- est - z*se2 ul2 <- est + z*se2 out1 <- t(c(est, se1, ll1, ul1)) out2 <- t(c(est, se2, ll2, ul2)) out <- rbind(out1, out2) colnames(out) <- c("Estimate", "SE", "LL", "UL") rownames(out) <- c("Equal Var & Cor Assumed:", "Only Equal Cor Assumed:") return(out)}

Example:

m = c(33.5, 37.9, 38.0)sd = c(2.65, 2.90, 3.25)cor = .82n = 20h = c(-.5, -.5, 1)CIstdmeanWS(.05, m, sd, cor, n, h)

Estimate SE LL ULEqual Var & Cor Assumed: 0.7813463 0.1614220 0.4649650 1.097728Only Equal Cor Assumed: 0.7813463 0.1580369 0.4715997 1.091093

Page 16: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 26: Confidence interval for one median

CImedian1 <- function(alpha, y) { # Computes confidence interval for a population median # Arguments: # alpha: alpha level for 1-alpha confidence # y: vector of sample scores # 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} median <- median(y) 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)CImedian1(.05, dollars) Median SE LL UL[1,] 20 5.390263 10 30

Page 17: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 27: Confidence interval for a median difference (2-group design)

CImedian2 <- function(alpha, y1, y2) { # Computes confidence interval for a difference of # population medians in a 2-group design # 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 - alpha/2) n1 <- length(y1) y1 <- sort(y1) n2 <- length(y2) y2 <- sort(y2) median1 <- median(y1) median2 <- median(y2) 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(median1, median2, diff, se, L, U)) colnames(out) <- c("Med1", "Med2", "Med1-Med2", "SE", "LL", "UL") return(out)}

Example:

CImedian2(.05,group1, group2) Med1 Med2 Med1-Med2 SE LL UL[1,] 34.5 43 -8.5 4.316291 -16.95977 -0.04022524

Page 18: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 28: Confidence interval for a Mann-Whitney parameter

CImann <- function(alpha, y1, y2){ # Compute distribution-free 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

Page 19: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 29: Confidence interval for a ratio of means (2-group design)

CImeanRatioBS <- function(alpha, y1, y2){ # Compute confidence interval for a ratio of population means # of ratio-scale measurements in a 2-group design. Equality of # variances is not assumed. # 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 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 <- t(c(m1, m2, exp(est), LL, UL)) colnames(out) <- c("Mean1", "Mean2", "Mean1/Mean2", "LL", "UL") return(out)}

Example:

group2 = c(32, 39, 26, 35, 43, 27, 40, 37, 34, 29, 49, 42, 40)group1 = c(36, 44, 47, 42, 49, 39, 46, 31, 33, 48)CImeanRatioBS(.05, group1, group2) Mean1 Mean2 Mean1/Mean2 LL UL[1,] 41.5 36.38462 1.140592 0.9897482 1.314425

Page 20: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 30: Confidence interval for a ratio of means (within-subjects design)

CImeanRatioWS <- function(alpha, y1, y2){ # Compute confidence interval for a ratio of population # means of ratio-scale measurements in a within-subjects # design. Equality of variances is not assumed. # Arguments: # alpha: alpha level for 1-alpha confidence # y1: vector of scores for condition 1 # y2: vector of scores for condition 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 <- t(c(m1, m2, exp(est), LL, UL)) colnames(out) <- c("Mean1", "Mean2", "Mean1/Mean2", "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) Mean1 Mean2 Mean1/Mean2 LL UL[1,] 3.4875 3.075 1.134146 1.09417 1.175583

Page 21: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 31: Confidence interval for a ratio of medians (2-group design)

CImedianRatioBS <- function(alpha, y1, y2) { # Computes confidence interval for a ratio of # population medians of ratio scale measurements # in a 2-group design. # 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 - alpha/2) n1 <- length(y1) y1 <- sort(y1) n2 <- length(y2) y2 <- sort(y2) median1 <- median(y1) median2 <- median(y2) y1 <- log(y1 + 1) y2 <- log(y2 + 1) a1 <- round((n1 + 1)/2 - sqrt(n1)) if (a1 < 1) {a1 = 1} L1 <- log(exp(y1[a1]) - 1) U1 <- log(exp(y1[n1 - a1 + 1]) - 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 <- log(exp(y2[a2]) - 1) U2 <- log(exp(y2[n2 - a2 + 1]) - 1) p <- pbinom(a2 - 1, size = n2, prob = .5) z0 <- qnorm(1 - p) se2 <- (U2 - L2)/(2*z0) se <- sqrt(se1^2 + se2^2) diff <- log(median1) - log(median2) L <- exp(diff - z*se) U <- exp(diff + z*se) out <- t(c(median1, median2, exp(diff), L, U)) colnames(out) <- c("Med1", "Med2", "Med1/Med2", "LL", "UL") return(out)}

Example:

group2 = c(32, 39, 26, 35, 43, 27, 40, 37, 34, 29, 49, 42, 40)group1 = c(36, 44, 47, 42, 49, 39, 46, 31, 33, 48)CImedianRatioBS(.05, group1, group2) Med1 Med2 Med1/Med2 LL UL[1,] 43 37 1.162162 0.927667 1.455933

Page 22: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 32: Confidence interval for mean absolute deviation (1-group design)

CImd1 <- function(alpha, y) { # Computes confidence interval for a population MD # Arguments: # alpha: alpha level for 1-alpha confidence # y: vector of sample data # Returns: # estimated MD and confidence interval n <- length(y) z <- qnorm(1 - alpha/2) c <- n/(n - 1) median <- median(y) md <- mean(abs(y - median)) skw <- (mean(y) - median)/md kur <- (sd(y)/md)^2 se <- sqrt((skw^2 + kur - 1)/n) L <- exp(log(c*md) - z*se) U <- exp(log(c*md) + z*se) out <- t(c(c*md, L, U)) colnames(out) <- c("MD", "LL", "UL") return(out)}

Example:

score = c(30, 20, 15, 10, 10, 60, 20, 25, 20, 30, 10, 5, 50, 40, 20, 10, 0, 20, 50)CImd1(.05, score) MD LL UL[1,] 12.5 7.962667 19.62282

Page 23: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 33: Confidence interval for a ratio of mean absolute deviations (2-group design)

CImdRatio <- function(alpha, y1, y2) { # Computes confidence interval for a ratio of population MDs # from a two-group 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: # estimated MD1/MD2 and confidence interval z <- qnorm(1 - alpha/2) n1 <- length(y1) c1 <- n1/(n1 - 1) n2 <- length(y2) c2 <- n2/(n2 - 1) median1 <- median(y1) median2 <- median(y2) md1 <- mean(abs(y1 - median1)) skw1 <- (mean(y1) - median1)/md1 kur1 <- (sd(y1)/md1)^2 var1 <- (skw1^2 + kur1 - 1)/n1 md2 <- mean(abs(y2 - median2)) skw2 <- (mean(y2) - median2)/md2 kur2 <- (sd(y2)/md2)^2 var2 <- (skw2^2 + kur2 - 1)/n2 se <- sqrt(var1 + var2) c <- c1/c2 est <- md1/md2 L <- exp(log(c*est) - z*se) U <- exp(log(c*est) + z*se) out <- t(c(c1*md1, c2*md2, c*est, L, U)) colnames(out) <- c("MD1", "MD2", "MD1/MD2", "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)CImdRatio(.05, group1, group2) MD1 MD2 MD1/MD2 LL UL[1,] 5.111111 5.888889 0.8679245 0.4520879 1.666253

Page 24: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 34: Confidence Interval for a ratio of mean absolute deviations (within-subjects design)

CImdRatioWS <- function(alpha, y1, y2) { # Computes confidence interval for a ratio of population MDs # from a within-subjects design # Arguments: # alpha: alpha level for 1-alpha confidence # y1: vector of measurement 1 scores # y2: vector of measurement 2 scores # Returns: # estimated MD1/MD2 and confidence interval z <- qnorm(1 - alpha/2) n <- length(y1); c <- n/(n - 1) median1 <- median(y1); median2 <- median(y2); md1 <- mean(abs(y1 - median1)) skw1 <- (mean(y1) - median1)/md1 kur1 <- (sd(y1)/md1)^2 var1 <- (skw1^2 + kur1 - 1)/n md2 <- mean(abs(y2 - median2)) skw2 <- (mean(y2) - median2)/md2 kur2 <- (sd(y2)/md2)^2 var2 <- (skw2^2 + kur2 - 1)/n d1 <- abs(y1 - median1); d2 <- abs(y2 - median2) cor <- cor(d1, d2) se <- sqrt(var1 + var2 - 2*cor*sqrt(var1*var2)) est <- md1/md2 L <- exp(log(est) - z*se) U <- exp(log(est) + z*se) out <- t(c(c*md1, c*md2, est, L, U)) colnames(out) <- c("MD1", "MD2", "MD1/MD2", "LL", "UL") return(out)}

Example:

score2 = c(21, 4, 9, 12, 35, 18, 10, 22, 24, 1, 6, 8, 13, 16, 19)score1 = c(67, 28, 30, 28, 52, 40, 25, 37, 44, 10, 14, 20, 28, 40, 51)CImdRatioWS(.05, score1, score2) MD1 MD2 MD1/MD2 LL UL[1,] 12.71429 7.5 1.695238 1.109176 2.590961

Page 25: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 35: Confidence interval for difference in medians (within-subjects design)

CImedian2WS <- function(alpha, y1, y2) { # Computes confidence interval for a difference of # population medians in a 2-level within-subjects design # Arguments: # alpha: alpha level for 1-alpha confidence # y1: vector of scores for level 1 # y2: vector of scores for level 2 # Returns: # sample medians, SE of difference, confidence interval z <- qnorm(1 - alpha/2) n <- length(y1) y1 <- sort(y1) y2 <- sort(y2) median1 <- median(y1) median2 <- median(y2) a <- round((n + 1)/2 - sqrt(n)) if (a < 1) {a1 = 1} p <- pbinom(a - 1, size = n, prob = .5) z0 <- qnorm(1 - p) L1 <- y1[a] U1 <- y1[n - a + 1] se1 <- (U1 - L1)/(2*z0) L2 <- y2[a] U2 <- y2[n - a + 1] se2 <- (U2 - L2)/(2*z0) a1 <- (y1 < median1) a2 <- (y2 < median2) a3 <- a1 + a2 a4 <- sum(a3 == 2) if (n/2 == trunc(n/2)) { p00 <- (sum(a4)+.25)/(n + 1) } else { p00 <- (sum(a4) + .25)/n } cov <- (4*p00 - 1)*se1*se2 diff <- median1 - median2 se <- sqrt(se1^2 + se2^2 - 2*cov) L <- diff - z*se U <- diff + z*se out <- t(c(median1, median2, diff, se, L, U)) colnames(out) <- c("Med1", "Med2", "Med1-Med2", "SE", "LL", "UL") return(out)}

Example:

y1 = c(21, 4, 9, 12, 35, 18, 10, 22, 24, 1, 6, 8, 13, 16, 19)y2 = c(67, 28, 30, 28, 52, 40, 25, 37, 44, 10, 14, 20, 28, 40, 51)CImedian2WS(.05, y1, y2) Med1 Med2 Med1-Med2 SE LL UL[1,] 13 30 -17 1.970661 -20.86243 -13.13757

Page 26: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 36: Confidence interval for ratio of medians (within-subjects design)

CImedianRatioWS <- function(alpha, y1, y2) { # Computes confidence interval for a ratio of # population medians in a 2-level within-subjects design # Arguments: # alpha: alpha level for 1-alpha confidence # y1: vector of scores for level 1 # y2: vector of scores for level 2 # Returns: # sample medians, SE of difference, confidence interval z <- qnorm(1 - alpha/2) n <- length(y1) y1 <- sort(y1) y2 <- sort(y2) median1 <- median(y1) median2 <- median(y2) y1 <- log(y1 + 1) y2 <- log(y2 + 1) a <- round((n + 1)/2 - sqrt(n)) if (a < 1) {a1 = 1} p <- pbinom(a - 1, size = n, prob = .5) z0 <- qnorm(1 - p) L1 <- log(exp(y1[a]) - 1) U1 <- log(exp(y1[n - a + 1]) - 1) se1 <- (U1 - L1)/(2*z0) L2 <- log(exp(y2[a]) - 1) U2 <- log(exp(y2[n - a + 1]) - 1) se2 <- (U2 - L2)/(2*z0) a1 <- (y1 < log(median1)) a2 <- (y2 < log(median2)) a3 <- a1 + a2 a4 <- sum(a3 == 2) if (n/2 == trunc(n/2)) { p00 <- (sum(a4) + .25)/(n + 1) } else { p00 <- (sum(a4) + .25)/n } cov <- (4*p00 - 1)*se1*se2 diff <- log(median1) - log(median2) se <- sqrt(se1^2 + se2^2 - 2*cov) L <- exp(diff - z*se) U <- exp(diff + z*se) out <- t(c(median1, median2, exp(diff), L, U)) colnames(out) <- c("Med1", "Med2", "Med1/Med2", "LL", "UL") return(out)}

Example:

y1 = c(21, 4, 9, 12, 35, 18, 10, 22, 24, 1, 6, 8, 13, 16, 19)y2 = c(67, 28, 30, 28, 52, 40, 25, 37, 44, 10, 14, 20, 28, 40, 51)CImedianRatioWS(.05, y1, y2) Med1 Med2 Med1/Med2 LL UL[1,] 13 30 0.4333333 0.3094838 0.6067451

Page 27: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 37: Tukey-Kramer unequal variance confidence intervals for all pairwise comparisons of means (between-subjects design)

CImeanTukey <-function(alpha, mean, sd, n) { # Computes Tukey-Kramer confidence interval for all pairwise # comparisons of means - equal variances are not assumed # Arguments: # alpha: alpha level for 1-alpha confidence # mean: vector of sample means # sd: vector of sample standard deviations # n: vector of sample sizes # Returns: # estimated difference, SE, test statistic, df, # adjusted p-value, confidence interval a = length(mean) v1 <- sd^2/n v2 <- sd^4/(n^2*(n - 1)) mean <- outer(mean, mean, '-') Diff <- mean[upper.tri(mean)] v1 <- outer(v1, v1, "+") v2 <- outer(v2, v2, "+") df = v1^2/v2 df <- df[upper.tri(df)] SE <- sqrt(v1[upper.tri(v1)]) t <- Diff/SE q <- qtukey(p = 1 - alpha, nmeans = a, df = df)/sqrt(2) p <- 1 - ptukey(sqrt(2)*abs(t), nmeans = a, df = df) p <- round(p*1000)/1000 LL <- Diff - q*SE UL <- Diff + q*SE pair <- t(combn(seq(1:a), 2)) out <- cbind(pair, Diff, SE, t, p, df, LL, UL) rownames(out) <- rep("", a*(a - 1)/2) return(out)}

Example:

mean = c(12.86, 17.57, 26.29)sd = c(3.185, 3.995, 3.773)n = c(7, 7, 7)CImeanTukey(.05, mean, sd, n)

Diff SE t p df LL UL 1 2 -4.71 1.931108 -2.439014 0.076 11.43254 -9.896569 0.4765685 1 3 -13.43 1.866232 -7.196318 0.000 11.67131 -18.427800 -8.4321995 2 3 -8.72 2.076933 -4.198499 0.003 11.96099 -14.263410 -3.1765902

Page 28: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Part IV – Confidence Interval Interpretation

Function 38: Interpret confidence interval for a single parameter (1-group design)

InterpretCI1 <- function(conf, N, pop, parm, dv, LL, UL) { # Generates an interpretation of a confidence interval for a # single population parameter. # Arguments: # conf: confidence level # N: approximate study population size (no comma) # pop: description of study population # parm: type of parameter (e.g., mean, median, MD) # dv: description of response variable # LL: lower limit of computed confidence interval # UL: upper limit of computed confidence interval # Returns: # interpretation of confidence interval s1 <- "We can be "; s2 <- paste("% confident that the", parm, dv, sep = " ") s3 <- " for all" s4 <- "is between" s5 <- "and" s6 <- paste(UL, ".", sep = "") o2 <- paste(s3, sep = " ") o3 <- paste(N, pop, s4, LL, s5, s6, sep = " ") cat(paste(s1, conf, s2, o2, sep = ""), fill = T) cat(o3, fill = T)}

Example:

conf = 95N = 13800pop = "Watsonville residents"parm = "mean"dv = "dollar donation amount"LL = 15.14UL = 30.36InterpretCI1(conf, N, pop, parm, dv, LL, UL)

We can be 95% confident that the mean dollar donation amount for all13800 Watsonville residents is between 15.14 and 30.36.

Page 29: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 39: Interpret confidence interval for a difference in two parameters (2-group experimental design)

InterpretCI2E <- function(conf, N, pop, parm, dv, T1, T2, LL, UL) { # Generates an interpretation of a confidence interval for a # difference in parameters in a 2-group experimental design. # Arguments: # conf: confidence level # N: approximate study population size (no comma) # pop: description of study population # parm: type of parameter (e.g., mean, median, MD) # dv: description of response variable # T1: description of treatment condition 1 # T2: description of treatment condition 2 # LL: lower limit of computed confidence interval # UL: upper limit of computed confidence interval # Returns: # interpretation of confidence interval s1 <- paste("We can be ", conf, "% confident that if all ", sep = "") s2 <- paste(N, pop, "had received the", T1, sep = " ") s3 <- paste(", their", parm, dv, sep = " ") if (LL > 0) { s4 <- paste("would be between", LL, "and", UL, sep = " ") s5 <- paste("greater than their", parm, dv, sep = " ") } else if (UL < 0) { s4 <- paste("would be between", abs(UL), "and", abs(LL), sep = " ") s5 <- paste("less than their", parm, dv, sep = " ") } else { s4 <- paste("would be between", abs(LL), "less than and", abs(UL), sep = " ") s5 <- paste("greater than their", parm, dv, sep = " ") } s6 <- paste("if they had instead all received the ", T2, ".", sep = "") cat(paste(s1, s2, s3, sep = ""), fill = T) cat(paste(s4, s5, s6), fill = T)}

Example:

conf = 95N = 3500pop = "UCSC social science students"parm = "mean"dv = "reading comprehension score"T1 = "new teaching method"T2 = "old teaching method"LL = 12.16UL = 27.82InterpretCI2E(conf, N, pop, parm, dv, T1, T2, LL, UL)

We can be 95% confident that if all 3500 UCSC social science students had received the new teaching method, their mean reading comprehension score would be between 12.16 and 27.82 greater than their mean reading comprehension score if they had instead all received the old teaching method.

Page 30: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 40: Interpret confidence interval for a difference in two parameters (2-group non-experimental design)

InterpretCI2N <- function(conf, N1, N2, pop1, pop2, parm, dv, LL, UL) { # Generates an interpretation of a confidence interval for a difference # in two parameters in a 2-group non-experimental design. # Arguments: # conf: confidence level # N1: approximate study population 1 size (no comma) # N2: approximate study population 2 size (no comma) # pop1: description of study population 1 # pop2: description of study population 2 # parm: type of parameter (e.g., mean, median, MD) # dv: description of response variable # LL: lower limit of computed confidence interval # UL: upper limit of computed confidence interval # Returns: # interpretation of confidence interval s1 <- paste("We can be ", conf, "% confident that the", sep = "") s2 <- paste(parm, dv, sep = " ") s3 <- paste("of the", N1, pop1, sep = " ") if (LL > 0) { s4 <- paste("is between", LL, "and", UL, sep = " ") s5 <- paste(" greater than the", parm, dv, sep = " ") } else if (UL < 0) { s4 <- paste("is between", abs(UL), "and", abs(LL), sep = " ") s5 <- paste(" less than the", parm, dv, sep = " ") } else { s4 <- paste("is between", abs(LL), "less than and", UL, sep = " ") s5 <- paste(" greater than the", parm, dv, sep = " ") } s6 <- paste(" of the", N2, pop2, sep = " ") cat(paste(s1, s2, s3, sep = " "), fill = T) cat(paste(s4, s5, s6, ".", sep = ""), fill = T)}

Example:

conf = 95N1 = 8250N2 = 9660pop1 = "female undergraduate students"pop2 = "male undergraduate students"parm = "mean"dv = "cultural sensitivity score"LL = 6.82UL = 11.15InterpretCI2N(conf, N1, N2, pop1, pop2, parm, dv, LL, UL)

We can be 95% confident that the mean cultural sensitivity score of the 8250 female undergraduate students is between 6.82 and 11.15 greater than the mean cultural sensitivity score of the 9660 male undergraduate students.

Function 41: Interpret confidence interval for a standardized mean difference (2-group

Page 31: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

experimental design)

InterpretCICohenE <- function(conf, N, pop, dv, T1, T2, LL, UL) { # Generates an interpretation of a confidence interval for a standardized # mean difference (Cohen's d) in a 2-group experimental design. # Arguments: # conf: confidence level # N: approximate study population size (no comma) # pop: description of study population # dv: description of response variable # T1: description of treatment condition 1 # T2: description of treatment condition 2 # LL: lower limit of computed confidence interval # UL: upper limit of computed confidence interval # Returns: # interpretation of confidence interval s1 <- paste("We can be ", conf, "% confident that if all ", sep = "") s2 <- paste(N, pop, "had received the", T1, sep = " ") s3 <- paste(", their mean", dv, sep = " ") if (LL > 0) { s4 <- paste("would be between", LL, "and", UL, sep = " ") s5 <- paste("standard deviations greater than their mean", dv, sep = " ") } else if (UL < 0) { s4 <- paste("would be between", abs(UL), "and", abs(LL), sep = " ") s5 <- paste("standard deviations less than their mean", dv, sep = " ") } else { s4 <- paste("would be between", abs(LL), "standard deviations less and", sep = " ") s5 <- paste(abs(UL), "standard deviations greater than their mean", dv, sep = " ") } s6 <- paste("if they had instead all received the ", T2, ".", sep = "") cat(paste(s1, s2, s3, sep = ""), fill = T) cat(paste(s4, s5, s6), fill = T)}

Example:

conf = 95N = 20000pop = "Alzheimer's patients"dv = "memory test score"T1 = "Aricept"T2 = "new drug"LL = -.57UL = .63InterpretCICohenE(conf, N, pop, dv, T1, T2, LL, UL)

We can be 95% confident that if all 20000 Alzheimer's patients had received the Aricept, their mean memory test score would be between 0.57 standard deviations less than and 0.63 standard deviations greater than their mean memory test score if they had instead all received the new drug.

Function 42: Interpret confidence interval for a standardized mean difference (2-group

Page 32: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

non-experimental design)

InterpretCICohenN <- function(conf, N1, N2, pop1, pop2, dv, LL, UL) { # Generates an interpretation of a confidence interval for a standardized # mean difference (Cohen's d) in a 2-group non-experimental design. # Arguments: # conf: confidence level # N1: approximate study population 1 size (no comma) # N2: approximate study population 2 size (no comma) # pop1: description of study population 1 # pop2: description of study population 2 # dv: description of response variable # LL: lower limit of computed confidence interval # UL: upper limit of computed confidence interval # Returns: # interpretation of confidence interval s1 <- paste("We can be ", conf, "% confident that the", sep = "") s2 <- paste("mean", dv, sep = " ") s3 <- paste("of the", N1, pop1, "is between", sep = " ") if (LL > 0) { s4 <- paste(LL, "and", UL, sep = " ") s5 <- paste(" standard deviations greater than the mean", dv, sep = " ") } else if (UL < 0) { s4 <- paste(abs(UL), "and", abs(LL), sep = " ") s5 <- paste(" standard deviations less than the mean", dv, sep = " ") } else { s4 <- paste(abs(LL), "standard deviations less than and", UL, sep = " ") s5 <- paste(" standard deviations greater than the mean", dv, sep = " ") } s6 <- paste(" of the", N2, pop2, sep = " ") cat(paste(s1, s2, s3, sep = " "), fill = T) cat(paste(s4, s5, s6, ".", sep = ""), fill = T)}

Example:

conf = 95N1 = 960N2 = 980pop1 = "middle school girls"pop2 = "middle school boys"dv = "number of hurtful messages"LL = .22UL = 1.35InterpretCICohenN(conf, N1, N2, pop1, pop2, dv, LL, UL)

We can be 95% confident that the mean number of hurtful messages of the 960 middle school girls is between 0.22 and 1.35 standard deviations greater than the mean number of hurtful messages of the 980 middle school boys.

Function 43: Interpret confidence interval for a ratio of two parameters (2-group

Page 33: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

experimental design)

InterpretCIRatioE <- function(conf, N, pop, parm, dv, T1, T2, LL, UL) { # Generates an interpretation of a confidence interval for a ratio of # two parameters in a 2-group experimental design. # Arguments: # conf: confidence level # N: approximate study population size (no comma) # pop: description of study population # parm: type of parameter (e.g., mean, median, MD) # dv: description of response variable # T1: description of treatment condition 1 # T2: description of treatment condition 2 # LL: lower limit of computed confidence interval # UL: upper limit of computed confidence interval # Returns: # interpretation of confidence interval s1 <- paste("We can be ", conf, "% confident that if all ", sep = "") if (LL > 1) { s2 <- paste(N, pop, "had received the", T1, sep = " ") s3 <- paste(", their", parm, dv, sep = " ") s4 <- paste("would be between", LL, "and", UL, sep = " ") s5 <- paste("times as large as their", parm, dv, sep = " ") s6 <- paste("if they had instead all received the ", T2, ".", sep = "") } else if (UL < 1) { a <- as.integer(100/UL)/100 b <- as.integer(100/LL)/100 s2 <- paste(N, pop, "had received the", T2, sep = " ") s3 <- paste(", their", parm, dv, sep = " ") s4 <- paste("would be between", a, "and", b, sep = " ") s5 <- paste("times as large as their", parm, dv, sep = " ") s6 <- paste("if they had instead all received the ", T1, ".", sep = "") } else { s2 <- paste(N, pop, "had received the", T2, sep = " ") s3 <- paste(", their", parm, dv, sep = " ") s4 <- paste("would be between", LL, "and", UL, sep = " ") s5 <- paste("times as large as their", parm, dv, sep = " ") s6 <- paste("if they had instead all received the ", T2, ".", sep = "") } cat(paste(s1, s2, s3, sep = ""), fill = T) cat(paste(s4, s5, s6), fill = T)}

Example:

conf = 95N = 1500pop = "UCSC research participant pool students"parm = "median"dv = "reading comprehension speed"T1 = "new teaching method"T2 = "old teaching method"LL = 1.16UL = 4.42InterpretCIRatioE(conf, N, pop, parm, dv, T1, T2, LL, UL)

We can be 95% confident that if all 1500 UCSC research participant pool students had received the new teaching method, their median reading comprehension speed would be between 1.16 and 4.42 times as large as their median reading speed if they had instead all received the old teaching method

Function 44: Interpret confidence interval for a ratio of two parameters (2-group

Page 34: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

non-experimental design)

InterpretCIRatioN <- function(conf, N1, N2, pop1, pop2, parm, dv, LL, UL) { # Generates an interpretation of a confidence interval for a difference # in two parameters in a 2-group non-experimental design. # Arguments: # conf: confidence level # N1: approximate study population 1 size (no comma) # N2: approximate study population 2 size (no comma) # pop1: description of study population 1 # pop2: description of study population 2 # parm: type of parameter (e.g., mean, median, MD) # dv: description of response variable # LL: lower limit of computed confidence interval # UL: upper limit of computed confidence interval # Returns: # interpretation of confidence interval s1 <- paste("We can be ", conf, "% confident that the", sep = "") s2 <- paste(parm, dv, sep = " ") if (LL > 1) { s3 <- paste("of the", N1, pop1, sep = " ") s4 <- paste("is between", LL, "and", UL, sep = " ") s5 <- paste(" times as large as the", parm, dv, sep = " ") s6 <- paste(" of the", N2, pop2, sep = " ") } else if (UL < 1) { a <- as.integer(100/UL)/100 b <- as.integer(100/LL)/100 s3 <- paste("of the", N2, pop2, sep = " ") s4 <- paste("is between", a, "and", b, sep = " ") s5 <- paste(" times as large as the", parm, dv, sep = " ") s6 <- paste(" of the", N1, pop1, sep = " ") } else { s3 <- paste("of the", N1, pop1, sep = " ") s4 <- paste("is between", LL, "and", UL, sep = " ") s5 <- paste(" times as large as the", parm, dv, sep = " ") s6 <- paste(" of the", N2, pop2, sep = " ") } cat(paste(s1, s2, s3, sep = " "), fill = T) cat(paste(s4, s5, s6, ".", sep = ""), fill = T)}

Example:

conf = 95N1 = 960N2 = 980pop1 = "middle school girls"pop2 = "middle school boys"parm = "MD"dv = "number of hurtful messages"LL = 1.49UL = 3.96InterpretCIRatioN(conf, N1, N2, pop1, pop2, parm, dv, LL, UL)

We can be 95% confident that the MD number of hurtful messages of the 960 middle school girls is between 1.49 and 3.96 times as large as the MD number of hurtful messages of the 980 middle school boys.

Part V – Miscellaneous Procedures

Page 35: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Function 45: Generate random sample

RandomSample <- function(popsize, samsize){ # Generates a random sample of participant IDs # Arguments: # popsize: study population size # samsize: sample sample # Returns: # participant IDs to use in study out <- sort(sample(popsize, samplesize, replace = FALSE, prob = NULL)) return(out)}

RandomSample(3000, 25)

[1] 37 94 134 186 212 408 485 697 722 781 998 1055 1182 1224 1273[16] 1335 1452 1552 1783 1817 2149 2188 2437 2850 2936

Function 46: Randomize sample into groups

Randomize <- function(n){ # Randomly assigns a sample of participants into k groups # Arguments: # n: kx1 vector of sample sizes per group # where k is number of groups # Returns: # assignment of ordered list of participants to groups k <- length(n) out <- sample(rep(1:k, n)) return(out)}

n = c(10, 10, 5)Randomize(n) [1] 2 3 2 1 1 2 3 3 2 1 2 1 3 1 1 2 3 1 1 2 2 1 1 2 2

Function 47: 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 # Arguments: # 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)}

UpperVar(.25, 15, 60)[1] 17.23264

Bar chart with CI lines

Page 36: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Example 1: 3 group bar chart – enter the group label, sample mean, LL, and UL for each group

library(ggplot2)data <- read.table(text = "group mean LL ULTreatment_A 7.43 5.03 9.83Treatment_B 2.72 0.62 4.82Treatment_C 5.27 2.97 7.57", header = T)ggplot(data, aes(x = group, y = mean)) + geom_bar(fill = "blue", stat = "identity", position = position_dodge()) + geom_errorbar(aes(ymin = LL, ymax = UL), width = .25)

Example 2: Clustered bar chart for 2x2 design – enter the group label, sample mean, LL, and UL for each group

library(ggplot2)data <- read.table(text = "Gender Treatment Mean LL ULMale A 7.43 5.03 9.83Female A 2.72 0.62 4.82Male B 4.27 1.97 6.57Female B 5.27 2.97 7.57", header = T)dodge <- position_dodge(width = 0.9)ggplot(data, aes(fill = Treatment, y = Mean, x = Gender)) + geom_bar(stat = "identity", position = dodge) + geom_errorbar(aes(ymin = LL, ymax = UL), position = dodge, width = .25) + scale_fill_brewer(palette = "Paired")#changing "Paired" to "Set1", "Set2", "Set3" or "Dark2" will give different color combinations

Adjusted p-values using Holm method

Page 37: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

Example: Create a vector of p-values (the program will rank order them internally) for all tests. The function will adjust each p-value using the Holm method. Reject H0 for any test with a Holm adjusted p-value less than the specified alpha value. The following example gives the p-values for all six pairwise comparisons in a one-factor design with four levels. For α = .05, H0 can be rejected for the first, third, and fifth comparison.

p = c(.0021, .0473, .0114, .0561, .0035, .0278)p.adjust(p, method = "holm")[1] 0.0126 0.0946 0.0456 0.0946 0.0175 0.0834

Confidence interval for η2 (or partial eta-squared)

Install the MBESS package and call the ci.R2 function. 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

Sign Test (one-group design)

library(BSDA)score = c(385, 497, 376, 405, 520, 790, 480, 530, 345, 371, 468, 501, 586, 583, 460)SIGN.test(score, md = 570, alternative = "two.sided", conf.level = 0.95)

One-sample Sign-Test

data: scores = 3, p-value = 0.03516alternative hypothesis: true median is not equal to 57095 percent confidence interval: 388.5634 528.2183sample estimates:median of x 480

Conf.Level L.E.pt U.E.ptLower Achieved CI 0.8815 405.0000 520.0000Interpolated CI 0.9500 388.5634 528.2183Upper Achieved CI 0.9648 385.0000 530.0000

Mann-Whitney Test (2-group design)

Page 38: · Web viewR Functions Part I: Sample Size for Desired Precision Function 1: Sample size to estimat e one mean sizeCImean1

D.G. Bonett (1/2018)

group1 = c(32, 39, 26, 35, 43, 27, 40, 37, 34, 29)group2 = c(36, 44, 47, 42, 49, 39, 46, 31, 33, 48)wilcox.test(group1, group2)

Wilcoxon rank sum test with continuity correction

data: group1 and group2W = 20.5, p-value = 0.02831alternative hypothesis: true location shift is not equal to 0

Warning message:In wilcox.test.default(group1, group2) : cannot compute exact p-value with ties

Kruskal-Wallis Test (single-factor between-subjects design)

score = c(12, 10, 11, 14, 15, 9, 11, 12, 13, 10, 15, 10, 12, 13, 17, 11, 16, 13, 12, 14, 17, 12, 16, 18, 14, 21, 17, 16, 17, 22, 16, 22, 19, 20, 18, 16)group = factor(c(1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3))kruskal.test(score ~ group)

Kruskal-Wallis rank sum test

data: scores by groupKruskal-Wallis chi-squared = 20.051, df = 2, p-value = 4.426e-05

Wilcoxon Signed-rank Test (paired-samples design)

y1 = c(21, 4, 9, 12, 35, 18, 10, 22, 24, 1, 6, 8, 13, 16, 19)y2 = c(67, 28, 30, 28, 52, 40, 25, 37, 44, 10, 14, 20, 28, 40, 51)wilcox.test(y1, y2, paired = T)

data: y1 and y2V = 0, p-value = 0.0007175alternative hypothesis: true location shift is not equal to 0

Warning message:In wilcox.test.default(y1, y2, paired = T) : cannot compute exact p-value with ties

Friedman Test (single-factor within-subjects design)

score = c(25, 22, 15, 20, 18, 17, 39, 34, 30, 29, 25, 22, 18, 16, 14, 22, 21, 19, 25, 22, 19, 30, 27, 26)time = factor(c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3))subject = factor(c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8))friedman.test(score ~ time|subject)

Friedman rank sum test

data: scores and time and subjectFriedman chi-squared = 16, df = 2, p-value = 0.0003355