rm(list =ls())knitr::opts_chunk$set(warning =FALSE, message =FALSE) Mypackages <-c("lme4","tidyverse","effects","ggplot2","psych","MASS","Rmisc","lmerTest","ggthemes", "knitr","lsmeans","pastecs","sjstats","car","ordinal","Rcpp","corrplot", "ggpubr", "EnvStats","easyStats", "cowplot","see","datawizard", "ggcorrplot", "lavaan", "qualtRics", "effsize","stringr")# install.packages(Mypackages) #you must remove the # in this comment if you need to install the packages! lapply(Mypackages, require,character.only =TRUE)options(knitr.kable.NA ='—')set.seed(1)
Load Data
Code
# read in data filessetwd("~/Desktop")data_raw <-read_survey("/Users/mtrenfield17/Desktop/Research/Boston College Research/Morality Lab Research/Study 14/Moral Pilot N20/Study14_MoralN20.csv")
Functions
Code
plot_fn <-function(data, iv, dv, coln =NULL, rown =NULL, facet_var =NULL, facet_var2 =NULL, x_label ="", y_label ="", title ="", x_text_size =13, y_text_size =13, x_title_size =13, y_title_size =13, plot_title_size =16, facet_text_size =12, x_levels =NULL, x_labels =NULL) {# Reorder the x variable if x_levels is providedif (!is.null(x_levels)) { data[[deparse(substitute(iv))]] <-factor(data[[deparse(substitute(iv))]], levels = x_levels) }# Rename the x variable if x_labels is providedif (!is.null(x_labels)) { data[[deparse(substitute(iv))]] <-factor(data[[deparse(substitute(iv))]], labels = x_labels) }# Create the base plot part1 <-ggplot(data, aes(x = {{iv}}, y = {{dv}}, fill = {{iv}})) +geom_violin(alpha =0.3, scale ="count") +stat_summary(fun ="mean", geom ="point", size =3, color ="black") +stat_summary(fun.data = mean_cl_normal, geom ="errorbar", width =0.2,size =1.5, color ="black") +theme_classic() +xlab(x_label) +ylab(y_label) +ggtitle(title) +theme(panel.background =element_rect(fill ="transparent"), legend.position ="right", plot.title =element_text(face ="bold", hjust =0.5, size = plot_title_size), plot.subtitle =element_text(hjust =0.5),panel.grid.major.y =element_line(color='grey75'), axis.text.x =element_text(face ="plain", size = x_text_size, color ="black"),axis.text.y =element_text(face ="plain", size = y_text_size, color ="black"),axis.title.x =element_text(face ="plain", size = x_title_size, color ="black"),axis.title.y =element_text(face ="plain", size = y_title_size, color ="black", margin =margin(t =0, r =10, b =0, l =0)),panel.border =element_rect(color ="black", fill =NA, size =1),strip.text =element_text(size = facet_text_size) # Adjust the facet text size )# Check if a facet_var (row) and facet_var2 (column) are providedif (!is.null(facet_var) &!is.null(facet_var2)) {# If both row and column variables are provided, use facet_grid part1 <- part1 +facet_grid(as.formula(paste(facet_var, "~", facet_var2))) } elseif (!is.null(facet_var)) {# If only one facet variable is provided, facet by rows part1 <- part1 +facet_wrap(as.formula(paste("~", facet_var)), ncol =if (!is.null(coln)) coln elseNULL, nrow =if (!is.null(rown)) rown elseNULL, scales ="free", as.table =TRUE) }# Final plot adjustmentsggpar(part1, legend ="none")}
Reshaping data
Demographics
Code
# making a column for white vs non-whitedata_raw$White <-ifelse(grepl("White", data_raw$Race_Ethnicity_TEXT), "White", "Non-White")# making a column for URM vs non-URMurm_groups <-c("Black", "Hispanic or Latino/a/x", "American Indian and Native Alaskan", "Pacific Islander or Native Hawaiian", "Middle Eastern and North African")data_raw$URM <-ifelse(grepl(paste(urm_groups, collapse="|"), data_raw$Race_Ethnicity_TEXT), "URM", "Non-URM")## Making a Gender column with just Man & Womandata_raw <- data_raw %>%mutate(genderMF =ifelse(Gender_TEXT %in%c("Man", "Woman"), Gender_TEXT, NA))# changing numeric demos to numericdata_raw <- data_raw %>%mutate_at(vars(Age, attn_self), as.numeric)# reordering demosdata_raw <- data_raw %>%mutate(Gender_TEXT =factor(Gender_TEXT, levels =c("Man", "Woman", "I identify as:")),# pol_TEXT = factor(pol_TEXT, levels = c("Very Liberal", "Liberal", "Somewhat Liberal", "Moderate",# "Somewhat Conservative", "Conservative", "Very Conservative")),# edu_TEXT = factor(edu_TEXT, levels = c("Some schooling, but no high school diploma or degree", # "High school diploma or GED", "Some college, Technical degree, or Associates degree", # "Bachelor's degree", "Graduate degree (Masters, PhD, etc)")),# inc_TEXT = factor(inc_TEXT, levels = c("less than $25,000", "$25,000 - $49,999", "$50,000 - $74,999", # "$75,000 - $99,999", "$100,000 - $149,999", "$150,000 - $199,999","more than $200,000")),# political_group = factor(political_group, levels = c("Liberal", "Moderate", "Conservative")),White =factor(White, levels =c("White", "Non-White")),URM =factor(URM, levels =c("Non-URM", "URM")),genderMF =factor(genderMF, levels =c("Man", "Woman")) )
Study Details
Code
# filtering people who failed the attn checkdata_raw$attn_self <-as.numeric(data_raw$attn_self)data <- data_raw %>%filter(attn_self >2)# making numeric DVs numericstart_col <-"1_moral_public"end_col <-"40_private_feedback"cols_in_range <-colnames(data)[which(colnames(data) == start_col):which(colnames(data) == end_col)]columns_to_convert <-grep(".*(?<!_feedback)$", cols_in_range, value =TRUE, perl =TRUE)for (col in columns_to_convert) { data[[col]] <-ifelse(data[[col]] !="feedback", as.numeric(data[[col]]), data[[col]])}# Renaming## renaming motives from # to textmotive_code <-c("1"="Rep", "2"="Norm", "3"="Injuct", "4"="Principle")### Identify the columns that match the pattern "_motive_public|_motive_private"cols_to_rename <-grep("motive_(public|private)_[1-4]", colnames(data), value =TRUE)for (col in cols_to_rename) { number <-str_extract(col, "[1-4]$") # Extract the motive number at the end (1-4) type <-ifelse(str_detect(col, "public"), "public", "private") # Extract whether it's public or private motive_group <- motive_code[[number]] # Get the corresponding motive group from motive_code new_name <-str_replace(col, paste0("motive_", type, "_", number), paste0("motive", motive_group, "_", type)) # Create the new column namecolnames(data)[colnames(data) == col] <- new_name # Rename the column in the dataset}## Rename feedback columns from condition_feedback to feedback_condition colnames(data) <-gsub("_(public|private)_feedback$", "_feedback_\\1", colnames(data))# make dataset longdata_long <- data %>%gather(stim, resp, "1_moral_public":"40_feedback_private")## Split variable names into respective DVsdata_long <- data_long %>%separate(stim, into =c("scenario", "DV", "condition"), sep ="_")# shift dataset back to wide formatdata_long_spread <- data_long %>%pivot_wider(names_from = DV, values_from = resp)# removing dud rows # Define the range of columns to check for NAcolumns_to_check <-c("moral", "virtue", "motiveRep", "motiveNorm", "motiveInjuct", "motivePrinciple") # Adjust the column names as needed# Filter rows where not all values in the specified columns are NAdata_long_spread <- data_long_spread %>%filter(rowSums(is.na(dplyr::select(., all_of(columns_to_check)))) !=length(columns_to_check))# Convert outcomes to numericdata_long_spread[columns_to_check] <-lapply(data_long_spread[columns_to_check], as.numeric)
# Subset your data frame to include only the demographic columnsdemo_data <- data[, c("Gender_TEXT", "Race_Ethnicity_TEXT", "White", "URM")]# Agemean(data$Age, na.rm=TRUE)
[1] 36.4
Code
sd(data$Age, na.rm=TRUE)
[1] 13.84122
Code
# Loop through each demographic column and calculate frequency countsfreq_tables <-list()for (col innames(demo_data)) { { freq_table <-as.data.frame(table(demo_data[[col]])) freq_table$Percent <-round(freq_table$Freq /sum(freq_table$Freq) *100, 2) freq_tables[[col]] <- freq_table }}# Print the frequency tablesfor (i inseq_along(freq_tables)) {if (!is.null(freq_tables[[i]])) {cat("\nTable of frequencies for", names(freq_tables)[i], ":\n")print(freq_tables[[i]]) }}
Table of frequencies for Gender_TEXT :
Var1 Freq Percent
1 Man 20 50
2 Woman 20 50
3 I identify as: 0 0
Table of frequencies for Race_Ethnicity_TEXT :
Var1 Freq Percent
1 Black or African American 4 10.0
2 East Asian 1 2.5
3 East Asian,Southeast Asian,White 1 2.5
4 Hispanic or Latina/o/x/e 3 7.5
5 Hispanic or Latina/o/x/e,White 2 5.0
6 White 29 72.5
Table of frequencies for White :
Var1 Freq Percent
1 White 32 80
2 Non-White 8 20
Table of frequencies for URM :
Var1 Freq Percent
1 Non-URM 36 90
2 URM 4 10
Demographic Plot
Code
# List of demographic columns to plotdemographic_columns <-c("Gender_TEXT", "Race_Ethnicity_TEXT", "White", "URM") # Use column names as strings# Function to create percent plotcreate_percent_plot <-function(data, column) {# Calculate the frequency and percentage for each category freq_table <- data %>%group_by(across(all_of(column))) %>% dplyr::summarise(Freq =n()) %>%mutate(Percent = Freq /sum(Freq) *100)# Create the plot p <-ggplot(freq_table, aes_string(x = column, y ="Percent", fill = column)) +geom_bar(stat ="identity", position ="dodge") +scale_y_continuous(labels = scales::percent_format(scale =1)) +labs(x = column, y ="Percentage", title =paste("Distribution of", column)) +theme_minimal() +theme(axis.text.x =element_text(angle =45, hjust =1))return(p)}# Loop through demographic columns and plotlapply(demographic_columns, function(col) create_percent_plot(demo_data, col))
[[1]]
[[2]]
[[3]]
[[4]]
Code
ggplot(data, aes(x = Age)) +geom_histogram(binwidth =5, color ="black", alpha =0.7) +labs(title ="Age", x =" ", y ="Distribution") +theme_minimal()