Get startedGet started for free

Randomized block design: controlling variance

1. Randomized block design: controlling variance

Next, we'll delve further into the concept of blocking in experimental design.

2. Understanding blocking

Blocking involves grouping experimental units, often with similar characteristics, to minimize variance within these groups. This ensures that each block, representing a specific level of the blocking factor, receives every treatment. This approach allows us to concentrate on the treatment effects while controlling for variance attributable to the blocking factor, thus improving the precision of our results.

3. Block design data example

For this athlete performance DataFrame of 200 rows, blocking is represented by Initial_Fitness_Level with categories of Beginner, Intermediate, and Advanced. Muscle_Gain_kg is a numeric response variable measured on participants for the year prior to blocks being assigned.

4. Implementing randomized block design

To implement a randomized block design, we'll group the rows into blocks based on the Initial_Fitness_Level in this case, shuffle the rows within these blocks, and randomly assign a treatment. To shuffle the rows in each Initial_Fitness_Level block, we start with .groupby() on Initial_Fitness_Level. To shuffle each row in that block, we chain the .apply() method to the groupby, and pass it a lambda function that reads: for each group, denoted by x, we sample all rows with frac=1, effectively shuffling them. We reset the index to not have both an index and column called Block. The grouped data is ordered alphabetically by fitness level.

5. Implemented randomized blocks

Then, within each block, we assign exercise program treatments randomly using numpy.random.choice(). This method allows us to control for block effects while focusing on the differences caused by the treatments. Here is a sample of the implemented randomized block DataFrame with the treatment randomly applied within each block.

6. Visualizing treatment effects within blocks

A boxplot is an effective tool for visualizing the distribution of treatment effects across different blocks. By plotting the Muscle_Gain_kg variable versus the Initial_Fitness_Level, coloring by Treatment, we observe the central tendencies and variabilities within each block. Scanning this boxplot, we see similar median values throughout the blocks and treatments. The variability is a bit wider for some, though, such as Cardio for Advanced and Beginner.

7. ANOVA within blocks

We can use ANOVA to statistically check for these differences. Let's set a significance level at 5% prior to reviewing our results. We group the DataFrame by the blocking column and then apply a lambda function to each group. Within the lambda function, we perform a one-way ANOVA test between the Muscle_Gain_kg values for each treatment within each block using f_oneway from scipy.stats. Finally, it returns the F-statistic and p-value for each block's ANOVA test. Each of the p-values are above the alpha significance level of 5%. This gives evidence that significant differences don't exist across treatments within blocks. This is an ideal goal when setting up randomized block design experiments.

8. Visualizing effects across blocks

We can also look for differences in the outcome across randomized blocks. Here, we do not break down further by treatment. These boxplots look similar, so we might guess that none of the blocks has a significantly different mean outcome compared to the others.

9. ANOVA between blocks

Next we compute the one-way ANOVA test across the blocks. It compares the Muscle_Gain_kg values for each block separately to assess whether there are significant differences in means among the blocks. The function f_oneway calculates the F-statistic and associated p-value, indicating the likelihood of observing the data if the null hypothesis of equal means across all blocks is true. A p-value greater than 0.05 supports what we saw with the boxplot - that there is no significant difference.

10. Let's practice!

Time to test your knowledge of randomized blocks!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.