10.9 Write equations with RMarkdown - Video Tutorials & Practice Problems
Video duration:
7m
Play a video:
<v Voiceover>Being that R</v> is used for data science, sometimes when you're writing reports or presentations you want to include equations. To make this easy, markdown allows you to write equations in a LaTeX like format and have it rendered in the browser very easily. Let's create a new markdown document to see how this works. Click the New File button, Rmarkdown, we will leave it in the HTML format and we will call it equations. We don't need all of this body that's generated automatically so we will delete that. And, you put equations in between sets of double dollar signs. So, our equation will go here. Starting off simple, let's look at the equation for the coefficients of linear regression. That's beta hat equals x transpose x inverse, x transpose y. To form a hat character, you say slash hat. And, whatever is inside these curly braces gets a hat put over it. And, there we want a beta, so we say slash beta equals, and in parentheses, x caret t. That t will appear as a superscript. Then we'll put an x, close the parentheses, and now we wanna say inverse, which is going to be negative one. To do that, we need a caret to say this is a superscript. But, since it's more than one character, we need to enclose it in curly braces. So, that'll be curly brace, negative one, curly brace. We then want to finish off the equation saying x transpose y, so we have x caret t then y. Let's save this document and then we'll run it and see what it looks like. (keyboard clicking) We'll call it equations dot rmd. You can see we have a new file in our directory as seen in the git pane, and we'll run it with the net HTML button. We now have beta hat equals x transpose x inverse, x transpose y. Let's look at them side by side. The slash hat created the hat above the beta and the slash beta created the beta, equals in parentheses we have x caret t gives us x transpose where the t is a superscript, we have x just as normal x, and then the caret negative one inside curly braces gives us the negative one as a superscript above the parentheses. X caret t gives us x superscript t, then y. Let's look a slightly more complicated equation where we have a summation and a fraction. You will create a new equation block, and here we want to say x bar equals the sum of i equals one to n of xi over n. So, to get a bar we say slash bar and inside the curly braces we put x. That way, we get x bar. Equals. We now want the summation symbol, so we say slash sum. Underscore will give us a subscript. Since we want the subscript to be i equals one, it's more than one character, it needs to be in curly braces. We can now say i equals one, close the curly braces, and the superscript will be n, because the sum of i equals one to n is just one character so we don't need curly braces. To make the fraction, we use a special fraction command where we say slash frac, the numerator goes in the first set of curly braces and the denominator goes in the second set of curly braces. In the numerator, we have x underscore i, this gives us x sub i. And in the denominator, we have n. Let's knit this and see what it looks like. Side by side, slash bar curly braces x gives us x bar, equals, the slash sum gives us the summation symbol. The underscore i equals one gives us the i equals one at the bottom of the summation. And, the caret n gives us the n at the top of the summation. The slash frac symbol gives us this fraction, x underscore i gives us x sub i, and n just gives us the n. It's a nice, simple way to use mathematical operators. And, to see some more mathematical symbols, let's look at the equation for the normal distribution. We will make another equation block, up here we will say f of x, semi-colon slash mu comma slash sigma, that's because the normal distribution is parameterized by mu and sigma. This is equal to the fraction one over the square to two pi, sigma. So, we say slash frac, in the first section we say just one, in the second curly braces we say slash sqrt for square root two slash pi, then slash sigma. Close the curly brace for the fraction. Then, it is e to the negative x minus mu squared over two sigma squared. So, we've said e caret slash frac. In the first set of curly braces is negative parentheses x minus slash mu, close the parentheses and square it and close the curly brace. Open a second set of curly braces and put in two slash sigma squared, close the curly brace. We will now knit this. And, we get our nice equation for the normal distribution. Displaying these equations depends on math jacks, which means that this HTML file will either need an internet connection or would need to be on a computer where the math jacks files are located. Luckily, our studio will embed those into the folder for you but it makes it harder to pass around the HTML file as just one file. So, keep that in mind when you give presentations that require equations. Other than that, using the easy LaTeX formatting for your equations makes it very easy to put math in your presentations and documents.