SCRIPT TO TEST ASSIGNMENT 3 FUNCTIONS.

Set up a test data frame.

> dt <- data.frame (list (treatment=c("drug","drug","placebo","placebo"),
+                         A=c(6,2,4,0), B=c(1,9,3,5)))

Find effects and p-values for this test data, using a large number of permutations.

> set.seed(1)
> print_effects_with_pvalues(dt,c("A","B"),nperm=10000)
A      B
effect 2.0000 1.0000
pvalue 0.3349 0.5035

Number of subjects: 4

The correct effects are 2 for A and 1 for B, since the means for the drug group are 4 for A and 5 for B, while the means for the placebo group are 2 for A and 4 for B. The correct pvalues are 1/3 for a, ½ for b. To see this, consider the six possible results of permuting the treatment variable (D=drug, P=placebo,):

> #     D D P P   A effect: 4-2 = 2   B effect: 5-4 = 1
> #     D P D P   A effect: 5-1 = 4   B effect: 2-7 = -5
> #     D P P D   A effect: 3-3 = 0   B effect: 3-6 = -3
> #     P D D P   A effect: 3-3 = 0   B effect: 6-3 = 3
> #     P D P D   A effect: 1-5 = -4  B effect: 7-2 = 5
> #     P P D D   A effect: 2-4 = -2  B effect: 4-5 = -1

Of these, 2 out of 6 have A effect of at least 2, and 3 out of 6 have B effect of at least 1, giving the p-values of 2/6 = 1/3 and 3/6 = ½.