For lab 9, we will be presenting the results from an analysis in the form of an R Markdown file. Markdown is a format for making documents that combines written text and code, a very useful tool given the awkwardness of presenting work done in R. Markdown files can be converted into webpages, word documents, PDFs, slides, and more. The document you are reading now is an R Markdown File.
Take a look at the R Markdown Gallery to see many of the options that are available. Also, check out this useful RMarkdown cheatsheet.
If you have not done so already, please also install the packages
(“rmarkdown”) and (“knitr”).
Open RStudio and create a new R Project.
Download the following files: bear_data.csv and big_bear.png. If the links do not work, you can find them in the Canvas/Syllabus page next to the Lab 9 link.
Create a new R Markdown file. This requires a similar approach to creating a new R Script, but select the R Markdown option instead. When you create the new R markdown file, make sure to provide a title, add your name, and select HTML.
Clear the R Markdown file by deleting all code below line 7. This material is informative and can be utilized to create a report but for the sake of the lab it will be easier to remove it.
With any report, we want to start by making clear it’s purpose.
# Research Question.The # symbol will identify the line as a header and will
increase font size. Likewise, if you want to create a subheader, use
##, which will result in smaller text. Remember that there
needs to be a space between # and your text to make it work.
You don’t need the # because this is the body text. Also, remember to keep your text clean by adding a blank line between headers, body text, paragraphs, etc.
Add the image file “big_bear.png” with a caption. To add an image,
you type  on a new line.
The caption can be whatever you want. Since this is your first figure in the report, you can replace the word [caption] with [Figure 1: A beautiful bear].
The file location tells R where to find the image and must end in the image name with the file type. The easiest option is to have the image in your R Project folder. If it is in the R Project folder, you can jusy write the file’s name. In this case, you would only need to write (big_bear.png) as the file location.
On a new line, type *** to create a horizontal rule, a
line that spans the width of the document. This is useful for visually
separating different elements of your document. The text should change
color when it works.
Insert an R Chunk, adjust it to include a caption, and hide the R code and messages in the report. A R chunk is a space within the R Markdown file that operates by the same rules as R and allows you to run R code.
```{r} #YOUR CODE ```.To avoid confusion, write all your R code for charts, tests, etc., in a separate R script (just as you have done before). Then, test it to ensure it works in that R script before you copy and paste the working code into the chunks in the R Markdown.
Before you can create figures and run test, you first need to load your packages and data. First create a new R chuck by holding CTRL + ALT + I (or CMD + OPT + I on a Mac).
Load the libraries dplyr and ggplot2,
which you will use to manipulated and plot the data, just has we have
done in past labs.
Load the “bear_data.csv” file and give it a name like
bear.
There are three variables in the dataset. The first,
X, is the number of the observation. The second,
hibernation, states whether the observation was taken
before or after the period of hibernation. It should be a factor. The
third, weight_kg, has the weights of the bears in
kilograms.
You can set options about how an R chunk should behave which you
place in the curly brackets in the first part of the chunk. Use the
option echo = FALSE to NOT display the R code and
message = FALSE to NOT display R messages, such as when you
load packages and don’t need to show all the messages that usually
appear in the console.
```{r, echo=FALSE, message=FALSE}. There are many more
options options you can adjust, just as fig.cap, which
we’ll talk about next.As always, we want to visually plot the data before running any statistical tests. Let’s create a bar graph with error bars to compare the bear weight before and after hibernation.
Add a new header “Initial Graphical Analysis” using # then add R code to a new R Chunk that creates a bar graph comparing Pre and Post Hibernation mean bear weight.
Check that your code works by clicking the green triangle on the right of the chunk. If all your files are in the R Project file, it should work just fine. As always, be sure the names of the files and the names in the code match.
You can add a caption to any figures produced in the R Chunk by
using the text fig.cap=' ' in the R Chuck options. It
should look like
```{r, echo=FALSE, message=FALSE, fig.cap=''}. The caption
can be any text you want but should describe what you see in the figure.
For example, since this is the second figure in our document, you might
use:
fig.cap='Figure 2: Bear weight pre and post hibernation (kg).'
## # A tibble: 2 × 3
## hibernation mean sd
## <fct> <dbl> <dbl>
## 1 Before 217. 59.3
## 2 Post 168. 58.7
Figure 2. Mean and Standard Deviations for Bear Weights Pre and Post Hibernation
Before we run a Paired T-test (we are measuring the same bears before and post hibernation), check your data for assumption of normality. For the Paired T-test, the null hypothesis is that there is no difference between the groups, or the difference between the groups is zero (H0 = 0). Because the test is on the difference (one group), we will check the normality assumption on the difference, but there is no need to check for equal variances between groups (as we no longer have two groups). Below is the code you would need to use to calculate differences:
#filter by group
before <- filter(bear, hibernation == "Before")
post <- filter(bear, hibernation == "Post")
#select only weight_kg variable
before <- before %>% select(weight_kg)
post <- post %>% select(weight_kg)
#calculate the difference between the groups
difference <- before - post
Add header saying “Checking Assumptions of T-Test” using
#.
Add a sub-header saying “Distribution of Weight Difference” using
##.
Create a new R Chunk and add in the code to create a histogram showing the distribution of weight differences.
Note that because you already loaded the packages and csv file in the first R Chunk you do not need to do so again. Click the green triangle to check that your code works.
Add a caption to your histogram starting with ‘Figure 3’ and followed by a brief description.
Type in a sentence or two interpreting the histogram. Be sure it
is separate text on a new line that is not within the R Chunk. Be sure
to address if the histogram show the data meets the assumption of
normality. If you are unsure, you are encouraged to run a normality test
using shapiro.test(difference$weight_kg)
shapiro.test(difference$weight_kg)
##
## Shapiro-Wilk normality test
##
## data: difference$weight_kg
## W = 0.94665, p-value = 0.3191
Figure 3. Frequency Distribution for the Weight Difference
You’re finally at the point where you can statistically test whether there is a significant difference in bear weight before and after hibernation.
Add a header saying “Performing a Paired T-Test” using
#.
Create a new R Chunk and copy-paste in the code to perform a
t-test comparing bear weight pre and post hibernation:
t.test(x ~ y, data = bear, paired = TRUE)
Type in a sentence or two, interpreting the results of the paired t-test making sure to describe the test results (e.g., test statistic, df, p-value).
##
## Paired t-test
##
## data: weight_kg by hibernation
## t = 50.084, df = 19, p-value < 2.2e-16
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## 47.23973 51.36027
## sample estimates:
## mean difference
## 49.3
Once you have completed the previous tasks, it is time to turn in your work. Be sure everything works. Submit a compressed folder to Canvas with the following files:
Note that you don’t need to submit your R script if you used one to build your analysis. Your R Markdown file should include everything needed in the R chunks to run your analysis.