This is supposed to be an example that can help you produce nice documents using R markdown in R notebooks. You don’t need to use this method; as discussed in class, you can do whatever works for you to produce a readable pdf file. But this can work very well if you are willing either to use some latex for equations or import some images (e.g. photos of handwritten equations written out on a piece of paper).

To learn to use this kind of method, you can look at the Word or pdf output in pset-Rmarkdown-help.docx or pset-Rmarkdown-help.pdf and then look at the R markdown document pset- Rmarkdown-help.Rmd to see the code that produced that output.

Problem 1

(1a)

Here is some text first, and then we’ll write some R code. Remember you can start an R code block by using the keyboard shortcut ctrl-alt-I (replace ctrl by cmd on a Mac).

k <- 40 # number of people; this is an R comment
set.seed(123) # you can set the random number seed if you want to get
              # the same random numbers every time you run your code
bdays <- sample(1:365, k, replace=TRUE)
bdays
##  [1] 179  14 195 306 118 299 229 244  14 153  90  91 256 197  91 348 137 355 328
## [20]  26   7 137 254 211  78  81  43 359 332 143  32 109 263 330  23 309 135 309
## [39] 224 166

So that’s 40 randomly generated birthdays. The function table can help us see whether there is a match.

table(bdays)
## bdays
##   7  14  23  26  32  43  78  81  90  91 109 118 135 137 143 153 166 179 195 197 
##   1   2   1   1   1   1   1   1   1   2   1   1   1   2   1   1   1   1   1   1 
## 211 224 229 244 254 256 263 299 306 309 328 330 332 348 355 359 
##   1   1   1   1   1   1   1   1   1   2   1   1   1   1   1   1
max(table(bdays))
## [1] 2

The fact that this max is larger than 1 tells us there was at least one match, and by looking at the output for table(bdays) we see that indeed two people shared the birthday 253.

(1b)

We could repeat the above experiment 10000 times with a loop as follows:

nrep <- 10000
maxs <- rep(0, nrep)
for(i in 1:nrep){
  bdays <- sample(1:365, k, replace=TRUE)
  maxs[i] <- max(table(bdays))
}
mean(maxs > 1)
## [1] 0.8935

This simulation gives an estimate of about 89% for the probability of at least one match among 40 randomly chosen birthdays.

(1c)

Including R plots is easy; just include the commands to make the plot and it will appear in your document.

plot(table(maxs))

Problem 2

(2a)

This could be a “paper and pencil” problem involving math. We can write equations using Latex expressions.1 For example, you can write inline equations, such as \(e^3\approx 20\), and also \(\int_0^1 x^2\,dx=\frac{1}{3}\). To do this you put your latex code between single dollar signs, $, when you write your R script.

If you want math to be displayed on its own line, centered, you can simply put the code between double dollar signs $$ in your R script, like this: \[\int_0^1 x^2\,dx=\frac{1}{3}.\] See how the previous line was displayed?

(2b)

We could explain that last calculation in more detail with a multiline equation:

\[ \begin{aligned} \int_0^1 x^2\,dx &= \left.\frac{1}{3} x^3\right|_0^1 \\ &= \frac{1}{3}1^3 - \frac{1}{3}0^3 \\ &= \frac{1}{3} \end{aligned} \]

This is made with a bunch of latex math, using \\ to end the lines in the multiline equation, and & signs to mark where the equations should be aligned.

(2c) More about Latex

A convenient and helpful list of Latex math symbols can be found here. (And you’ve just seen an example of how to insert a hyperlink. Or you can just write the URL and that will be turned into a hyperlink.)

A tool on the web that might be helpful for writing mathematical expressions in a relatively intuitive way is here. In this tool you click on symbols and things, and the latex code comes out in the yellow box.

If you want to compile directly to pdf, you will need to have Latex installed on your computer; more information about installation can be found at http://www.latex-project.org. But you can also get pdf by first compiling to html or Word and then printing or saving to pdf, so there is no particular need to install latex unless you’d like to.

Problem 3

Here is a way to put in a picture, using the knitr::include_graphics function. You could use this method to include a photo of some equations you’ve written, or a puppy.

For R to be able to find the picture file “puppy.jpg” just given the file name, the file should be in the same folder (or directory) as the R script. (If not, you can specify the whole path to the file instead of just the file name, but it’s probably simplest just to put picture files in the same folder as the R notebook that uses them.) The out.width option added to the beginning of the R is supposed to scale the picture to determine the width (it works when compiling to html and pdf but apparently not to Word). The echo=FALSE option causes the corresponding R chunk not to be “echoed” or included in the resulting document.


  1. To repeat, Latex isn’t required–any legible homework is accepted! But if you are willing to put in a bit of extra time, learning Latex can be a nice thing. And this shows how to make a footnote.↩︎