week3 R learning summary

load packages

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.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ 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

import data

swow <- "data_swow.csv.zip" %>%
  read_tsv() %>%           # read the data from file
  mutate(id = 1:n()) %>%   # add the "id" column
  rename(
    n_response = R1,       # nicer name for the response count
    n_total = N,           # nicer name for the total cue presentations
    strength = R1.Strength # nicer name for the estimated response strength  
  )
## Multiple files in zip: reading 'swow.csv'
## Rows: 483636 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (2): cue, response
## dbl (3): R1, N, R1.Strength
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

words associated with “man” and “woman” ———————————

woman_fwd <- swow %>%
  filter(cue == "woman", n_response > 1) %>%
  select(cue, response, strength, id) %>%
  mutate(
    rank = rank(-strength),  # rank the data by strength
    type = "forward",        # this is forward associate (i.e., it's woman_FWD)
    word = "woman",          # the word is "woman" (i.e., it's WOMAN_fwd)
    associate = response     # for forward associations, the RESPONSE is the asociate
  )

woman_bck <- swow %>%
  filter(response == "woman", n_response > 1)  %>%
  arrange(desc(strength)) %>%
  select(cue, response, strength, id) %>%
  mutate(
    rank = rank(-strength),  # rank the data by strength
    type = "backward",       # this is backward associate (i.e., it's woman_BCK)
    word = "woman",          # the word is "woman" (i.e., it's WOMAN_bck)
    associate = cue          # for backward associations, the CUE is the associate
  )

man_fwd <- swow %>%
  filter(cue == "man", n_response > 1)  %>%
  select(-n_response, -n_total)   %>%
  mutate(
    rank = rank(-strength),  # rank of the association
    type = "forward",        # direction of the association
    word = "man",            # word being "associated to"
    associate = response     # word that is the "associate of"
  )

man_bck <- swow %>%
  filter(response == "man", n_response > 1) %>%
  arrange(desc(strength)) %>%
  select(-starts_with("n_")) %>% # ... remove variables starting with "n_"
  mutate(
    rank = rank(-strength),  # rank of the association
    type = "backward",       # direction of the association
    word = "man",            # word being "associated to"
    associate = cue          # word that is the "associate of"
  )

combine the data sets —————————————————

gender <- bind_rows(woman_fwd, woman_bck, 
                    man_fwd, man_bck) %>%
  select(id:associate) %>%
  filter(associate != "man", associate != "woman")

create and plot gender_fwd ———————————————-

gender_fwd <- gender %>% 
  filter(
    type == "forward"
  ) %>% 
  pivot_wider(
    id_cols = associate, 
    names_from = word, 
    values_from = rank
  ) %>%
  mutate(
    woman = replace_na(1/woman, 0), #filter out the NA
    man = replace_na(1/man, 0),     #filter out the NA
    diff = woman - man
  )  %>%  
  arrange(diff) 


picture_fwd <- ggplot(
  data = gender_fwd,
  mapping = aes(
    x = associate %>% reorder(diff), 
    y = diff
  )) + 
  geom_col() + 
  coord_flip()

plot(picture_fwd)

create and plot gender_bck ———————————————-

gender_bck <- gender %>% 
  filter(
    type == "backward"
  ) %>% 
  pivot_wider(
    id_cols = associate, 
    names_from = word, 
    values_from = rank
  ) %>%
  mutate(
    woman = replace_na(1/woman, 0),  #filter out the NA
    man = replace_na(1/man, 0), 
    diff = woman - man
  )  %>%  
  arrange(diff)

#the dataset gender_bck has 477 rows
#the plot in the slides selectively use columns with relatively large difference

gender_man_bck <- gender_bck [1:17,]
gender_woman_bck <- gender_bck [461:477,]
data_bck = bind_rows(gender_man_bck, gender_woman_bck)

picture_bck <- ggplot(
  data = data_bck,
  mapping = aes(
    x = associate %>% reorder(diff),  
    y = diff
  )) + 
  geom_col() + 
  scale_x_discrete(name = NULL) +  
  scale_y_continuous(name = NULL) + 
  coord_flip() 

plot(picture_bck)