Last active
August 16, 2017 07:07
-
-
Save MartinMacharia/40b39065ed95d26bdc13d7efae48d8db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Guide to Generating field book and field layouts in R | |
January 2014 | |
What is R? | |
R is a programming language for statistical computing and graphics. It is available as free software. As a thriving open-source project, R is supported by a large community of users and developers worldwide. Whether you're using R for data management, analyzing genomic sequences, or designing field books and layouts for agronomy work, experts in every domain have made resources, applications and code available for free, online. | |
Why we need R in OFRA | |
R presents a quick way to randomize treatments and generate field books and field layouts. Beyond this R is the leading tool for statistics, data analysis, and machine learning. It is more than a statistical package; it’s a programming language, so you can create your own objects, functions, and packages. | |
What is a package? | |
Peer reviewed methods in statistics and statistical modeling from lead researchers in statistics that have been packaged into applications and can be downloaded for free and run in R. The repository for such applications is a cloud on the internet commonly known as a CRAN. The gateway to a cloud or CRAN is called a CRAN mirror. | |
What is CRAN? | |
Comprehensive R Archive Network (CRAN) is a network of servers around the world that store identical, up-to-date, versions of code and documentation for R. It is advisable to use a CRAN mirror nearest to you to minimize network load. | |
How to obtain R | |
The current version of R can be downloaded and installed using the link; http://cran.r-project.org/ . The program can be installed directly from the internet or saved and ran from the computer. When installing, you can probably just accept the default settings. | |
The link above provides R versions depending on the computer operating system you are using; Windows, Linux/Unix or Mac. | |
Getting started with R | |
Usually after downloading R, a shortcut to the program (as a blue R icon) is created on the desktop. | |
Double click on the icon to launch R. You should see an interface similar to the one below; this is the R console where you can type your commands at the prompt (red by default). | |
R is a command line driven program. The user enters commands at the prompt. Each command is executed one at a time by pressing enter on the keyboard. Alternatively, clicking file at the top left corner and as show below will enable the user to create an R script where commands can be written and executed (file>new script). | |
We will use the R script for our experimental design; randomize treatments and create a field book and field layout. | |
Obtain experimental design package | |
In order to design a field book and a field layout in R, we require the package “agricolae” which does not come with the standard R installation. We have to download it from the R repository of packages called CRAN. We therefore require an internet connection. Copy and paste (or type) the following command in the R script window; install.packages(“agricolae”) , and execute the command by pressing control+r to initialize the downloading process, see illustration below. | |
Note: R commands are case sensitive, type the commands as presented | |
After executing the command another window will appear prompting you to select a CRAN mirror. A CRAN mirror is a gateway to a server that stores the package. Well, it is said UK (Bristol) is faster when downloading packages from Africa but you may try other CRAN Mirror. | |
Select a CLAN mirror, then select OK | |
After selecting a CRAN mirror, the package will automatically start downloading and progress can be monitored as shown below. | |
More about “Agricolae” | |
“Agricolae” offers extensive functionality on experimental design especially for agricultural and plant breeding experiments. It supports planning of lattice, Alpha, Cyclic, Complete Block, Latin Square, Graeco-Latin Squares, augmented block, factorial, split and strip plot designs. It also offers various analytic facilities for experimental data, e.g. treatment comparison procedures and several non-parametric tests comparison, biodiversity indexes and consensus cluster. | |
Design a field book and field layout | |
Load the package | |
Once the package “agricolea” has been downloaded, it is automatically saved in drive C (Somewhere). It must to be loaded in the current R session in order to be useful (Sometimes statisticians say they are calling the package). Copy paste or type the following command; require(agricolae) on the R script and execute by pressing ctrl+r concurrently on the keyboard to load the package, see illustration below. | |
Specify the treatment structure in R | |
Let’s have a look at a generic (subject to change) treatment structure for maize, sorghum, rice, finger millet and cassava as presented in the OFRA protocol; | |
Maize, sorghum, rice, finger millet, cassava† | |
0P0K 15P,0K 90N,0K 90N,15P Diagnostic | |
0N 0N 7.5P 10K‡ 90N,15P,20K,15S,2.5Zn,10Mg,0.5B | |
30N 30N 20K | |
60N 60N 22.5P 30K | |
90N 90N | |
120N 120N | |
The above 16 treatment combinations can be specified in R as shown below; | |
treatments=c("0N0P0K","30N0P0K","60N0POK","90N0P0K","120N0P0K","0N15P0K","30N15P0K","60N15P0K","90N15P0K","120N15P0K","90N7.5P0K","90N22.5P0K","90N15P10K","90N15P20K","90N15P30K","Diagnostic") | |
A breakdown of the code/command; | |
• treatment is an object that we have specified to carry the 16 treatments | |
• “=” is the assignment parameter that tells R that treatment is a column vector with 16 treatments | |
• “c” tells R to create a row vector of the 16 treatments | |
• …..the rest specifies the treatments enclosed in “” and separated by , | |
Generate the field book | |
The command below is use to generate a field book in R by copy pasting or typing it in the R script. | |
Fieldbook=design.rcbd(treatments,3,serie=2, 34589,"Super-Duper", first=TRUE) | |
Fieldbook | |
Before viewing the output on the R console, let’s breakdown the command; | |
• Fieldbook is our object name | |
• design.rcbd tells R that we want a randomized complete block design | |
• treatments is a row vector of our treatments that we had previously created | |
• 3 represents the number of blocks in our design, this number can be adjusted depending on the number of blocks in the trial | |
• serie=2 is the plot numbering, we therefore expect our plot numbering to start at 101 hence series=2. If it was series=1 our plot numbering would have been 1, 2 etc, and if it was series=3 then it would have been 1001, 1002 etc. | |
• 34589 is a randomization seed; usually changing this produces another randomization sequence of the treatments. The choice of seed is arbitrary; you can try any non-zero number. | |
• Super-Duper is a randomization method, others to consider are; Wichmann-Hill, Marsaglia-Multicarry, Mersenne-Twister, Knuth-TAOCP, user-supplied, Knuth-TAOCP-2002, default | |
• first is either true or false, true indicates whether the first block should be randomized, true is the ideal situation. | |
Now let us execute the command and look at our output, to execute highlight the command as previously illustrated and use the ctrl+r keys on the keyboard. | |
Transfer the field book to an excel workbook | |
Click on the left button of the mouse and drag the mouse (simultaneously) over the region containing the field book on the R console | |
Once the entire field book is highlighted (default of highlight is blue), click on the right button of the mouse to display a dialogue box as shown below. Move the mouse so that the cursor rests on copy in the dialogue box and right click on the mouse button. | |
Open a blank excel workbook, on the right hand side of the excel sheet, click on the paste button to reveal a dialogue box. On the dialogue box, select paste. This will paste the field book from R to your excel work sheet; a combo box will appear at the bottom of the excel sheet after pasting the field book, see below illustration of the combo box. | |
Right clicking on the combo box produces a dialogue box with the options of pasting to “match destination formatting” or “use text import wizard”. Using the text import wizard will tidy our field book. Therefore select that option. | |
Selecting the “Use Text Import Wizard” will open a dialogue box similar to what is shown below; | |
Below is a section of the field book as viewed in excel | |
Create the field layout | |
Now that we have a field book, let’s create a field layout. The command below will do precisely that. The objects in the command that need to be defined and which we have previously defined are; Fieldbook and treatments, recall these commands? | |
The rest of the command is standard R programming language. Type or copy past the command as presented; | |
book=outdesign$book | |
write.table(book,"rcbd.txt", row.names=FALSE, sep="\t") | |
file.show("rcbd.txt") | |
fieldbook <- zigzag(outdesign) | |
fieldbook | |
print(t(matrix(fieldbook[,1],16,3))) | |
print(t(matrix(fieldbook[,3],16,3))) | |
Two field layouts are generated; | |
The first (top) with the plot numbers that correspond to the treatments (each number has three integers – the first integer on the left refers to the block number, the other two refer to the plot number within the block). The second with actual treatments to aid in tracing the treatments to plot numbers. See R output below; | |
Transfer the field layout to excel | |
To transfer the layout to excel, first we will need to highlight the layout in R by clicking on the left mouse button and move the mouse over the field layout region on the R console simultaneously, see below; | |
After highlighting as shown above, click the right hand side button of the mouse to display a dialogue box as shown below; | |
Open a blank excel workbook and on the right hand side of the excel sheet, click on the paste button to reveal a dialogue box. On the dialogue box, select paste. This will paste the field layout from R to your excel work sheet; a combo box will appear at the bottom of the excel sheet after pasting the field layout. Right clicking on the combo box reveals a dialogue box with the options of pasting to “match destination formatting” or “use text import wizard”. Using the text import wizard will tidy our field layout, select that option. See steps below; | |
Step 1 Step 2 | |
Step 3 | |
See output of the 3 steps below; | |
The table below has been produced after reorganizing table above | |
By following step 1 to 3 above, we can generate another table in excel with the randomized treatments corresponding to plot numbers above, see below; | |
These tables can be printed and used in the field | |
Reference | |
Elipe de Mendiburu (2013). agricolae: Statistical Procedures for Agricultural Research. R package version 1.1-6. http://CRAN.R-project.org/package=agricolae. | |
Alternative script for generating the field book and layout | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment