Learn R Programming

userfriendlyscience (version 0.1-2)

meanDiff: meanDiff

Description

The meanDiff function compares the means between two groups. It computes Cohen's d, the unbiased estimate of Cohen's d (Hedges' g), and performs a t-test. It also shows the achieved power, and, more usefully, the power to detect small, medium, and large effects.

Usage

meanDiff(x, y=NULL, paired = FALSE, var.equal = "test",
         conf.level = .95, digits = 2, envir = parent.frame())

Arguments

x
Dichotomous factor: variable 1; can also be a formula of the form y ~ x, where x must be a factor with two levels (i.e. dichotomous).
y
Numeric vector: variable 2; can be empty if x is a formula.
paired
Boolean; are x & y independent or dependent? Note that if x & y are dependent, they need to have the same length.
var.equal
String; only relevant if x & y are independent; can be "test" (default; test whether x & y have different variances), "no" (assume x & y have different variances; see the Warning below!), or "yes" (assume x & y have the same variance)
conf.level
Confidence of confidence intervals you want.
digits
With what precision you want the results to print.
envir
The environment where to search for the variables (useful when calling meanDiff from a function where the vectors are defined in that functions environment).

Value

  • An object is returned with the following elements:
  • variablesInput variables
  • groupsLevels of the x variable, the dichotomous factor
  • ci.confidenceConfidence of confidence intervals
  • digitsNumber of digits for output
  • xValues of dependent variable in first group
  • yValues of dependent variable in second group
  • typeType of t-test (independent or dependent, equal variances or not)
  • nSample sizes of the two groups
  • meanMeans of the two groups
  • sdStandard deviations of the two groups
  • objectsObjects used; the t-test and optionally the test for equal variances
  • varianceVariance of the difference score
  • meanDiffDifference between the means
  • meanDiff.dCohen's d
  • meanDiff.d.varVariance of Cohen's d
  • meanDiff.d.seStandard error of Cohen's d
  • meanDiff.JCorrection for Cohen's d to get to the unbiased Hedges g
  • powerAchieved power with current effect size and sample size
  • power.smallPower to detect small effects with current sample size
  • power.mediumPower to detect medium effects with current sample size
  • power.largelPower to detect large effects with current sample size
  • meanDiff.gHedges' g
  • meanDiff.g.varVariance of Hedges' g
  • meanDiff.g.seStandard error of Hedges' g
  • ci.usedZZ value used to compute confidence intervals
  • meanDiff.d.ci.lowerLower bound of confidence interval around Cohen's d
  • meanDiff.d.ci.upperUpper bound of confidence interval around Cohen's d
  • meanDiff.g.ci.lowerLower bound of confidence interval around Hedges' g
  • meanDiff.g.ci.upperUpper bound of confidence interval around Hedges' g
  • meanDiff.ci.lowerLower bound of confidence interval around raw mean
  • meanDiff.ci.upperUpper bound of confidence interval around raw mean
  • tStudent t value for Null Hypothesis Significance Testing
  • dfDegrees of freedom for t value
  • pp-value corresponding to t value

Warning

Note that when different variances are assumed for the t-test (i.e. the null-hypothesis test), the values of Cohen's d are still based on the assumption that the variance is equal. In this case, the confidence interval might, for example, not contain zero even though the NHST has a non-significant p-value (the reverse can probably happen, too).

Details

This function uses the formulae from Borenstein, Hedges, Higgins & Rothstein (2009) (pages 25-32).

References

Borenstein, M., Hedges, L. V., Higgins, J. P., & Rothstein, H. R. (2011). Introduction to meta-analysis. John Wiley & Sons.

Examples

Run this code
### Create simple dataset
dat <- PlantGrowth[1:20,];
### Remove third level from group factor
dat$group <- factor(dat$group);
### Compute mean difference and show it
meanDiff(dat$weight ~ dat$group);

### Look at second treatment
dat <- rbind(PlantGrowth[1:10,], PlantGrowth[21:30,]);
### Remove third level from group factor
dat$group <- factor(dat$group);
### Compute mean difference and show it
meanDiff(x=dat$group, y=dat$weight);

Run the code above in your browser using DataLab