-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdoBootstrapReadme.R
80 lines (65 loc) · 3.35 KB
/
doBootstrapReadme.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
71
72
73
74
75
76
# Bootstrap scripts written by Desmond Ong (dco@stanford)
# Last updated: Aug 20, 2014
# This is the Readme File.
source("doBootstrap.r")
# doBoot is the basic function that calculates descriptive statistics
#
# results <- doBoot(x, y=NULL, mediator=NULL, whichTest = "mean", numberOfIterations = 5000,
# confidenceInterval=.95, na.rm=TRUE)
#
# Output: Results
#
# Input:
# x = first data vector. Feed in a VECTOR!
# y = second data vector (not necessary if you're doing statistics on one data vector only, e.g. mean)
# mediator = mediator data vector (if you want to do mediation)
# whichTest = the test you want to do, as a string.
# Currently the tests supported are:
# "mean" (single vector test)
# "correlation" (correlation between two vectors, paired)
# "difference, unpaired" (difference between two vectors, unpaired)
# "difference, paired" (difference between two vectors, paired)
# "cohen, unpaired" (basically unpaired difference / pooled standard dev) * not bias corrected nor accelerated
# "cohen, paired" (basically paired difference / standard deviation) * not bias corrected nor accelerated
# "mediation" (calculates a x-->y, x-->med-->y mediation, using Benoit's bm.bootmed code)
# basically just feeds into Benoit's code for now.
# "custom function": allows you to supply your own function, such that the desired statistic
# is customFunction(x) or customFunction(x,y) if y is supplied.
# Warning: doBoot doesn't test if you have supplied the correct number of arguments. It'll just try to call it.
#
#
# numberOfIterations = number of bootstrapping iterations. Default is 5000
# confidenceInterval = specifies the percentile of the CI. Default is .95
# na.rm = remove NAs?
#
# using the mtcars dataset
head(mtcars)
resultsMean <- doBoot(mtcars$mpg)
resultsMean <- doBoot(mtcars$mpg, whichTest = "mean")
resultsCorrelation <- doBoot(mtcars$disp,mtcars$hp, whichTest = "correlation")
resultsDifference <- doBoot(mtcars$disp,mtcars$hp, whichTest = "difference, unpaired")
resultsDifference <- doBoot(mtcars$disp,mtcars$hp, whichTest = "difference, paired")
resultsCohen <- doBoot(mtcars$disp,mtcars$hp, whichTest = "cohen, unpaired")
resultsCohen <- doBoot(mtcars$disp,mtcars$hp, whichTest = "cohen, paired")
# just using Benoit's code off the bat...
resultsMediation <- doBoot(mtcars$disp,mtcars$hp, mtcars$cyl, whichTest = "mediation")
# # doBootRegression is the function you call if you want to bootstrap regressions.
#
# results <- doBootRegression(dataset, formula, mixedEffects = FALSE, numberOfIterations = 5000,
# confidenceInterval=.95, na.rm=TRUE)
#
# Output: Results
#
# Input:
# dataset = input dataset
# formula = the regression formula of interest. For e.g., "y ~ x" or "y ~ x + (1|z)"
# mixedEffects = whether your formula has mixed effects. Default FALSE.
# If true, will use lmer, otherwise, lm.
# For lmer, will output only fixed effect coefficients
# For lm, will output coefficients, and R^2
# numberOfIterations = number of bootstrapping iterations. Default is 5000
# confidenceInterval = specifies the percentile of the CI. Default is .95
# na.rm = remove NAs?
#
resultsRegression <- doBootRegression(mtcars, disp~hp)
resultsRegression <- doBootRegression(mtcars, disp~hp+(1|cyl), mixedEffects = TRUE)