This is my R Markdown Project

day_2_Rmarkdown

The above text is called a YAML header.

Set up defalts for all chunks in document, <include=FALSE> means it won’t show up in final HTML. <echo=FALSE> means code won’t appear by default. “Global” means it will be applied to entire Markdown.

Header 1

Biggest, RMarkdown format

Header 1

Biggest, HTML format

Header 2

Big

Header 3

Pretty big

Header 4

Bold header line

Intro to Markdown Syntax

No line breaks

Here is an example of bold text. Another example of bold. Here is an example of italic text. Another example of italic. Here is an example of bold italic. What about superscript? x2 What about subscript? x2 Underlining text in RMarkdown isn’t advised. When we incorporate URLs, those links show up as being underlined. But here is how to do it in HTML. Underlined text = no!

No difference

Here is an example of bold text. Another example of bold. Here is an example of italic text. Another example of italic. Here is an example of bold italic. What about superscript? x2 What about subscript? x2 Underlining text in RMarkdown isn’t advised. When we incorporate URLs, those links show up as being underlined. But here is how to do it in HTML. Underlined text = no!

Line breaks

Ctrl+Shift+C

Hidden line break (two spaces):
Line 1
Line 2

Obvious way (break escape):
Line 1
Line 2

* Note: that <br> does a line break and not a new paragraph. Enclosed in back ticks.

Body of document

This is our report introduction and we want to include supporting information in the form of a blockquote from an important person in our field.

This is a quote from an important person.

This person is really important.

This will nest a quote.

Paragraph 2

Paragraph 3

Lists

A numbered list

  1. item 1
  2. item 2
  3. item 3

A symbol list, doesn’t matter what symbol you use

  • item 1

  • item 2

  • item 3

  • item 1

  • item 2

  • item 3

  • item 1

  • item 2

  • item 3

A list with a sublist

  1. item 1
    1. subitem 1
  2. item 2
    1. subitem 2
  3. item 3
    1. subitem 3

This is my super important list

  1. item 1
  2. item 2
  3. item 3

Paragraphs or descriptions under list items

  1. Item 1

    This is a description for item 1. Requires 2 tabs in.

  2. Item 2

    This is a description for item 2.

  3. Item 3

    This is a description for item 3.

Tables in Markdown

Demonstration of a messy table.
Col 1Col 2Col 3Col 4
1111
12121212
123123123123
Demonstration of a clean table.
Col 1Col 2Col 3Col 4
1111
12121212
123123123123

Code chunks in Markdown

Also another way to include a graphic, echo hides the code and only shows the graphic
<include=FALSE> wouldn’t include the figure
A cool graphic

A cool graphic

Generate data

x <- rnorm(n = 100, mean = 5, sd = 1)
y <- 2 + 2 * x + rnorm(n = length(x))
plot(x, y, main = 'y = f(x)')

Include information between (x, y). LaTeX

Our response variable is \(y\) is distributed \(N(\mu, \sigma^2_y)\), where \[\begin{align} \mu &= 2 + 2*E[x] + E(e) \\ \sigma^2 &= 2^Var[x] + Var[e] + 2(2)Cov(x, e) \end{align}\] The <&=> sign aligns the equation by equals sign. <\\> signals new equation line.

We see that \(\mu =\) 11.8318928 and \(\sigma^2 =\) 3.4451727. Let’s round the output.

\(\mu =\) 11.83

\(\sigma^2 =\) 3.45

Single equation in the center instead of in=line equations: \[ y = f(x) \]

Working with Data

df_all <- read_csv('data/daily_bike_data.csv')
dftemp <- df_all %>% select(cnt, temp)
ss_dftemp <- sapply(dftemp,
                    function(x) c(mean(x), min(x), max(x), sd(x))) %>% 
  data.frame() %>% 
  round(digits = 2)

row.names(ss_dftemp) <- c('mean', 'min', 'max', 'sd')

ss_dftemp %>% knitr::kable(caption = 'Summary Statistics')
Summary Statistics
cnttemp
mean4504.350.50
min22.000.06
max8714.000.86
sd1937.210.18
ggplot(dftemp, aes(temp, cnt)) +
  geom_point() +
  labs(title = 'Daily Bike Rental and Temp',
       x = 'Temperature (F, normalized)',
       y = 'Bike Rentals')

dftemp <- dftemp %>% 
  mutate(temp2 = temp^2)

mod1 <- lm(formula = cnt ~ temp,
           data = dftemp)

mod2 <- lm(formula = cnt ~ temp + temp2,
           data = dftemp)

pred_mod1 <- predict(mod1, dftemp['temp'])

pred_mod2 <- predict(mod2, dftemp[c('temp', 'temp2')])

dftemp <- dftemp %>% 
  mutate(cnt_mod1 = pred_mod1,
         cnt_mod2 = pred_mod2)
ggplot(dftemp, aes(temp, cnt)) +
  geom_point() +
  geom_line(aes(temp, pred_mod1), colour = 'red', size = 1) +
  geom_line(aes(temp, pred_mod2), colour = 'blue', size = 1) +
  labs(title = 'Daily Bike Rental and Temp',
       x = 'Temperature (F, normalized)',
       y = 'Bike Rentals')

To try running a Markdown file on Kamiak:

Use an idev and load kitr and markdown before running things. And only output as HTML not PDF.