INTRODUCTION

Instructions

Section 1: R & RStudio Installation

# Run this code to check your R installation
cat("R Version:", paste(R.version$major, R.version$minor, sep="."), "\n")
## R Version: 4.5.2
cat("Platform:", R.version$platform, "\n")
## Platform: x86_64-w64-mingw32

Completion checklist: * R is installed (version 4.0 or later) * RStudio is installed and functional - Check: Help → About RStudio - Current version shown: _______R Version: 4.5.2 ____

Reflection Question 1: What version of R and RStudio are you running? (Screenshot or write the versions below)

Your answer: My R version is 4.5.2

Section 2: Required Packages

# This code checks and loads required packages
required_packages <- c(
  "tidyverse", "rmarkdown", "knitr", "ggplot2", "dplyr", 
  "car", "lmtest", "sandwich", "stargazer", "broom"
)

cat("Loading required packages...\n\n")
## Loading required packages...
all_loaded <- TRUE
for (pkg in required_packages) {
  tryCatch({
    library(pkg, character.only = TRUE)
    cat("✓", pkg, "loaded successfully\n")
  }, error = function(e) {
    cat("✗", pkg, "FAILED to load\n")
    all_loaded <<- FALSE
  })
}
## ✓ tidyverse loaded successfully
## ✓ rmarkdown loaded successfully
## ✓ knitr loaded successfully
## ✓ ggplot2 loaded successfully
## ✓ dplyr loaded successfully
## ✓ car loaded successfully
## ✓ lmtest loaded successfully
## ✓ sandwich loaded successfully
## ✓ stargazer loaded successfully
## ✓ broom loaded successfully
cat("\n")
if (all_loaded) {
  cat("✓ All packages loaded successfully!\n")
} else {
  cat("⚠ Some packages failed. Run the code in Appendix A to install them.\n")
}
## ✓ All packages loaded successfully!

All 10 packages loaded without errors Reflection Question 2: Did any packages fail to load initially? If yes, which ones and how did you resolve it?

Your answer:No, all packages loaded correctly

Section 3: Working Directory & Project Structure

# set working directory
setwd("C:/Users/God's Icon/Desktop/553-Statistical Inference/553 Lab Codes")

# Check current working directory
cat("C:/Users/God's Icon/Desktop/553-Statistical Inference/553 Lab Codes")
## C:/Users/God's Icon/Desktop/553-Statistical Inference/553 Lab Codes
cat(getwd(), "\n\n")
## C:/Users/God's Icon/Desktop/553-Statistical Inference/553 Lab Codes
list.files()
##  [1] "553 Introductory Checklist.docx"         "553 Lab Codes.Rproj"                    
##  [3] "data"                                    "EPI 553_Lab.Rmd"                        
##  [5] "EPI553_Lab01_NHANES_DataExploration.Rmd" "epi553_review552.Rmd"                   
##  [7] "figures"                                 "Frimpong_Marina_Setup_Checklist.html"   
##  [9] "Frimpong_Marina_Setup_Checklist.Rmd"     "Lab01_NHANES_MarinaFrimpong.Rmd.html"   
## [11] "Lab01_NHANES_MarinaFrimpong.Rmd.Rmd"     "outputs"                                
## [13] "rsconnect"                               "scripts"
rproj_files <- list.files(pattern = "\\.Rproj$", ignore.case = TRUE)

if (length(rproj_files) > 0) {
  cat("✓ R Project file detected:", rproj_files[1], "\n")
} else {
  cat("⚠ No R Project file found in this folder.\n")
  cat("Create one in RStudio: File → New Project → Existing Directory → choose this folder\n")
}
## ✓ R Project file detected: 553 Lab Codes.Rproj
# Check for suggested folders
cat("\nFolder structure check:\n")
## 
## Folder structure check:
folders <- c("data", "scripts", "outputs", "figures")
for (folder in folders) {
  if (dir.exists(folder)) {
    cat("✓", folder, "/ exists\n")
  } else {
    cat("○", folder, "/ (needs to be created)\n")
  }
}
## ✓ data / exists
## ✓ scripts / exists
## ✓ outputs / exists
## ✓ figures / exists
  • Working directory is set to your course project folder

    • Path:C:/Users/God’s Icon/Desktop/553-Statistical Inference/553 Lab Codes
  • R Project file (.Rproj) exists in your project directory

  • Four folders exist: data/, scripts/, outputs/, figures/

dir.create("data")
dir.create("scripts")
dir.create("outputs")
dir.create("figures")

You can navigate between these folders in RStudio’s Files pane: Yes

Reflection Question 3: Describe your folder structure. What is the full path to your project directory?

Your answer:C:/Users/God’s Icon/Desktop/553-Statistical Inference/553 Lab Codes which includes the folders above

Section 4: R Markdown & Rendering

# Verify R Markdown package
if (require("rmarkdown", quietly = TRUE)) {
  cat("✓ rmarkdown package is installed and loaded\n")
  cat("✓ You can render to HTML, PDF, and Word formats\n\n")
  cat("To render a document:\n")
  cat("1. Click the 'Knit' button (top of editor) or press Ctrl+Shift+K\n")
  cat("2. Select output format (HTML, PDF, Word)\n")
  cat("3. View the rendered document in RStudio Viewer\n")
} else {
  cat("✗ rmarkdown not found. Run: install.packages('rmarkdown')\n")
}
## ✓ rmarkdown package is installed and loaded
## ✓ You can render to HTML, PDF, and Word formats
## 
## To render a document:
## 1. Click the 'Knit' button (top of editor) or press Ctrl+Shift+K
## 2. Select output format (HTML, PDF, Word)
## 3. View the rendered document in RStudio Viewer

Completion checklist:

  • You can see the “Knit” button at the top of this document in RStudio: Yes

  • You have successfully rendered an R Markdown file to HTML: Yes Document rendered: Yes Date/time rendered: _January 23, 2026__

  • The rendered HTML file opened automatically in the RStudio Viewer

  • You can save the rendered HTML file to your outputs/ folder

Reflection Question 4: Describe the process you followed to render this checklist to HTML. What did you click and what happened?

Your answer:To render the checklist to HTML, I opened the R Markdown file in RStudio and checked that the output format at the top was set to html_document. Then I clicked the Knit button at the top of the editor.

When I clicked Knit, RStudio ran the document from top to bottom, including all the code chunks. I could see messages in the Console as it worked. When it finished, an HTML file was created and automatically opened in the Viewer or in my web browser.

The first time I knitted the file, RStudio asked me to save it, which created the HTML file and any supporting folders for figures or other output. If there had been any errors, the knitting process would have stopped and shown me where the problem was so I could fix it before trying again.

Section 6: Data & Code Practice

# Load data
library(tidyverse)

# Use built-in mtcars dataset
cat("Dataset: mtcars (Motor Trend Car Road Tests)\n")
## Dataset: mtcars (Motor Trend Car Road Tests)
cat("Sample of data:\n")
## Sample of data:
print(head(mtcars, 3))
##                mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4     21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710    22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
cat("\n\nBasic Summary Statistics:\n")
## 
## 
## Basic Summary Statistics:
summary(mtcars$mpg)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    10.4    15.4    19.2    20.1    22.8    33.9

create a simple visualization

mtcars %>%
  ggplot(aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3, alpha = 0.7) +
  geom_smooth(method = "lm", se = FALSE, alpha = 0.5) +
  labs(
    title = "Fuel Efficiency vs Weight",
    subtitle = "EPI 553 - Setup & Workflow Checklist",
    x = "Weight (1000 lbs)",
    y = "Miles Per Gallon (mpg)",
    color = "Cylinders"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(size = 14, face = "bold"),
    legend.position = "bottom"
  )

R output visualization #### Fit a regression model

# Fit a simple linear regression model
model <- lm(mpg ~ wt + factor(cyl), data = mtcars)

cat("Linear Regression Model: mpg ~ weight + cylinders\n\n")
## Linear Regression Model: mpg ~ weight + cylinders
print(summary(model))
## 
## Call:
## lm(formula = mpg ~ wt + factor(cyl), data = mtcars)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -4.589 -1.236 -0.516  1.384  5.792 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    33.991      1.888   18.01  < 2e-16 ***
## wt             -3.206      0.754   -4.25  0.00021 ***
## factor(cyl)6   -4.256      1.386   -3.07  0.00472 ** 
## factor(cyl)8   -6.071      1.652   -3.67  0.00100 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.56 on 28 degrees of freedom
## Multiple R-squared:  0.837,  Adjusted R-squared:  0.82 
## F-statistic: 48.1 on 3 and 28 DF,  p-value: 3.59e-11
# Extract key results
cat("\n\nKey Results:\n")
## 
## 
## Key Results:
cat("R-squared:", round(summary(model)$r.squared, 4), "\n")
## R-squared: 0.8374
cat("Residual Std. Error:", round(summary(model)$sigma, 4), "\n")
## Residual Std. Error: 2.557

Including Plots

You can also embed plots, for example:

R output visualization

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