---
title: "Aesthetic & Geometric Mappings"
subtitle: "EDA w/ Tattoo data"
date: today
toc: true
format:
  html: 
    code-overflow: wrap
    toc: true
    toc-title: "Contents"
    # hide code?

execute:
  warning: false
  message: false
---

# Tattoo Study

We use data from a study on tattoos and impulsiveness. [link](https://www.sciencedirect.com/science/article/abs/pii/S0167268119302495?casa_token=2V6XPTlYs7EAAAAA:LbpPuxs8N1ot12BQ4edRli3oG0we-UYPBeXIticvGI9YA8r_ZqVwD2sJJUM3Rte1HgFAZisT#cit_9).

::: callout-note
There is no readme or label description for this dataset, so we'll have to use our best guess as to what each variable means.
:::

::: {.callout-note title="From the authors"}
*Frederick (2005) developed the cognitive-reflection task (CRT), three questions designed to test subjects' ability to overcome the intuitive but incorrect answer in order to think through the problem to arrive at the correct answer. Poor performance on the CRT has come to be interpreted as an indication of impulsiveness and predicts a wide range of behaviors, including low trust (Corgnet et al., 2016), susceptibility to the base-rate fallacy and other cognitive biases that involve a correct solution (Hoppe and Kusterer, 2011). Most relevant for our study, Frederick (2005) and Oechssler et al. (2009) both find that subjects with higher CRT scores are more likely than low-CRT subjects to choose a later, larger reward over the more immediate, smaller reward.*

*Because the decision to get a permanent-ink tattoo, particularly a visible tattoo, may have been made impulsively with little thought given to future employment consequences, we hypothesize that a tattoo will be associated with fewer correctly answered CRT questions.*
:::

# Expoloratory Data Analysis

From [Chapter 11](https://r4ds.hadley.nz/eda):

1.  Generate questions about your data.

2.  Search for answers by visualizing, transforming, and modelling your data.

3.  Use what you learn to refine your questions and/or generate new questions.

```{r}
# read_data contains read_dta
  library(haven) 
  library(tidyverse)
  library(cowplot)
  library(ggridges) 
```

Load data, examine variable names & view variables that include "tat".

```{r}
# load data from Week 5 folder
tat <- read_dta("https://euclid.nmu.edu/~joshthom/teaching/dat309/week4/tattoo_dataset.dta")

# get variable names
tat_vars <- names(tat)

# select variables involving the phrase tat
tat |> select(contains("tat")) |> glimpse()
```

The `tattoo` variable indicates who has a tattoo and who does not.

```{r}
# count number of people with a tatoo
tat |> group_by(tattoo) |> summarize(n = n())
```

The `occu` contains occupations, and `Teacher` is different from \`teacher. So we fix this. How are tattoos distributed among occupations?

Notice the `fct_reorder` reorders the x-axis for better readability. 

```{r}
tat <- mutate(tat, occu = factor(tolower(occu)))

tat_occu <- tat |> group_by(tattoo,occu) |> summarize(n = n()) |> arrange(-n)

tat_occu |> filter(n > 5) |>  
  ggplot(aes(
    x=fct_reorder(occu,-n), 
    y = n, 
    fill = as.factor(tattoo))) + 
  geom_col(position = "dodge") + 
  coord_flip()

```

Replacing the barplot with lollipop chart

```{r}
# attempt 1
tat_occu |> filter(n > 5, tattoo == 1) |>  
  ggplot(aes(
    y=occu, 
    x = n, 
    fill = as.factor(tattoo))) + 
  # this creates the stick
  geom_segment(aes(
    y = occu, 
    yend = occu, 
    x = 0,
    xend = n)) +
  # makes the circle
  geom_point(aes(
    fill = as.factor(tattoo)),
    shape = 21, size = 5, 
    stroke = 1, color = "black", alpha = .7,
    show.legend = FALSE) +
  labs(x = "Number of People", y = "Occupation", title = "Occupations & Tattoos") + 
    theme(plot.title = element_text(size = 20, colour = "#668cff"),
        axis.title.x = element_text(size = 10, colour = "#6699ff"),
        axis.title.y = element_text(size = 10, colour = "#ff8080")) + 
  # add vertical grid to plot
  # play around with where this is within the plotting function
  theme_minimal_vgrid() 
```

Improve the plot by:

```{r}
# attempt 2
tat_occu |> filter(n > 5, tattoo == 1) |>  
  ggplot(aes(
    y=fct_reorder(occu,-n), 
    x = n, 
    fill = as.factor(tattoo))) + 
  geom_segment(aes(y = fct_reorder(occu,-n), 
    yend = fct_reorder(occu,-n), 
    x = 0,
    xend = n)) +
  geom_point(aes(
    fill = as.factor(tattoo)),
    shape = 21, size = 5, stroke = 1, color = "black", alpha = .7,
    show.legend = FALSE) +
  labs(x = "Number of People", y = "Occupation", title = "Occupations & Tattoos") + 
    theme(plot.title = element_text(size = 20, colour = "#668cff"),
        axis.title.x = element_text(size = 10, colour = "#6699ff"),
        axis.title.y = element_text(size = 10, colour = "#ff8080")) + 
  # add vertical grid to plot
  theme_minimal_vgrid() 
```

But maybe this was too many times to use `fct_reorder` ...

```{r}
# Improve the code above using 
# mutate to make the occu variable an factor ordered by frequency
tat_occu <- tat_occu |> mutate(occu = fct_reorder(occu,-n))
```

Search for variables of a particular type.

```{r}
  # find other categorical variables
  tat |> select(!where(is.numeric))
```

```{r}
  tat |> ggplot(aes(x=alcohol, y = smoke, color = as.factor(tattoo))) + geom_point(position = "jitter")
```

```{r}
tat |> group_by(tattoo, smoke) |> summarize(m = mean(alcohol)) |>
   ggplot(aes(x=smoke,y=m,group = tattoo,fill = as_factor(tattoo))) + geom_col(position = "dodge") 
```

```{r}
  # notice the negative sign
  p <- tat |> ggplot(aes(x=alcohol, y = smoke, color = as.factor(tattoo))) + geom_smooth()

# what happens when you remove this
suppressWarnings(print(p))
```

```{r}
  tat |> ggplot(aes(x=smoke, y = ave_health, color = as.factor(tattoo))) + geom_smooth()
```

```{r}
  tat |> ggplot(aes(x=alcohol, y = ave_health, color = as.factor(tattoo))) + geom_smooth()

```

```{r}
   tat |> ggplot(aes(x = factor(tattoo), fill = factor(CRT2_correct))) + geom_bar()
```

```{r}
   tat |> ggplot(aes(
     x = factor(cut(tat_vis,breaks = c(-1,0,1,50))), 
     fill = factor(CRT2_correct))) + 
     geom_bar(position = "fill") + 
     scale_x_discrete(
       name = "Number of Tattoos", 
       labels = c("0","1","Multiple"))
   
  # tattoos vs. alcohol
  tat |> ggplot(aes(y = factor(tattoo), x = alcohol)) + geom_density_ridges()
  
```

# Tattoos & Crowd-funding

This [study](https://www.sciencedirect.com/science/article/pii/S2352673422000154#bib27) seems interesting but their data is only available upon request. It might be good for a subsequent project.
