BUA 345 - Posit Cloud Demo

Author

Penelope Pooler Eisenbies

Published

January 23, 2025

Setup

Clicking this first Code shows the setup code required before using R and RStudio to do other things.

In BUA 345, this code will ALWAYS be provided and is straightforward to run.

Code
```{r message=F}
#|label: Setup

# this line specifies options for default options for all R Chunks
knitr::opts_chunk$set(echo=T, 
                      highlight=T)

# suppress scientific notation
options(scipen=100)

# install helper package (pacman) if needed
if (!require("pacman")) install.packages("pacman", repos = "http://lib.stat.cmu.edu/R/CRAN/")

# install and load required packages
# pacman should be first package in parentheses and then list others
pacman::p_load(pacman, tidyverse, gridExtra, magrittr)

# verify packages (comment out in finished documents)
# p_loaded()
```

Import and Clean Data

In BUA 345, data management code will ALWAYS be provided and is straightforward to run.

Click the Code to see the R code below.

This code

  • imports the .csv file.
  • removes duplicates.
  • renames the variables to be lower-case (easier to work with).
  • creates a new categorical variable for Sex with order (levels) and labels as specified.
Code
```{r}
#|label: Import and manage Titanic data
#|
titanic <- read_csv("data/titanic.csv", show_col_types = F) 

# remove duplicates
# rename variables 
# create a factor variable sexF
titanic <- titanic[!duplicated(titanic),] |>
  select(`SURVIVED?`, CLASS, GENDER) |>
  rename(survived=`SURVIVED?`, class=CLASS, gender=GENDER) |>
  mutate(sexF = factor(gender, levels = c("male", "female"), labels = c("Male", "Female")))
```

Barplot of Titanic Data

In BUA 345, data visualization code will ALWAYS be provided and is straightforward to run.

Click the Code to see the R code below.

This code creates a grouped bar plot comparing survival rate by sex with separate panels for each passenger class.

Code
```{r}
#|label: Formatted Boxplot

# grouped side_by_side boxplots with separate panel for each class
(box_titanic <- titanic |>
  ggplot() +
  geom_bar(aes(x=sexF, fill=survived), position="dodge") +
  facet_grid(~class) +
  theme_classic() +
  scale_fill_brewer(palette = "Set1") +
  labs(title="Titanic Survival Demographics", y="Number of Passengers", x="", fill="Survived"))
```

Next Steps

In BUA 345 you will learn

  • how to navigate the R/RStudio environment to run and examine provided code files.

  • how to create models and that analyze data like the data shown here.

  • how to interpret the model output to answer questions about the data.