Class 1 · Course Overview, Git & R Basics

STAT 517: Advanced Statistical Models · Fall 2026

Welcome to STAT 517. This interactive companion follows the Class 1 code demo: we review R, inspect and transform the gavote data, preview graphics, and finish with one complete Git/GitHub cycle.

Sections 1.1–1.3 cover course logistics, the data-science life cycle, and the scope of STAT 517 in the lecture notes. This code companion begins with Section 1.4 and retains the lecture-note numbering from that point onward.

Each WebR code box runs R directly in your browser. Press Run Code, inspect the result, change something, and run it again. Checkpoint response boxes are intentionally blank for the class demonstration.

1.4 Using R as a calculator

Run a line, inspect the result, then change one number or operator and run it again.

Useful arithmetic operators include +, -, *, /, and ^. Parentheses make the intended order of operations explicit.

TipQuestion 1.1: Pause and predict

Before running the next chunk, predict whether the first two expressions are equal. Explain the role of parentheses.

1.5 Objects, vectors, matrices, and functions

Use <- to assign a value or values to an object. The function c() combines values into a vector, and square brackets select elements.

Matrices store values in rows and columns. They are filled by column unless byrow = TRUE is specified.

TipQuestion 1.2: Try a small variation

Create a vector containing five values. Find its mean, extract its third value, and retain only values above the mean. Then create a \(2\times3\) matrix and a \(3\times2\) matrix, multiply them in both orders, and report the dimensions of both products.

For matrix multiplication, the inner dimensions must match. Use %*% for matrix multiplication and * for element-by-element multiplication.

1.6 Help, packages, and reproducibility

Consulting documentation is a normal part of working in R. The following commands open help pages, so they are displayed but not executed in WebR.

help(quantile)            # documentation for one function
help.search("quantiles")  # search for related help pages
help.start()              # open the documentation index

This page preloads the packages for the browser session. In a local R session, a package is installed once and loaded in each new session in which it is needed. The conditional checks below prevent unnecessary local installation.

if (!requireNamespace("faraway", quietly = TRUE)) install.packages("faraway")
if (!requireNamespace("dplyr", quietly = TRUE)) install.packages("dplyr")
if (!requireNamespace("tidyr", quietly = TRUE)) install.packages("tidyr")
if (!requireNamespace("ggplot2", quietly = TRUE)) install.packages("ggplot2")

Load the installed packages for this browser session.

Keep data preparation and analysis commands in an R script or QMD file. A console history alone does not clearly record what was run, in what order, or why.

1.7 A first data frame: gavote

The gavote data contain information from Georgia’s 159 counties in the 2000 U.S. presidential election. Begin with the documentation, first rows, overall summaries, dimensions, and variable names.

Typing gavote prints the full data frame. Use help(gavote) or ?gavote in a local R session to read the variable definitions.

gavote
help(gavote)
?gavote

Use $ to access one column. Looking at a few values and then a summary is usually more informative than printing all 159 entries.

Construct the relative undercount and Gore vote share from the columns defined in the data documentation.

Tables summarize categorical variables.

TipQuestion 1.3: County-level versus statewide summaries

Why is the statewide undercount not generally equal to mean(gavote$undercount)? Which calculation gives every county equal weight, and which gives every ballot equal weight?

Exploratory graphics

A graph should answer a question about a distribution, comparison, or relationship. These base-R commands follow the reviewed class script.

Pairwise summaries allow a quick scan of several continuous variables.

The grammar of ggplot2

Most ggplot2 calls name the data, map variables to visual properties, and add geometric layers.

  • ggplot() identifies the data frame.
  • aes() maps variables to position, color, shape, and other visual features.
  • geom_*() determines what is drawn, such as points, lines, bars, histograms, or boxplots.
  • labs() edits labels, while theme_*() controls non-data appearance.

1.8 Data wrangling with dplyr and tidyr

dplyr changes, selects, groups, and summarizes rows or columns within a data frame. tidyr changes how values are arranged, such as converting data between wide and long forms. The base pipe |> can be read as “then.”

Function Package Purpose
mutate() dplyr Create or modify columns while retaining rows
select() dplyr Keep, remove, or reorder columns
group_by() dplyr Define groups for a later calculation
summarise() dplyr Reduce rows to summaries, usually one row per group
pivot_longer() tidyr Gather several columns into names and values columns

Create derived variables with mutate() and use select() to make a focused preview.

Use group_by() before summarise() when the same summary should be computed inside each group. Here .groups = "drop" returns an ungrouped result.

pivot_longer() gathers the three candidate columns into a candidate-name column and a vote-count column.

TipQuestion 1.4: Check the reshaped structure

Before running the next chunk, predict how many rows votes_long contains. Which combination of variables uniquely identifies each row? Why does county_id repeat three times for every county?

1.9 Git and GitHub for reproducible work

Git records project history on your computer. GitHub hosts a remote copy of a Git repository. A commit creates a local snapshot; a push sends local commits to GitHub.

1.9.1 One-time setup

Run these commands in the RStudio Terminal, not in the R console. To keep a personal address out of future commits, open GitHub Settings > Emails, select Keep my email addresses private, and copy the exact GitHub-provided noreply address shown there. Do not guess the address because its format varies by account.

git --version
git config --global user.name "Your Name"
git config --global user.email "YOUR-NOREPLY-ADDRESS"
git config --global --get user.email

1.9.2 Clone and authenticate

Create a private GitHub repository named stat517-git-practice and initialize it with a README. In RStudio, choose File > New Project > Version Control > Git, paste the repository’s HTTPS URL, and create the project. The terminal equivalent is git clone REPOSITORY-URL.

The URL identifies the repository; it contains no secret and does not grant access. With HTTPS and Git Credential Manager, the first operation requiring permission opens a browser. Sign in to GitHub, complete two-factor authentication if requested, and authorize the credential manager. It stores an access token in the operating system’s secure credential store, not in the repository URL.

If Git asks for a username and password, do not enter the GitHub account password. Follow GitHub’s credential-caching instructions to configure Git Credential Manager or GitHub CLI; a personal access token is a fallback.

1.9.3 The daily cycle

TipQuestion 1.5: Practice one complete GitHub cycle

Add one sentence to README.md describing what you hope to learn in STAT 517. Review the diff, stage the file, commit it with a specific message, and push. Refresh the repository page on GitHub and locate both the sentence and commit message.

Run the following commands from the repository folder:

git pull
git status
git diff
git add README.md
git diff --staged
git commit -m "describe my first STAT 517 repository"
git push

The RStudio Git pane provides controls for the same operations.

WarningKeep coursework and credentials private

Use private repositories for coursework. Never commit exam materials, restricted data, passwords, access tokens, API keys, or .Renviron files. If a credential is exposed, deleting it in a later commit is insufficient; revoke it immediately.

1.10 Takeaways

  • Use R for arithmetic, objects, vectors, indexing, functions, and matrix multiplication.
  • Inspect the observational unit and variable definitions before transforming or modeling data.
  • Use base graphics and ggplot2 to examine distributions, comparisons, and relationships.
  • Use mutate(), group_by(), and summarise() for derived variables and grouped summaries; use pivot_longer() to reshape repeated value columns.
  • Use Git and GitHub deliberately: pull, inspect, stage, commit, and push.

Next class (August 20): review of simple linear regression, inference, prediction, and diagnostic plots.

1.11 Answer key

Checkpoint answers are intentionally omitted from this class-demo companion. The lecture notes provide the answer key for review after class discussion.