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”).

Setting up your project

Open RStudio and create a new R Project.

Adding some text

With any report, we want to start by making clear it’s purpose.

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.

Adding an image

Add the image file “big_bear.png” with a caption. To add an image, you type ![caption](file location) on a new line.

Figure 1: A beautiful bear
Figure 1: A beautiful bear

Add a horizontal rule

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.


About R chunks

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.

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.

Your first R chunk

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).

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.

Initial graphical analysis

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.

## # 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

Figure 2. Mean and Standard Deviations for Bear Weights Pre and Post Hibernation

Checking assumptions

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

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.

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

Figure 3. Frequency Distribution for the Weight Difference

Running the Paired T-test

You’re finally at the point where you can statistically test whether there is a significant difference in bear weight before and after hibernation.

## 
##  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

Submit your work

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.