ACCT 426/BUDA 450

Spring 2024, Exam 1

Your name: Caroline Rodriguez

Question 1 (30%)

Fix the code below

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
margin <- 0.5
ugly_tibble <- tibble(
  id = c(1:5),
  region = c("a", "b" ,"a" ,"b", NA),
  sales = c(23, 44, 44, 22, 100))
# Remove NA values
# Create a profit field using margin
# Sort by profit
# Remove id column
clean_tibble <- ugly_tibble %>%  
  filter(is.na(region)==FALSE) %>% 
  mutate(profit = sales * margin) %>% 
  arrange(profit) %>% 
  select(-id)
print(clean_tibble)
## # A tibble: 4 × 3
##   region sales profit
##   <chr>  <dbl>  <dbl>
## 1 b         22   11  
## 2 a         23   11.5
## 3 b         44   22  
## 4 a         44   22
# Find the number of rows per region, as well as the 
# sum of sales
group_tibble <- clean_tibble %>% 
  group_by(region) %>% 
 summarise(sum_of_sales = sum(sales))

print(group_tibble)
## # A tibble: 2 × 2
##   region sum_of_sales
##   <chr>         <dbl>
## 1 a                67
## 2 b                66
cor.test(clean_tibble$sales, clean_tibble$profit)
## 
##  Pearson's product-moment correlation
## 
## data:  clean_tibble$sales and clean_tibble$profit
## t = 67108864, df = 2, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  1 1
## sample estimates:
## cor 
##   1

Question 2

Complete the following tasks with this tibble.

# Data tibbles used for following tasks
# Do not modify
t <- tibble(
  id = 1:100,
  region = rep(c('WV', 'VA', 'CA', 'TX', 'FL'), 20),
  sales = (1 + runif(100)) ^ 4
)

t[1, ]$sales <- NA
t[2, ]$region <- NA

state_names <- tibble(
  region = c('WV', 'VA', 'CA', 'TX', 'FL'),
  state_name = c('West Virginia', 'Virginia', 'California', 'Texas', 'Florida')
)
print(t)
## # A tibble: 100 × 3
##       id region sales
##    <int> <chr>  <dbl>
##  1     1 WV     NA   
##  2     2 <NA>    2.70
##  3     3 CA      2.03
##  4     4 TX      1.91
##  5     5 FL      7.66
##  6     6 WV      1.12
##  7     7 VA      3.23
##  8     8 CA      4.83
##  9     9 TX      8.17
## 10    10 FL      4.42
## # ℹ 90 more rows

Q2a (20 points)

Create a tibble called WV sales that shows all sales for that state. Place the rows in order by sales. Round sales to the nearest integer (whole number) Exclude any values that are less than 5.

wv_sales <- t %>% 
  filter(region == "WV") %>% 
  arrange(sales) %>% 
  filter(sales < 5) %>% 
  mutate(sales =round(sales))
  

print(wv_sales)
## # A tibble: 9 × 3
##      id region sales
##   <int> <chr>  <dbl>
## 1     6 WV         1
## 2    76 WV         1
## 3    41 WV         2
## 4    71 WV         2
## 5    46 WV         2
## 6    81 WV         3
## 7    26 WV         3
## 8    86 WV         3
## 9    36 WV         4

Q2b (20 points)

Create a tibble called sales summary that shows grouped sales for each state.

Join it with the state names tibble.

Show the median sales by state name. Be sure to fix any issues with NA values.

sales_summary <- t %>% 
  group_by(region)

big_table_raw <- left_join(sales_summary,state_names,by ="region")

#remove Na's
big_table <- big_table_raw %>%  
  filter(is.na(sales)==FALSE,is.na(region)==FALSE)

summary_big_table <- big_table %>% 
  group_by(state_name) %>% 
  summarise(sales_mean = mean(sales))

sales_summary_complete <-  summary_big_table %>% 
  pivot_wider(names_from = state_name,values_from = sales_mean)

print(sales_summary_complete)
## # A tibble: 1 × 5
##   California Florida Texas Virginia `West Virginia`
##        <dbl>   <dbl> <dbl>    <dbl>           <dbl>
## 1       5.00    7.41  6.62     5.55            6.37

Q2c (10 points)

Use ggplot to create a box plot of the t tibble. Show region as x, and sales as y.

library(ggplot2)
library(ggrepel)#this is typically at the top of a document
#cannot have na in boxplots
t_clean <- t %>% 
  filter(is.na(region)==FALSE,is.na(sales)==FALSE,) 
ggplot(data = t_clean) +
  geom_boxplot(mapping = aes(x = region, y = sales))  +
labs(title = 'Sales Per State in the United States') +
  xlab('Sales per Unit') +
  ylab('States')

Q3 (10 points)

There are significant differences between accuracy, precision, and recall. Which measure we would care most about in the following situations:

  1. Investigating fraud. We never want to miss a case, even if we have to investigate a lot of false positives.

recall, okay so I get these words mixed up a lot but I know you are looking for in a confusion matrix you don’t care if we predicted a 1 but got a 0 and we want to minimized the changes of predicting a 0 but actually being a 1.

  1. Screening for a minor medical condition. We want to balance false positives and false negatives equally.

accuracy, in this scenario we are balancing which case we are getting wrong, so we are looking for a case where we actually predict zero and

  1. Testing before giving a difficult chemotherapy treatment. We never want to give this treatment unless it is absolutely necessary.

precision, in this scenario we don’t care about predicting not needing chemo(0) when they actually needing chemo(1), we want to minimize the the times we predict people need chem (1) when they don’t actually need it(0).