exclude: true class: middle center hide-slide-number monash-bg-gray80 .info-box.w-50.bg-white[ These slides are viewed best by Chrome or Firefox and occasionally need to be refreshed if elements did not load properly. Please note this web version takes a while to load. See <a href="edibble.pdf">here for the PDF <i class="fas fa-file-pdf"></i></a>. ] <br> .white[Press the **right arrow** to progress to the next slide!] --- background-size: cover class: title-slide count: false # .monash-blue[The Grammar of Experimental Design] <br> <h2 style="font-weight:900!important;"></h2> .bottom_abs.width100[ Presenter: *Emi Tanaka* <i class="fas fa-university"></i> Department of Econometrics and Business Statistics, <Br> Monash University, Melbourne, Australia <i class="fas fa-envelope"></i> emi.tanaka@monash.edu <a href="https://twitter.com/statsgen" style="color:black"><i class="fab fa-twitter"></i> @statsgen</a> <i class="fas fa-calendar-alt"></i> 7 July 2021 @ useR!2021 <br> ] ??? Hi there, I'm Emi Tanaka and I'm going to be talking to you about the Grammar of Experimental Design. --- background-image: url("images/OLD_PAPER_TEXTURE.jpg") background-size: 100% count: false # Classical split-plot design **Context**: Study of 2 irrigation methods and 2 fertilizer brands on the yields of a crop. <br> <center> <img src='images/split1.png' alt="Figure shows four rectangles that respresent the wholeplots"> </center> .footnote[ <a href='https://www.freepik.com/free-photos-vectors/background'>Background vector created by boggus - www.freepik.com</a> ] ??? To begin with let's have a look at this classical split-plot design. The context of this experiment is that it's a study on the yields of a crop with two irrigation methods, either irrigated or rain-fed, and two fertilizer brands. So in order to conduct this study, the experimental resources that is available to us is this land where there are four fields, which I'm going to referring to these as the wholeplot. --- background-image: url("images/OLD_PAPER_TEXTURE.jpg") background-size: 100% count: false # Classical split-plot design **Context**: Study of two irrigation methods and two fertilizer brands on yield of a crop. <br> <center> <img src='images/split2.png' alt="The outer two wholeplots have received irrigation while the middle two wholeplots is rain-fed."> </center> .footnote[ <a href='https://www.freepik.com/free-photos-vectors/background'>Background vector created by boggus - www.freepik.com</a> ] ??? Now for the irrigation method, we have a restriction such that only one irrigation method can be applied per wholeplot. So what you see here, where we got the irrigated wholeplots on the edges and the rainfed wholeplots in the middle, is just one possible randomisation of the irrigation method on the whole plots. --- background-image: url("images/OLD_PAPER_TEXTURE.jpg") background-size: 100% # Classical split-plot design **Context**: Study of two irrigation methods and two fertilizer brands on yield of a crop. <br> <center> <img src='images/split3.png' alt="the wholeplots are split into two with 8 subplots in total"> </center> .footnote[ <a href='https://www.freepik.com/free-photos-vectors/background'>Background vector created by boggus - www.freepik.com</a> ] ??? Then what we do next is to split the wholeplot into two subplots, which means that we have 8 subplots in total. --- background-image: url("images/OLD_PAPER_TEXTURE.jpg") background-size: 100% # Classical split-plot design **Context**: Study of two irrigation methods and two fertilizer brands on yield of a crop. <br> <center> <img src='images/split4.png' alt="The first subplot receives fertilizer brand A, second subplot receives fertilizer brand B, third one is B, fourth one is A, fifth one is B, sixth one is A, seventh one is B and last one is A."> </center> .footnote[ <a href='https://www.freepik.com/free-photos-vectors/background'>Background vector created by boggus - www.freepik.com</a> ] ??? Now the fertilizer can be applied to each subplots independently. We don't want to confound the fertilizer brands to the wholeplot, so for each wholeplot, we randomly apply one fertilizer brand to one subplot and the the remaining subplot receives the other fertilizer brand. What you see here is one possible restricted randomisation of fertilizer onto subplot. --- background-image: url("images/OLD_PAPER_TEXTURE.jpg") background-size: 100% # Classical split-plot design **Context**: Study of two irrigation methods and two fertilizer brands on yield of a crop. <br> <center> <img src='images/split5.png' alt="shows the final split plot with randomisation combined from the ones described before"> </center> .footnote[ <a href='https://www.freepik.com/free-photos-vectors/background'>Background vector created by boggus - www.freepik.com</a> ] ??? Putting together the two randomisation results, here we have our classical split plot design. --- # How to .monash-blue[code] design of experiments? .flex[ .w-80.br[ From the grounds up: ```r set.seed(1) # wholeplot water <- c("irrigated", "rainfed") data.frame( wholeplot = c("W1", "W2", "W3", "W4"), water = sample(rep(water, 2)) ) # subplot brand <- c("A", "B") data.frame( subplot = paste0("S", 1:8), fert = as.vector(replicate(4, sample(brand))) ) ``` ] .w-30[ <table class="kable_wrapper"> <tbody> <tr> <td> <table class=" lightable-classic" style='font-family: "Arial Narrow", "Source Sans Pro", sans-serif; width: auto !important; margin-left: auto; margin-right: auto;'> <thead> <tr> <th style="text-align:left;"> wholeplot </th> <th style="text-align:left;"> water </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> W1 </td> <td style="text-align:left;"> irrigated </td> </tr> <tr> <td style="text-align:left;"> W2 </td> <td style="text-align:left;"> irrigated </td> </tr> <tr> <td style="text-align:left;"> W3 </td> <td style="text-align:left;"> rainfed </td> </tr> <tr> <td style="text-align:left;"> W4 </td> <td style="text-align:left;"> rainfed </td> </tr> </tbody> </table> </td> <td> <table class=" lightable-classic" style='font-family: "Arial Narrow", "Source Sans Pro", sans-serif; width: auto !important; margin-left: auto; margin-right: auto;'> <thead> <tr> <th style="text-align:left;"> subplot </th> <th style="text-align:left;"> fert </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> S1 </td> <td style="text-align:left;"> A </td> </tr> <tr> <td style="text-align:left;"> S2 </td> <td style="text-align:left;"> B </td> </tr> <tr> <td style="text-align:left;"> S3 </td> <td style="text-align:left;"> A </td> </tr> <tr> <td style="text-align:left;"> S4 </td> <td style="text-align:left;"> B </td> </tr> <tr> <td style="text-align:left;"> S5 </td> <td style="text-align:left;"> B </td> </tr> <tr> <td style="text-align:left;"> S6 </td> <td style="text-align:left;"> A </td> </tr> <tr> <td style="text-align:left;"> S7 </td> <td style="text-align:left;"> A </td> </tr> <tr> <td style="text-align:left;"> S8 </td> <td style="text-align:left;"> B </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> ] ] ??? Okay let's get to some R code. We can generate the split plot design from the grounds up just using the base R functions. Here I'm randomising the irrigation method to the wholeplot first, then I randomise the fertilizer brand to subplot. All the result are wrapped up in a data frame format so you can see the output as a table on the right. --- background-image: url("images/ivan-jevtic-B5EJm5V5gJs-unsplash.jpg") background-size: 100% # The .monash-blue[cool kid] on the block: `agricolae` ```r library(agricolae) design.split(trt1 = water, trt2 = brand, r = 2)$book ``` | plots|splots |block |water |brand | |-----:|:------|:-----|:---------|:-----| | 101|1 |1 |irrigated |A | | 101|2 |1 |irrigated |B | | 102|1 |1 |rainfed |A | | 102|2 |1 |rainfed |B | | 103|1 |2 |rainfed |B | | 103|2 |2 |rainfed |A | | 104|1 |2 |irrigated |A | | 104|2 |2 |irrigated |B | ??? Another way you can generate a split plot design is using the very popular agricolae package. There's a function called design.split where you parse the first treatment factor that is applied to whole plot and then the second treatment factor to the subplot, and the number of replicates. You can get the design layout with all the randomisation as a table format just like you see here. --- layout: true background-image: url("images/joanna-kosinska-mjC9apK53a8-unsplash.jpeg") background-size: 100% --- # The .monash-blue[new kid] on the block: `edibble` .flex[ .w-65[ ```r library(edibble) *start_design("split-plot") ``` ] .w-35[ ] ] ??? I've been working on an alternative method implemented as the edibble R package which builds the experimental design progressively by mapping experimental components to an object oriented system. So let me show how it works. First I'm indicating that I'm creating a new experimental design named split plot which creates an intermediate construct of the experimental design. --- # The .monash-blue[new kid] on the block: `edibble` .flex[ .w-65[ ```r library(edibble) start_design("split-plot") %>% * set_units(wholeplot = 4) ``` ] .w-35[ <center> <img src="images/split1.png" width="80%" alt="four wholeplots"> </center> ] ] ??? Next, in my code I explicit input that there are 4 wholeplots by using the `set_units` function. --- # The .monash-blue[new kid] on the block: `edibble` .flex[ .w-65[ ```r library(edibble) start_design("split-plot") %>% set_units(wholeplot = 4) %>% * set_units(subplot = nested_in(wholeplot, 2)) ``` ] .w-35[ <center> <img src="images/split-str.png" width="80%" alt="four wholeplots with two subplots each"> </center> ] ] ??? Next I split each wholeplot into two subplots and I do this by set_units and nested_in and the code now reads that there is a unit called subplot and it is nested in wholplot with 2 subplots per wholeplot. --- # The .monash-blue[new kid] on the block: `edibble` .flex[ .w-65[ ```r library(edibble) start_design("split-plot") %>% * set_units(wholeplot = 4, * subplot = nested_in(wholeplot, 2)) ``` ] .w-35[ <center> <img src="images/split-str.png" width="80%" alt="the experimental structure with 4 wholeplots and 2 subplots each"> </center> ] ] ??? I don't have to have a separate set_units function, it can all be within one set_units function and this will still be the same result. --- # The .monash-blue[new kid] on the block: `edibble` .flex[ .w-65[ ```r library(edibble) start_design("split-plot") %>% set_units(wholeplot = 4, subplot = nested_in(wholeplot, 2)) %>% * set_trts(water = c("irrigated", "rainfed"), * fertilizer = c("A", "B"))) ``` ] .w-35[ <center> <img src="images/split-trt.png" width="80%" alt="two treatment factors: irrigation methods and fertilizer brands"> </center> ] ] ??? Next I'm defining the treatments, there are two factors which is the irrigation method and fertilizer brand. --- # The .monash-blue[new kid] on the block: `edibble` .flex[ .w-65[ ```r library(edibble) start_design("split-plot") %>% set_units(wholeplot = 4, subplot = nested_in(wholeplot, 2)) %>% set_trts(water = c("irrigated", "rainfed"), fertilizer = c("A", "B"))) %>% * allocate_trts(water ~ wholeplot, * fertilizer ~ subplot) ``` ] .w-35[ <center> <img src="images/split-alloc.png" width="80%" alt="treatment factor pointing to a particular unit to show allocation"> </center> ] ] ??? Then I define my intention to allocate which treatments to which units. So you see here water, or the irrigation method, is allocated to wholeplots and fertilizer brands to subplot. --- # The .monash-blue[new kid] on the block: `edibble` .flex[ .w-65[ ```r library(edibble) start_design("split-plot") %>% set_units(wholeplot = 4, subplot = nested_in(wholeplot, 2)) %>% set_trts(water = c("irrigated", "rainfed"), fertilizer = c("A", "B"))) %>% allocate_trts(water ~ wholeplot, fertilizer ~ subplot) %>% * randomise_trts() ``` ] .w-35[ <center> <img src="images/split-edibble.png" width="80%" alt="the final randomisation from edibble R-package"> </center> ] ] ??? Then we randomise the treatments to the units. The benefit of this approach is that you can mix and match any steps you like if you don't like the original implementation. --- # The .monash-blue[new kid] on the block: `edibble` .flex[ .w-65[ ```r library(edibble) start_design("split-plot") %>% set_units(wholeplot = 4, subplot = nested_in(wholeplot, 2)) %>% set_trts(water = c("irrigated", "rainfed"), fertilizer = c("A", "B")) %>% allocate_trts(water ~ wholeplot, fertilizer ~ subplot) %>% randomise_trts() %>% * serve_table() ``` ] .w-35[ |wholeplot |subplot |fertilizer |water | |:----------|:--------|:----------|:---------| |wholeplot1 |subplot1 |B |rainfed | |wholeplot1 |subplot2 |A |rainfed | |wholeplot2 |subplot3 |B |irrigated | |wholeplot2 |subplot4 |A |irrigated | |wholeplot3 |subplot5 |A |irrigated | |wholeplot3 |subplot6 |B |irrigated | |wholeplot4 |subplot7 |B |rainfed | |wholeplot4 |subplot8 |A |rainfed | ] ] ??? Now finally you can signal that you're done constructing the design and get the output as a data frame using the serve_table command. --- class: transition <h1 id="shadowBox" class="rainbow rainbow_text_animated">Grammar of <br>Experimental Designs</h1> .gray.dance[Coming soon to an experiment near you] https://github.com/emitanaka/edibble ??? That was a quick overview of the edibble R-package which implements what I call the grammar of experimental design. You can find the package and more information on my github account at github.com/emitanaka/edibble.