Goals

My goal this week was to refine the code I wrote last week to produce the table below from my group’s COVID paper. Jenny had some helpful suggestions on how to trim down the code and combine my two CI columns into one. I hope to implement some feedback Carla gave me in the workshop - which was to include more comments in my code. Also, I hope to use some of my R skills to create a plot for a project for another course.


Progress

Refined Code for Table 1

# Setup

library(tidyverse)
library(ggplot2)
library(here)
library(janitor)
library(devtools)
library(dplyr)
library(papaja)

master <- read.csv("AgeAdvantagesEmotionCovid_Data.csv", header = TRUE)

# Table 1 - Positive Emotions

## Name, Mean and SD

### Select positive emotions from master data set.
pos_emot <- master %>%
  select(f_calm, f_qui, f_app, f_int, f_cont, f_hap, f_rela, f_pea,
         f_ener, f_aff, f_amu, f_acc, f_joy, f_pro, f_reli, f_exc)

### Create a new data frame with the descriptives (M and SD) for positive emotions.
pos_des <- pos_emot %>%
  summarise(
    across(.cols = everything(), na.rm = TRUE, list(M = mean, SD = sd)))

### Rename the positive emotions to table-friendly names.
pos_des <- pos_des %>%
  rename(Calm = f_calm_M, Quiet = f_qui_M, Appreciative = f_app_M,
         Interested = f_int_M, Content = f_cont_M, Happy = f_hap_M,
         Relaxed = f_rela_M, Peaceful = f_pea_M, Energetic = f_ener_M,
         Affectionate = f_aff_M, Amused = f_amu_M, Accomplished = f_acc_M,
         Joyful = f_joy_M, Proud = f_pro_M, Relieved = f_reli_M,
         Excited = f_exc_M)

### Pivot the descriptive twice - one creates two variable with the name and mean of each positive emotion, the other creates the SD.
pos_pivot1 <- pos_des %>%
  select(-starts_with("f_")) %>%
  pivot_longer(cols = everything(), 
               names_to = "Positive Emotion",
               values_to = "M")
  
pos_pivot2 <- pos_des %>%
  select(starts_with("f_")) %>%
  pivot_longer(cols = everything(),
               names_to = NULL,
               values_to = "SD")

### Bind them together to create table-friendly descriptive stats.
pos_des_table <- bind_cols(pos_pivot1, pos_pivot2)

### Create lower and upper 95% CI limits
n <- 945; se <- pos_des_table$SD / sqrt(n)
pos_des_table <- pos_des_table %>%
  mutate(lower_CI = M - qt(1 - (0.05 / 2), n - 1) * se,
         upper_CI = M + qt(1 - (0.05 / 2), n - 1) * se)

### Round CIs to 2 d.p. (can't do this above because of non-numeric variable)
pos_des_table$lower_CI <- format(round(pos_des_table$lower_CI, 2), 
                                 nsmall = 2)
pos_des_table$upper_CI <- format(round(pos_des_table$upper_CI, 2), 
                                 nsmall = 2)

### Combine the CIs into one column
pos_des_table <- pos_des_table %>%
  unite(col = "95% CI", 
        lower_CI:upper_CI,
        sep = " ") %>%
  mutate("95% CI" = paste0("[", `95% CI`, "]"))

### Apply the apa_table function (NOTE: Only produces correct output in APA formatted markdown template which comes with the papaja package)
apa_table(pos_des_table, caption = "Mean Frequencies of Emotions",
          note = "N = 945. CI = 95% confidence interval")
(#tab:unnamed-chunk-1)
Mean Frequencies of Emotions
Positive Emotion M SD 95% CI
Calm 2.44 0.87 [2.39 2.50]
Quiet 2.43 0.87 [2.38 2.49]
Appreciative 2.40 0.93 [2.35 2.46]
Interested 2.28 0.83 [2.23 2.33]
Content 2.15 0.94 [2.09 2.21]
Happy 2.13 0.80 [2.08 2.19]
Relaxed 2.13 0.89 [2.07 2.19]
Peaceful 2.05 0.95 [1.99 2.11]
Energetic 1.90 0.80 [1.85 1.95]
Affectionate 1.89 0.86 [1.83 1.94]
Amused 1.87 0.72 [1.83 1.92]
Accomplished 1.84 0.87 [1.78 1.89]
Joyful 1.71 0.90 [1.65 1.76]
Proud 1.67 0.97 [1.61 1.73]
Relieved 1.48 0.88 [1.42 1.53]
Excited 1.46 0.79 [1.41 1.51]

Note. N = 945. CI = 95% confidence interval

 
Again, the table doesn’t look great in the HTML doc, but in word it looks like this:

Plot for Another Project

This week I had to create a plot showing the expected results of a study I had to design with other students. My newly developed R skills allowed me to quickly whip up a really cool (albeit bland) graph which shows the results from a binary decision task. I am super thankful for this course and its staff for teaching me such practical skills. The fact that I could produce this so quickly for an assignment in another course is really satisfying. I can’t wait to see how I can develop my R skills further.

perc <- c(4, 10, 45, 15, 9, 17,
          2, 4, 18, 6, 15, 55)

group <- c(1, 1, 1, 1, 1, 1,
           2, 2, 2, 2, 2, 2)

predictions <- c(5, 6, 7, 8, 9, 10,
                 5, 6, 7, 8, 9, 10)

exp_data2 <- data_frame(predictions, group, perc)

exp_data2$group <- factor(exp_data2$group, 
                          levels = c(1, 2),
                          labels = c("Low", "High"))
exp_data2$predictions <- factor(exp_data2$predictions, 
                          levels = c(5, 6, 7, 8, 9, 10),
                          labels = c("5", "6", "7", "8", "9", "10"))

exp_bar2 <- ggplot(data = exp_data2,
                   mapping = aes(x = predictions, y = perc, fill = group))

exp_bar2 <- exp_bar2 + 
  stat_summary(fun.y = mean,
               geom = "bar",
               position = "dodge",
               colour = "black") +
  xlab("Number of More-Likely-Outcome Predictions") +
  ylab("Percentage of Participants") +
  theme_apa() +
  scale_fill_manual(name = NULL,
                    labels = c("Low Causal Strength Condition", 
                               "High Causal Strength Condition"),
                    values = c("grey24", "grey69")) +
  theme(legend.position = c(0.3, 0.85),
        legend.title = element_text(size = 8), 
        legend.text = element_text(size = 9)) +
  guides(shape = guide_legend(override.aes = list(size = 1))) +
  guides(color = guide_legend(override.aes = list(size = 0.5))) +
  ylim(0, 80)

print(exp_bar2)

Challenges and Successes

The main challenge I encountered this week was inconsistency in my code whilst refining it. When I removed certain lines or condensed others I would often find my code would break because I removed certain variables or they were named something else. This just encouraged me to check the continuity in my script every time I made a minor change.

I was rather successful this week. I quickly got the hang of the unite() function and used it to combine my CI columns for the table, only to realise that Jenny had managed to do it in wayyyy simpler way (so I ended up just copying her method haha). I also used the R skills I developed in this course to create a really useful plot for an assignment for another course. Also, I added comments to my code outlining each step to allow others (and myself) to better understand my intentions (thanks Carla for the advice!).

Next Step

The next step will be to produce the same table but for negative emotions, or create one big table for all the emotions. I still haven’t decided which route I am going to take.