Chapter 1 Setup

1.1 What you need

To work through this book, you will need:

  • Access to R/RStudio
  • An internet connection (for installing packages)
  • A basic familiarity with saving and opening files on your computer

1.2 R and RStudio (quick overview)

R is the language that runs your analysis. RStudio is the interface you use to write code, run code, view plots, and manage files. You have three options for using R/RStudio:

  1. Use OSC Classroom On Demand
    Using the OSC Classroom on Demand is by far the easiest way to use R/RStudio for this course. All packages (see below for more info on packages) should already be loaded and tested to ensure compatability.
  2. Install on your own machine
    This option involves two steps. First, install “base” R from the Comprehensive R Archive Network (CRAN): https://cran.r-project.org/. Second, install RStudio: https://posit.co/download/rstudio-desktop/#download.
  3. Use a computer in an on-campus computer lab.
    This option should be considered a last resort. I cannot guarantee all packages will be able to be installed on university machines, and you would need to install the packages every time you use the machine.

1.4 Installing and loading packages

Packages extend R. You typically:

  1. Install a package once per computer
  2. Load the package each time you start a new R session

1.4.1 Install packages (one-time)

Packages are installed using the install.packages("package_name") function. For example, run the code below in the Console. If you see messages scrolling by, that is normal.

install.packages("tidyverse")
install.packages("devtools")

1.4.2 Load packages (each session)

Packages are loaded using the library(package_name) function. You may see messages and/or warnings scroll by when loading package, which is normal behavior.

library(tidyverse)
library(devtools)

1.5 Installing the MKT4320BGSU package

The MKT4320BGSU package contains the functions and datasets used throughout this course.

1.5.1 Install from GitHub

devtools::install_github("jdmeyer73/MKT4320BGSU")

1.5.2 Load the package

library(MKT4320BGSU)

1.5.3 Verify everything works

If the chunks below run without errors, you are set.

data("directmktg")
head(directmktg)
# A tibble: 6 × 5
    userid   age buy   gender salary
     <dbl> <dbl> <fct> <fct>   <dbl>
1 15624510    19 No    Male       19
2 15810944    35 No    Male       20
3 15668575    26 No    Female     43
4 15603246    27 No    Female     57
5 15804002    19 No    Male       76
6 15728773    27 No    Male       58

1.6 R Notebooks (How You Will Complete Assignments)

All lab assignments in this course will be completed using R Notebooks (.Rmd files).

An R Notebook combines:

  • Written answers (text you type)
  • R code (that you run)
  • Output (tables, plots, results)

into a single, reproducible document.

You will submit your completed notebook file for grading.


1.6.1 What Is an R Notebook?

An R Notebook is a special type of document that lets you:

  • Write explanations in plain text
  • Run R code in chunks
  • See results immediately below the code
  • Save everything in one file

Conceptually, think of it as:

A Word document + an R script + your results, all in one place

This is how professional analysts document their work.


1.6.2 The Assignment Workflow

For each lab assignment, you will:

  1. Use the provided notebook template in RStudio (for example: LA01WriteUp.Rmd)
  2. Work inside the existing structure
  3. Add:
    • Code inside code chunks
    • Written answers under the Answers sections
  4. Save the file
  5. Submit the .Rmd file in Canvas

You should not delete chunk labels, headings, or instructions unless explicitly told to do so.


1.6.3 Code Chunks (Very Important)

Code in an R Notebook lives inside code chunks, which look like this:


``` r
# Your code goes here
```

Each chunk:

  • Has a label (for example, q1)
  • Runs independently
  • Produces output directly below it

You should only put R code inside code chunks.


1.6.4 Running Code While You Work

While completing assignments, you will primarily run code one chunk at a time or line-by-line inside code chunks.

Use:

  • Run → Run Current Chunk, or
  • Ctrl + Shift + Enter line-by-line

Running chunks:

  • Shows output immediately
  • Helps you debug errors
  • Lets you work step by step through the assignment

This is the expected workflow for completing labs in this course.


1.6.5 Why We Use R Notebooks in This Course

Using R Notebooks allows you to:

  • Clearly connect analysis and interpretation
  • Practice writing professional, data-driven explanations
  • Keep your work organized and reproducible
  • Learn workflows used in real analytics jobs

It also allows your instructor to:

  • See both your code and reasoning
  • Verify results
  • Provide more meaningful feedback

1.6.6 A Final Tip

If you see an error like “object not found,” it usually means:

  • A chunk that creates the object has not been run yet
  • Code is missing or out of order

When in doubt:

Run chunks from the top of the notebook down.

1.7 Getting help when you’re stuck

These are the most useful help tools in R:

  • ?function_name opens help for a function
  • help.search("keyword") searches help files
  • If an error happens, read the last line carefully (it often tells you what to fix)

Examples:

?mean
help.search("logistic regression")

1.8 How you’ll use this book

Most weeks, your workflow will look like this:

  1. Read the relevant sections of the book
  2. Open the lab file for the week
  3. Run the example code
  4. Modify the code to answer lab questions
  5. Interpret results and write conclusions

Errors are normal—debugging is part of learning analytics.

1.9 What’s next

In the next chapter, we’ll cover basic R fundamentals you’ll use constantly, including:

  • objects and assignment
  • vectors and data frames
  • common functions
  • importing data and basic data manipulation