## Necessary packages to install

install.packages("readxl")
install.packages("ggplot2")
install.packages("ggraph")
install.packages("dplyr")
install.packages("tidyr")
install.packages("tidyverse")
install.packages("plotly")
install.packages("knitr")
install.packages("kableExtra")
install.packages("psych")





# 1. Set a working directory to where the .csv file is located

Please set your working directory before you run the code
setwd("path/to/your/directory")


# 2.   Loading all the necessary packages

library(readxl)
library(ggplot2)
library(ggraph)
library(dplyr)
library(tidyr)
library(tidyverse)
library(plotly)
library(knitr)
library(kableExtra)
library(psych)


# Necessary packages

packages_to_install <- c("readxl", "ggplot2", "ggraph", "dplyr", "tidyr", "tidyverse", "plotly",
                         "knitr","KableExtra")

# Install and load packages
install_and_load <- function(package) {
  if (!require(package, character.only = TRUE, quietly = TRUE)) {
    install.packages(package, dependencies = TRUE)
    library(package, character.only = TRUE)
  }
}

# Apply the function to install and load each package
invisible(sapply(packages_to_install, install_and_load))

3. Read the CSV file

# Read the CSV file

data <- read.csv("C:/Users/svudaru/Desktop/R files/SampleDataforReportingPipeline.csv")




#removing missing values from the data
data <- na.omit(data)
#printing data summary
summary(data) # you can see there are no more missing values
##    subjectID          Group       Condition1_Item1 Condition1_Item2
##  Min.   :  1.00   Min.   :1.000   Min.   :1.000    Min.   :1.00    
##  1st Qu.: 25.50   1st Qu.:1.000   1st Qu.:3.000    1st Qu.:2.00    
##  Median : 50.00   Median :2.000   Median :5.000    Median :4.00    
##  Mean   : 50.29   Mean   :1.535   Mean   :4.414    Mean   :3.99    
##  3rd Qu.: 74.50   3rd Qu.:2.000   3rd Qu.:6.000    3rd Qu.:6.00    
##  Max.   :101.00   Max.   :2.000   Max.   :7.000    Max.   :7.00    
##  Condition1_Item3 Condition1_Item4 Condition1_Item5 Condition1_Item6
##  Min.   :1.000    Min.   :1.000    Min.   :1.000    Min.   :1.000   
##  1st Qu.:3.000    1st Qu.:2.000    1st Qu.:2.000    1st Qu.:2.000   
##  Median :5.000    Median :3.000    Median :4.000    Median :4.000   
##  Mean   :4.455    Mean   :3.697    Mean   :4.071    Mean   :3.848   
##  3rd Qu.:7.000    3rd Qu.:5.000    3rd Qu.:6.000    3rd Qu.:6.000   
##  Max.   :7.000    Max.   :7.000    Max.   :7.000    Max.   :7.000   
##  Condition1_Item7 Condition1_Item8 Condition2_Item1 Condition2_Item2
##  Min.   :1.000    Min.   :1.000    Min.   :5.000    Min.   :5.000   
##  1st Qu.:2.000    1st Qu.:2.000    1st Qu.:5.000    1st Qu.:5.000   
##  Median :4.000    Median :3.000    Median :6.000    Median :6.000   
##  Mean   :3.919    Mean   :3.596    Mean   :5.909    Mean   :5.949   
##  3rd Qu.:6.000    3rd Qu.:5.000    3rd Qu.:7.000    3rd Qu.:7.000   
##  Max.   :7.000    Max.   :7.000    Max.   :7.000    Max.   :7.000   
##  Condition2_Item3 Condition2_Item4 Condition2_Item5 Condition2_Item6
##  Min.   :5.000    Min.   :5.00     Min.   :5.00     Min.   :5.000   
##  1st Qu.:5.000    1st Qu.:5.00     1st Qu.:5.00     1st Qu.:5.500   
##  Median :6.000    Median :6.00     Median :6.00     Median :6.000   
##  Mean   :6.121    Mean   :6.04     Mean   :5.99     Mean   :6.141   
##  3rd Qu.:7.000    3rd Qu.:7.00     3rd Qu.:7.00     3rd Qu.:7.000   
##  Max.   :7.000    Max.   :7.00     Max.   :7.00     Max.   :7.000   
##  Condition2_Item7 Condition2_Item8
##  Min.   :5.00     Min.   :5.00    
##  1st Qu.:5.00     1st Qu.:5.00    
##  Median :6.00     Median :6.00    
##  Mean   :5.97     Mean   :6.01    
##  3rd Qu.:7.00     3rd Qu.:7.00    
##  Max.   :7.00     Max.   :7.00

4. Creating a table showing the N, mean, standard deviation, and standard error

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
# 4. Creating a table showing the N, mean, standard deviation, and standard error


# Melt the data frame to long format
data_long <- data %>%
  pivot_longer(
    cols = starts_with("Condition"),
    names_to = c("Condition", ".value"),
    names_pattern = "Condition(\\d+)_(Item\\d+)"
  )

Finding N, Mean, Median, Mode

library(knitr)
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
# 4 Separate Condition and Item columns

#Produce a table showing the N, mean, standard deviation, and standard error around the mean for each Item (Item 1 - Item 8), broken down by Group and Condition.
result_table <- data %>%
  pivot_longer(
    cols = starts_with("Condition"),
    names_to = c("Condition", "Item"),
    names_pattern = "Condition(\\d+)_Item(\\d+)"
  ) %>%
  group_by(Group, Condition, Item) %>%
  summarise(
    N = n(),
    Mean = mean(value),
    SD = sd(value),
    SE = sd(value) / sqrt(n())
  )
## `summarise()` has grouped output by 'Group', 'Condition'. You can override
## using the `.groups` argument.
# Print the result table
kable(result_table)  # Tabular output 
Group Condition Item N Mean SD SE
1 1 1 46 4.739130 1.8431227 0.2717536
1 1 2 46 4.239130 2.0131212 0.2968185
1 1 3 46 4.673913 2.0981934 0.3093617
1 1 4 46 3.391304 1.6928380 0.2495953
1 1 5 46 3.739130 2.1440852 0.3161281
1 1 6 46 3.782609 2.0101193 0.2963759
1 1 7 46 3.673913 2.0226973 0.2982304
1 1 8 46 3.326087 1.9556681 0.2883475
1 2 1 46 5.760870 0.7655001 0.1128668
1 2 2 46 6.065217 0.8794487 0.1296676
1 2 3 46 6.152174 0.8156086 0.1202549
1 2 4 46 5.891304 0.7952152 0.1172481
1 2 5 46 5.956522 0.7875848 0.1161230
1 2 6 46 6.195652 0.8331884 0.1228469
1 2 7 46 6.000000 0.7888106 0.1163038
1 2 8 46 6.021739 0.8816432 0.1299912
2 1 1 53 4.132076 2.2449750 0.3083710
2 1 2 53 3.773585 1.9378965 0.2661905
2 1 3 53 4.264151 2.2883563 0.3143299
2 1 4 53 3.962264 1.9996371 0.2746713
2 1 5 53 4.358491 1.9815916 0.2721925
2 1 6 53 3.905660 2.1773674 0.2990844
2 1 7 53 4.132076 2.0003628 0.2747710
2 1 8 53 3.830189 2.0544122 0.2821952
2 2 1 53 6.037736 0.8077096 0.1109474
2 2 2 53 5.849057 0.7178018 0.0985977
2 2 3 53 6.094340 0.7909136 0.1086403
2 2 4 53 6.169811 0.7779621 0.1068613
2 2 5 53 6.018868 0.8201915 0.1126620
2 2 6 53 6.094340 0.7662132 0.1052475
2 2 7 53 5.943396 0.7699924 0.1057666
2 2 8 53 6.000000 0.8086075 0.1110708

#Styled Table

library(knitr)
library(kableExtra)

# Run this code to get Tabular output in Viewer.
# Create a styled table
styled_table <- result_table %>%
  kable("html") %>%
  kable_styling()

# Print an HTML table with additional styling
kable(result_table, format = "html", table.attr = 'class="table table-striped table-bordered table-hover"', escape = FALSE)
Group Condition Item N Mean SD SE
1 1 1 46 4.739130 1.8431227 0.2717536
1 1 2 46 4.239130 2.0131212 0.2968185
1 1 3 46 4.673913 2.0981934 0.3093617
1 1 4 46 3.391304 1.6928380 0.2495953
1 1 5 46 3.739130 2.1440852 0.3161281
1 1 6 46 3.782609 2.0101193 0.2963759
1 1 7 46 3.673913 2.0226973 0.2982304
1 1 8 46 3.326087 1.9556681 0.2883475
1 2 1 46 5.760870 0.7655001 0.1128668
1 2 2 46 6.065217 0.8794487 0.1296676
1 2 3 46 6.152174 0.8156086 0.1202549
1 2 4 46 5.891304 0.7952152 0.1172481
1 2 5 46 5.956522 0.7875848 0.1161230
1 2 6 46 6.195652 0.8331884 0.1228469
1 2 7 46 6.000000 0.7888106 0.1163038
1 2 8 46 6.021739 0.8816432 0.1299912
2 1 1 53 4.132076 2.2449750 0.3083710
2 1 2 53 3.773585 1.9378965 0.2661905
2 1 3 53 4.264151 2.2883563 0.3143299
2 1 4 53 3.962264 1.9996371 0.2746713
2 1 5 53 4.358491 1.9815916 0.2721925
2 1 6 53 3.905660 2.1773674 0.2990844
2 1 7 53 4.132076 2.0003628 0.2747710
2 1 8 53 3.830189 2.0544122 0.2821952
2 2 1 53 6.037736 0.8077096 0.1109474
2 2 2 53 5.849057 0.7178018 0.0985977
2 2 3 53 6.094340 0.7909136 0.1086403
2 2 4 53 6.169811 0.7779621 0.1068613
2 2 5 53 6.018868 0.8201915 0.1126620
2 2 6 53 6.094340 0.7662132 0.1052475
2 2 7 53 5.943396 0.7699924 0.1057666
2 2 8 53 6.000000 0.8086075 0.1110708

5 a Plot standard error as error bars with white bars for Condition 1 and gray bars for Condition 2

library(ggplot2)
# 5 a Plot standard error as error bars with white bars for Condition 1 and gray bars for Condition 2

condition_levels <- unique(result_table$Condition)

# Plot standard error as error bars with white bars for Condition 1 and gray bars for Condition 2
ggplot(result_table, aes(x = Item, y = Mean, group = Condition, fill = Condition)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8), color = "black") +
  geom_errorbar(aes(ymin = Mean - SE, ymax = Mean + SE), position = position_dodge(width = 0.8), width = 0.25, color = "black") +
  scale_fill_manual(values = setNames(c("white", "gray"), condition_levels)) +
  labs(title = "Error Bar Plot of Mean Values with Standard Error",
       x = "Item",
       y = "Mean Value") +
  theme_minimal()

5b Create interleaved bar graph for mean of each item for Condition 1 and Condition 2, collapsing across Group

# 5b Create interleaved bar graph for mean of each item for Condition 1 and Condition 2, collapsing across Group
ggplot(result_table, aes(x = Item, y = Mean, fill = Condition)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.8) +
  labs(title = "Interleaved Bar Graph of Mean Values for Condition 1 and Condition 2 (Collapsed Across Group)",
       x = "Item",
       y = "Mean Value") +
  theme_minimal()

Filter for Group 1 participants

  # Filter for Group 1 participants
  result_table_group1 <- result_table %>%
  filter(Group == 1)
  
  
  # Create the same bar graph as in 4 for just Group 1 participants
  ggplot(result_table_group1, aes(x = Item, y = Mean, group = Condition, fill = Condition)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8), color = "black") +
  geom_errorbar(aes(ymin = Mean - SE, ymax = Mean + SE), position = position_dodge(width = 0.8), width = 0.25, color = "black") +
  labs(title = "Error Bar Plot of Mean Values with Standard Error (Group 1)",
       x = "Item",
       y = "Mean Value") +
  theme_minimal()