Final Project

Lucy D’Agostino McGowan

Agenda

  • Updating your yaml
  • Putting all code in the appendix
  • Hiding messages and warnings from your package loadings
  • Hiding interactive output
  • Creating pretty tables
  • Creating figure and table caption
  • Putting intermediate figures at the end
  • Including citations

Application Exercise

Copy starter files from:

https://github.com/sta-112-f23/final-project.git

Updating your yaml

  • For the final project I want you to create .pdf files.
---
title: "The title of your document"
author: "Your name"
format: pdf 
---

Updating your yaml

  • For the final project I want you to create .pdf files.
---
title: "The title of your document"
author: "Your name"
format: 
  pdf: 
    fontsize: 12pt 
---
  • 12 pt font

Updating your yaml

  • For the final project I want you to create .pdf files.
---
title: "Title"
author: "Authors Names"
format: 
  pdf:
    fontsize: 12pt
editor: visual
execute: 
  echo: false
  message: false
  warning: false
bibliography: citations.bib
abstract: |
  This is an abstract.
---
  • 12 pt font
  • Hide code
  • Add citations
  • Add abstract (150 words)

Putting all code in the Appendix

  • For these fancy reports, we want to see the R code, but we don’t want it interspersed throughout the document.
  • You can “hide” all of your chunks with the execute: echo: false in your yaml
  • THEN at the end of you document (in the Appendix) you can add
```{r ref.label=knitr::all_labels()}
#| echo: true
#| eval: false
```

Hiding interactive output

  • If you are running “interactive code”, that is code that is not saved to an object in R, it will print in your output, even if you hide the code with echo: false. For example:
glimpse(iris)
Rows: 150
Columns: 5
$ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.…
$ Sepal.Width  <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.…
$ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.…
$ Petal.Width  <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.…
$ Species      <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s…
  • Use the results: "hide" in the chunk options to hide it.
```{r}
results: "hide"
glimpse(iris)
```

Creating pretty tables

  • We’ve been using the summary() function to print tables of our model output.
  • These kind of look like “code” in our final reports

Creating pretty tables

  • We can make these prettier with the gtsummary package
  • Load the gtsummary package with library(gtsummary)
  • Use the tbl_regression() function to output a pretty table
library(gtsummary) 
lm(wt ~ am, data = mtcars) |>
  tbl_regression() 
Characteristic Beta 95% CI1 p-value
am -1.4 -1.9, -0.83 <0.001
1 CI = Confidence Interval

Creating Overall Table

tbl_summary(mtcars)
Characteristic N = 321
mpg 19.2 (15.4, 22.8)
cyl
    4 11 (34%)
    6 7 (22%)
    8 14 (44%)
disp 196 (121, 326)
hp 123 (97, 180)
drat 3.70 (3.08, 3.92)
wt 3.33 (2.58, 3.61)
qsec 17.71 (16.89, 18.90)
vs 14 (44%)
am 13 (41%)
gear
    3 15 (47%)
    4 12 (38%)
    5 5 (16%)
carb
    1 7 (22%)
    2 10 (31%)
    3 3 (9.4%)
    4 10 (31%)
    6 1 (3.1%)
    8 1 (3.1%)
1 Median (IQR); n (%)

Creating Overall Table

tbl_summary(mtcars, include = c(am, wt, mpg))
Characteristic N = 321
am 13 (41%)
wt 3.33 (2.58, 3.61)
mpg 19.2 (15.4, 22.8)
1 n (%); Median (IQR)

Creating table captions

library(gtsummary) 
lm(wt ~ am, data = mtcars) |>
  tbl_regression() |>
  modify_caption(caption = "Predicting Car's weight from transmission type") 
Predicting Car’s weight from transmission type
Characteristic Beta 95% CI1 p-value
am -1.4 -1.9, -0.83 <0.001
1 CI = Confidence Interval

Creating figure captions

  • If you have an R chunck that produces a Figure, you can caption it using the fig.cap chunk option. For example:

Figure 1. Scatterplot of Miles per gallon by Displacement
```{r}
#| fig.cap: "Scatterplot of Miles per gallon by Displacement"
ggplot(mtcars, aes(x = mpg, y = disp)) +
  geom_point()
```

Putting extra figures at the end of your document

  • Your prompt tells you to put all “intermediate” figures in the appendix
    • This includes figures that you used to check the assumptions of the models that were not the final model
  • To do this:
    • Save your figures as R objects
    • Create code chunks at the beginning of the Appendix that run these objects (with captions)

Putting extra figures at the end of your document

  • Save the plot to an R object, p
p <- ggplot(mtcars, aes(mpg, disp)) +
  geom_point()
  • Put this R object, p in a chunk at the beginning of your Appendix
```{r, fig.cap = "Scatterplot of Miles per gallon by Displacement"}
p
```

Citations

  • Your introduction should cite prior research in the area of your research question
  • You will need to create a citations.bib file with citations, and then reference them using [@citation]

Citations

  • Open the citations.bib file in your RStudio project

Citations

  • This bibliography is using BibTex, which is a certain way citations are saved in Latex
  • Your citations.bib is going to be a file with multiple BibTex entries
  • You can get Bibtex entries from Google Scholar
    • Search for the article you’d like to cite
    • Under the article there will be ", click that

Citations

  • This bibliography is using BibTex, which is a certain way citations are saved in Latex
  • Your citations.bib is going to be a file with multiple BibTex entries
  • You can get Bibtex entries from Google Scholar
    • Search for the article you’d like to cite
    • Under the article there will be ", click that
    • A window will pop up with several citation styles, click BibTex on the bottom

Citations

  • This bibliography is using BibTex, which is a certain way citations are saved in Latex
  • Your citations.bib is going to be a file with multiple BibTex entries
  • You can get Bibtex entries from Google Scholar
    • Search for the article you’d like to cite
    • Under the article there will be ", click that
    • A window will pop up with several citation styles, click BibTex on the bottom
    • Copy the text in the window that pops up into your citations.bib file in RStudio

Citations

  • The first argument of this citation is the reference key that you will use in your main document. Here it is roumie2017comparative
@article{roumie2017comparative,
  title={Comparative safety of sulfonylurea and metformin monotherapy on the risk of heart failure: a cohort study},
  author={Roumie, Christianne L and Min, Jea Young and D'Agostino McGowan, Lucy and Presley, Caroline and Grijalva, Carlos G and Hackstadt, Amber J and Hung, Adriana M and Greevy, Robert A and Elasy, Tom and Griffin, Marie R},
  journal={Journal of the American Heart Association},
  volume={6},
  number={4},
  pages={e005379},
  year={2017},
  publisher={Am Heart Assoc}
}

Citations

  • Go back to your final report document
  • When you want to cite this paper in the main text, use the key like this: [@roumie2017comparative]

Citations

  • What if the thing you want to cite isn’t on Google Scholar?
  • Generate a BibTex object here:

http://www.citationmachine.net/bibtex/cite-a-website

Citations

  • The first line of your results should say “All analysis is completed in R” and then cite the R packages you used.
  • Run this to get the citation:
print(citation("tidyverse"), bibtex = TRUE)
  • Make sure there is a reference key, if not add one.
@Manual{,
    title = {tidyverse: Easily Install and Load the 'Tidyverse'},
    author = {Hadley Wickham},
    year = {2017},
    note = {R package version 1.2.1},
    url = {https://CRAN.R-project.org/package=tidyverse},
  }

Citations

  • The first line of your results should say “All analysis is completed in R”[@rstats; @tidyverse] and then cite the R packages you used.
  • Run this to get the citation:
print(citation("tidyverse"), bibtex = TRUE)
  • Make sure there is a reference key, if not add one.
@Manual{tidyverse,
    title = {tidyverse: Easily Install and Load the 'Tidyverse'},
    author = {Hadley Wickham},
    year = {2017},
    note = {R package version 1.2.1},
    url = {https://CRAN.R-project.org/package=tidyverse},
  }

Citations

  • The first line of your results should say “All analysis is completed in R”[@rstats; @tidyverse; @broom] and then cite the R packages you used.
  • For more than one, separate with a ;
  • Run this to get the citation:
print(citation("broom"), bibtex = TRUE)
  • Make sure there is a reference key, if not add one.