#install.packages("modelsummary")

RStudio Intro

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com. See also https://bookdown.org/yihui/rmarkdown/

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Note: R is case sensitive. So Cars and cars are different.

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
#summary(Cars)
# Captial C cars does not load the cars data frame. 

YAML options

you can add a table of contents with toc: true you can hide code chunks you can add date and time prefrences

Chunk options

you can hide the code and just show output echo you can hide both if you want as well with include you can hide any messages such as error messages with message = -

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Install packages

Check out https://rladiessydney.org/courses/ryouwithme/01-basicbasics-2/

# Install the tidyverse "meta" package
#install.packages("tidyverse")

# Install the lme4 package
#install.packages('lme4')
# Apparently I needed that psych package also??
#install.packages("psych")

Load a package

Hint: Try the (PC) Ctrl + Alt + I/(Mac) Cmd + Option + I shortcut.

Hint: Use Tab for code completeion

# Load tidyverse
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.1     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Load lme4
library(lme4)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack

Data Frame

# Load sleepstudy -- what's sleepstudy?

Sleep <- sleepstudy

# Extract one column
col_1 <- Sleep$Subject
# Extract column by index
col_2 <- Sleep[,2]
# Extract two rows
col_3 <- Sleep[1:2,]
# Compute the mean and sd, and chain them together
chain <-  Sleep %>% mean(na.rm = FALSE) %>% sd(na.rm = FALSE)
## Warning in mean.default(., na.rm = FALSE): argument is not numeric or logical:
## returning NA
# Correlation matrix with psych::pairs.panel()
psych::pairs.panels(Sleep)

# Find out what a function does (use `?function_name`, e.g., `?pairs.panel`)

Basic Markdown Elements

Italic and bolded texts

If I want to use italics I use single * and if I want to use bold I use double

Lists (Ordered and Unordered)

To make an ordered list we use numbers

  1. Item number 1
  2. Item number 2
  3. Item number 3

the for unorder we use -

  • main header
    • sub header
    • sub header

then end the list by returning to left align.

Equations (LaTeX)

Inline: \(a^2 + b^2 = c^2\)

Display: \[ 0 = ax^2 + bx +c \]

Cheatsheet

More detailed cheatsheet https://raw.githubusercontent.com/rstudio/cheatsheets/master/rmarkdown-2.0.pdf


Exercises

Complete the following in this R Markdown document:

  1. Copy the following LaTeX equation to below: A_1 = \pi r^2. How does this say about writing Greek letters and subscripts/superscripts? \[[A_1 = \pi r^2 + a]\]

  2. Install and then load the modelsummary package, and run the following. You’ll need to remove eval=FALSE so that it runs. Find out what this code chunk does.

    # Install and load the modelsummary package first; otherwise it won't run
      #install.packages("modelsummary")
    library(modelsummary)
    fm1 <- lm(dist ~ speed, data = cars)
    fm2 <- lm(dist ~ poly(speed, 2), data = cars)
    fm3 <- lm(log(dist) ~ log(speed), data = cars)
    msummary(list(fm1, fm2, fm3))
    ## Warning in !is.null(rmarkdown::metadata$output) && rmarkdown::metadata$output
    ## %in% : 'length(x) = 3 > 1' in coercion to 'logical(1)'

     (1)

      (2)

      (3)

    (Intercept)

    −17.579

    42.980

    −0.730

    (6.758)

    (2.146)

    (0.376)

    speed

    3.932

    (0.416)

    poly(speed, 2)1

    145.552

    (15.176)

    poly(speed, 2)2

    22.996

    (15.176)

    log(speed)

    1.602

    (0.140)

    Num.Obs.

    50

    50

    50

    R2

    0.651

    0.667

    0.733

    R2 Adj.

    0.644

    0.653

    0.728

    AIC

    419.2

    418.8

    55.5

    BIC

    424.9

    426.4

    61.3

    Log.Lik.

    −206.578

    −205.386

    −24.766

    RMSE

    15.07

    14.71

    0.40

  3. Run the following. You’ll need to remove eval=FALSE so that it runs. Find out what this code chunk does.

    ggplot(cars, aes(x = log(speed), y = log(dist))) + 
      geom_point() + 
      geom_smooth()
    ## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

  4. Knit the document to HTML, PDF, and Word. If you run into an error when knitting to any one of the formats, record the error message. Which format do you prefer?

HTML is my prefered

  1. Go to the top of this Rmd file, and change the line inside YAML

     ```
       html_document: default
     ```
    
     to
    
     ```
       html_document: 
           toc: TRUE
     ```
    
     Knit the document again. What does it do?
    
     toc adds a table of contents. I prefer for floating format so I added that here. 
  2. Think about some things you might want to accomplish with R and R Markdown using RStudio and Google to see if people have make packages that will help with that process, or just Google around about these topics and see what you see. Either way, describe 5 interesting things that you found and include URLs.