---
title: "Homework guidelines for Computational Statistics"
output: pdf_document
---

All homeworks for this course should be written using RMarkdown in RStudio, and submitted as a PDF document.  RMarkdown is a plain-text format that allows you to create rich documents in a variety of common formats, such as PDF, HTML, or Microsoft Word.  The basic idea is that you write a plain-text document with some plain-text formatting notations, and your machine (such as RStudio) turns that into a pretty PDF, HTML, or Word document.  The power of RMarkdown is that it allows you to include R code and equations in a way that is easy to write, but produces attractive and legible output.

To create a new RMarkdown document, in RStudio you go to File -> New File -> R Markdown.  This creates a new plain-text file with a .Rmd ending.  Once you have created a new .Rmd plain-text document, you write your homework answers in that document, including any R code or equations you want, and then when you are done (or want to check how the output will look so far), you "knit" the .Rmd file into the final PDF, Word, or HTML document, which is what you will submit as your finished product.  You will always have two documents for any assignment: the plain-text .Rmd file, and the prettier PDF, Word or HTML file you "knit" the .Rmd file into.  

To "knit" the pretty document, you click on "Knit PDF" in the Source pane in RStudio, which will create the PDF in the same directory as the .Rmd file, and also open it in RStudio to preview it. For this class, we want the homework submissions to be in PDF format, because that works well with Blackboard. Homework file names should be in the following format: HW_01_lastname_firstinitial.pdf, where of course 01 gets replaced with whatever the appropriate homework number is.

### Generating PDFs with Latex

When you create a new RMarkdown document, RStudio asks you what format you want your final output to be in: PDF, Word, or HTML. Unlike HTML or Word formats, generating PDFs with RMarkdown requires one additional step.  You have a few different options for getting PDFs to work in RStudio:

1. To generate PDFs directly with RStudio, you first need to install another program called "Latex."  On a Mac, go here <https://tug.org/mactex/> to install MacTex; on a Windows machine go here to install Miktex <http://miktex.org/download> (see below for more details for Windows installation).  Once Latex is installed, this allows you to knit your .Rmd directly into PDF format.

2. If you have trouble installing Latex, as a __temporary solution__ you can knit the .Rmd as HTML or a Word document, and then open the HTML in your browswer and print the page to a PDF, or open the Word document as save as a PDF.  Note that this should be a temporary stop-gap in case you can't get method 1 to work immediately; please try to get Latex working as soon as possible, preferably in the first week.

  __Installing Latex on Windows__ If you run into trouble installing Latex on Windows or using it with RStudio's RMarkdown, it may help to follow these steps:    

1. Download the complete Latex package using the Net Installer (not: Basic Installer) from     
<https://miktex.org/2.9/setup>. When running the Miktex Setup, you will see two options: to download MikTex and to Install MikTex. Start with the download option, after which you need to run the Net Installer again and choose the Install option (otherwise the program may not work).

2. Install Pandoc from <https://www.pandoc.org/installing.html>. 

3. The first time you use RMarkdown, let it know where to find the Latex file by typing  the following: 

    ```{r}
    Sys.setenv(PATH=paste(Sys.getenv("PATH"),"C:/Program Files/MiKTeX                 
    2.9/miktex/bin/x64/",sep=";"))
    ```

### The RMarkdown syntax

RMarkdown comes from an earlier language called Markdown, which was originally designed for writing simple HTML.  RMarkdown is just Markdown with the additional ability to include R code and equations. For the purposes of the homeworks, you don't need anything fancy beyond the R code and equations, but if you want to do any fancier formatting, here is a quick reference sheet: <http://en.support.wordpress.com/markdown-quick-reference/> .  As you can see there, Markdown is just a way to generate nice looking formatting using plain text.  In fact, if you know HTML, you can actually just insert HTML directly into your RMarkdown code and it will mostly work fine.

When you open a new RMarkdown file, as you will see, it creates a new .Rmd document with some text already in there.  If you knit this, you can see a nice example of how to format R code to get the right output.  You can also use the homework_solution_example.Rmd file as your template, which is in the Course Resources folder.

If you want a quick review of how RMarkdown works in RStudio, you can go here: <http://rmarkdown.rstudio.com/RMarkdownCheatSheet.pdf>.  


### R Code

The lectures for this course were produced with RMarkdown, and as you can see from them, they do a nice job showing both the raw R code, and the output from that code, in pretty boxes in the final document.  To include R code in your .Rmd file that produces nice output, you put your R code inside a triple set of apostrophe marks like so:


<!-- COMMENT: For those reading the Rmd file, note that these pieces of code are weird because I need the PDF file to *show* the Rmd r code, not execute it.  You will never need to do this!  Everything you write in Rmd will just have the normal triple-apostrophe start, not the indented apostrophe-r used here. -->

        `r ''````{r}
        summary(cars)
        ```

The default text RStudio includes every time you create a new .Rmd document also has some examples in it.  When we knit the .Rmd file with this R code in it, it doesn't just print the R code -- it actually _runs_ the R code, and prints both the code itself, and the output of running that code -- in this case, the summary information about the built-in cars dataset.

```{r, comment=NA}
summary(cars)
```

### Plots

You can also embed R plots in your file, which is all done automatically just by including the R code that generates it:

        `r ''````{r}
        plot(cars)
        ```
        
And when you knit it, the above code produces:

```{r, comment=NA}
plot(cars)
```

You can put any R code inside the triple-apostrophes, and RStudio will quietly run it in the background and output the PDF, Word, or HTML document with the output included.  Again, see the sample code when you create a new RMarkdown file, or you can even look directly at the .Rmd file that generated these Homework Guidelines, which I have included in the Course Resources.

Note that if you are using any packages that need to be loaded, such as `ggplot2`, those packages need to be loaded in your .Rmd file just as they would be if you were running an R script.  But as with R code, the package only needs to be loaded once per file.  So if we wanted a fancier plot using `ggplot2` in our homework file, we would run somthing like:

      `r ''````{r fig.width=5, fig.height=2.5, comment=NA}
      library(ggplot2)
      ggplot(data=airquality,aes(x=Temp)) + geom_histogram()
      ```

This produces the following when we knit it:

```{r fig.width=5, fig.height=2.5}
library(ggplot2)
ggplot(data=airquality,aes(x=Temp)) + geom_histogram()
```

Note that we can also change the figure size and width, which gives a bit more control over looks.  Again, more information on these tweaks can be found here: <http://rmarkdown.rstudio.com/RMarkdownCheatSheet.pdf>.

### Math and equations

Last but not least, RMarkdown also lets you typeset equations. To include mathematical equations, you just put your math in between `$` signs: `$3x+4$` outputs $3x+4$.

There are a few basic examples of syntax for getting pretty equations. On the left is the raw syntax, and on the right the pretty output you get when you knit your .Rmd document.

In the long run, it is much easier to learn these basics than to write your math by hand, scan it, and send it in.

Basics: `$3x + 4y - 6.0z / 12 * 43.8$`: $3x + 4y - 6.0z / 12 * 43.8$

Exponents: `$3^{2x}$` : $3^{2x}$

Subscripts: `$Y_{i}$` : $Y_{i}$

Summation: `$\sum_{i=1}^{10} x_i$`: $\sum_{i=1}^{10} x_i$

Integral: `$\int_{1}^{10} x dx$`: $\int_{1}^{10} x dx$

Fractions: `$\frac{3x-9}{2}$` : $\frac{3x-9}{2}$

Hat: `$\hat{x}$`: $\hat{x}$

Bar: `$\bar{x}$`: $\bar{x}$

Square root: `$\sqrt{b^2-4ac}$`: $\sqrt{b^2-4ac}$

Some greek: `$\alpha$`: $\alpha$, `$\beta$`: $\beta$, `$\chi$`: $\chi$,  `$\delta$`: $\delta$, `$\epsilon$`: $\epsilon$,  `$\lambda$`: $\lambda$, `$\mu$`: $\mu$, `$\pi$`: $\pi$,  `$\rho$`: $\rho$, `$\sigma$`: $\sigma$, `$\theta$`: $\theta$, `$\infty$`: $\infty$

To put an equation on its own line and centered, use two `$$` instead of just one:

`$$p(x; \mu, \sigma) = \frac{1}{\sigma \sqrt{2 \pi}} e^{\frac{-(x-\mu)^2}{2 \sigma^2}}$$`

$$p(x; \mu, \sigma) = \frac{1}{\sigma \sqrt{2 \pi}} e^{\frac{-(x-\mu)^2}{2 \sigma^2}}$$

For more math code, see here: <http://faculty.cbu.ca/srodney/ShortSymbInd.pdf>, though you probably won't need much more than is included here.  

Give it a try!  Open a new RMarkdown file, tinker with it a bit, add a few equations, and then hit "Knit PDF" if you've installed Latex (or "Knit HTML" if you haven't yet).  You'll see that it's super-easy and produces nice-looking output instantly.

And again, remember you can use the homework_solution_example.Rmd in the Course Resources folder as your homework template.