Built-in Estimators: Difference-in-means

difference_in_means(formula, blocks = NULL, clusters = NULL, condition1 = NULL, condition2 = NULL, data, weights = NULL, subset = NULL, alpha = 0.05)

formula | An object of class "formula", such as Y ~ Z |
---|---|

blocks | An optional bare (unquoted) name of the block variable. Use for blocked designs only. |

clusters | An optional bare (unquoted) name of the variable that corresponds to the clusters in the data; used for cluster randomized designs. For blocked designs, clusters must be within blocks. |

condition1 | names of the conditions to be compared. Effects are estimated with condition1 as control and condition2 as treatment. If unspecified, condition1 is the "first" condition and condition2 is the "second" according to r defaults. |

condition2 | names of the conditions to be compared. Effects are estimated with condition1 as control and condition2 as treatment. If unspecified, condition1 is the "first" condition and condition2 is the "second" according to r defaults. |

data | A data.frame. |

weights | An optional bare (unquoted) name of the weights variable. |

subset | An optional bare (unquoted) expression specifying a subset of observations to be used. |

alpha | The significance level, 0.05 by default. |

This function implements difference-in-means estimation, with and without blocking. Standard errors are estimated as the square root of the sum of the within-group variances, divided by their respective sample sizes (Equation 3.6 in Gerber and Green 2012). If blocked, the difference in means estimate is taken in each block, then averaged together according to block size.

df <- data.frame(Y = rnorm(100), Z = sample(1:3, 100, replace = TRUE), block = sample(c("A", "B", "C"), 100, replace = TRUE)) difference_in_means(Y ~ Z, data = df)#> coefficient_name est se p ci_lower ci_upper df #> 1 Z 0.1224875 0.2674282 0.6485812 -0.4123728 0.6573477 60.41608difference_in_means(Y ~ Z, condition1 = 3, condition2 = 2, data = df)#> coefficient_name est se p ci_lower ci_upper df #> 1 Z 0.2770966 0.2432306 0.2588673 -0.208847 0.7630401 63.76421difference_in_means(Y ~ Z, blocks = block, data = df)#> coefficient_name est se p ci_lower ci_upper df #> 1 Z 0.02735372 0.2960366 0.926571 -0.560197 0.6149045 97difference_in_means(Y ~ Z, blocks = block, condition1 = 3, condition2 = 2, data = df)#> coefficient_name est se p ci_lower ci_upper df #> 1 Z 0.2107775 0.2830092 0.4582099 -0.3509175 0.7724725 97