Skip to content

MANE 3332.03

Lecture 15, March 13

Agenda

  • Continue Chapter 4
  • Today's Topics: R, Midterm Review, Quiz announcement
  • Tech Report One Assignment (assigned 3/6/2025, due 4/3/2025)

Handouts


R in MANE 3332.04

  • This is the third time that R is included in MANE 3332
  • Currently (and this could change) there will be no assignments in R
  • Examples of R will be included in lectures and handouts as covered in class
  • The Grade data shown in this lecture are from Spring 2024
  • R can be installed on your laptop or run through UTRGV Vlabs

What is R?

What is R


The R Project

R-Project


RStudio

RStudio

link to RStudio


RStudio IDE

RStudio IDE


RStudio IDE link to RStudio IDE


UTRGV vLabs

UTRGV vLabs

Link to vLabs


R Markdown

R Markdown link to R Markdown


R Markdown, part 2

R Markdown, part 2


R Markdown, part 3

R Markdown, part 3


Markdown Guide

link to Markdown guide


Creating First R Markdown Project

Create A New Project

  1. Two Methods

    • File, New Project
    • Click on New Project Icon
  2. In general, we use a new directory



Project Type Selection

We want to create a New Project


Create New Project

The Directory Name and location must be specified


Blank Project


Create RMarkdown File


New R Markdown Form

  1. Give title for the R Markdown document in the Title field
  2. Add your name as the author
  3. My preference is to click on the check box below the Date field to use the current date
  4. Select Word as the Default Output Format. PDF requires additional software to be installed. You can always export to PDF from Word.


IDE View


YAML

Yet another markup language(YAML) is contained between the dashed lines. It stores the document definition.


R Code

R Code is displayed in the grey box. R Code snippets have the structure of

```{r}
```


Markdown

Markdown codes appear in lines 12 - 16 of the file. A separate module will cover Markdown


More R Code and Markdown

Knitting a Document

Knitting is the process of create the output document from the YAML block, Markdown code and R code.


Save File


Results of Knitting

  1. Output appears in Console area (lower left pane)
  2. File list is updated and include a Word Document
  3. File area contains updated information (lower right pane)

R Demonstration

  • Creating and knitting default document

Data Creation

Two primary methods + c() functon + Import Excel


C Function

  • R Code
x<- c(1.1,2.2,3.3)
print(x)
  • Demonstration

C Function Output

c Function Output


Import Excel Spreadsheet

  • Requires readxl
  • Not part of standard distribution and must be imported
  • First import into R Studio
  • Add import code to R Markdown package

Import into R Studio

RStudio Window

  • Click on Import Dataset

R Studio - Import Dataset

Import Dataset

  • From Menu, click on "From Excel..."

R Studio - Import Dataset

  • Enter File/URL
  • Notice preview
  • Copy Code Preview in lower right corner
  • Click on Import

Import Dataset


R Studio - Import Dataset

  • Dataset is currently in R Studio but not R Markdown

Import Dataset


Importing an Excel Dataset

  • Add Code Preview lines in R chunk
  • Knit document or run R chunk
library(readxl)
midterm <- read_excel("/Volumes/NO NAME/midterm.xlsx")
View(midterm)     
  • Demonstration

Import Packages

  • Click on Packages (Lower right corner)
  • If not present, click on Import

Import Package


Import Packages, continued

Import Package


Numerical Summaries

  • Called Descriptive Statistics in Chapter 6
    • Descriptive statistics help us understand the location or central tendency of data and the scatter or variability in data
    • Included in all statistical software packages, R does a good job calculating descriptive statistics

Central Tendency

  • Ostle, et. al. (1996) define central tendency as "the tendency of sample data to cluster about a particular numerical value"

  • Population mean

\[ \mu=\frac{1}{N}\sum_{i=1}^Nx_i \]
  • Sample mean
\[ \bar{x}=\hat{\mu}=\frac{1}{n}\sum_{i=1}^nx_i \]
  • Sample median - middle value

  • Sample mode - most commonly occuring number(s)


Measures of Variability

  • There are several statistics that measure the variability or spread present in data

  • Population variance

\[ \sigma^2=\frac{\sum_{i=1}^N\left(x_i-\mu\right)^2}{N} \]
  • Sample variance
\[ s^2=\hat{\sigma}^2=\frac{\sum_{i=1}^n\left(x_i-\bar{x}\right)^2}{n-1} \]
  • Shortcut (Computational) Formula
\[ s^2=\frac{\sum_{i=1}^nx_i^2-\frac{\left(\sum_{i=1}^nx_i\right)^2}{n}}{n-1} \]
  • Standard deviation is often used because it is measured in the original units
\[ \sigma=\sqrt{\sigma^2};\;s=\sqrt{s^2} \]

R Function Summary - Data Frame

summary(midterm)

Descriptive Statistics


R Function Summary - Variable

summary(midterm$MidtermExam)

Descriptive Statistics


R Function Describe

  • Summary() does not report variability
  • Describe() has to be imported
  • Describe() is part of the package psych
library(psych)
describe(midterm)

Describe() Output


Describe Output, part 2

Describe Output