# Load necessary packages library(dplyr) library(ggplot2) library(gridExtra) library(knitr) # Prompt the user to select a file (LongHFScores.csv <- available in the Supplementary Materials) selected_file <- file.choose() # Load the selected file (LongHFScores.csv <- available in the Supplementary Materials) data <- read.csv(selected_file) # Display the first few records from the loaded file head(data) # Function to obtain a specific statistical summary by subgroup and format it as a table get_specific_summary <- function(data, condition, social_isolation) { filtered_data <- data %>% filter(CONDITION == condition, SOCIAL_ISOLATION == social_isolation) # Calculate statistical summary for each variable summary_specific <- as.numeric(summary(filtered_data$SPECIFIC_SCENARIOS)) summary_screen <- as.numeric(summary(filtered_data$SCREEN_TIME)) summary_total <- as.numeric(summary(filtered_data$TOTAL_SCORE)) # Create a data frame for the summary table summary_table <- data.frame( Statistic = c("Min", "1st Qu.", "Median", "Mean", "3rd Qu.", "Max"), SPECIFIC_SCENARIOS = summary_specific, SCREEN_TIME = summary_screen, TOTAL_SCORE = summary_total ) # Display the formatted table cat("Summary for CONDITION =", condition, "and SOCIAL_ISOLATION =", social_isolation, "\n") print(kable(summary_table, format = "markdown")) cat("\n") } # Get statistical summary for each combination of CONDITION and SOCIAL_ISOLATION # CONDITION 1 get_specific_summary(data, 1, 0) get_specific_summary(data, 1, 1) # CONDITION 2 get_specific_summary(data, 2, 0) get_specific_summary(data, 2, 1) # CONDITION 3 get_specific_summary(data, 3, 0) get_specific_summary(data, 3, 1) # Ensure necessary packages are loaded library(dplyr) library(ggplot2) library(gridExtra) # Set the number of bins for histograms num_bins <- 7 # Adjust for between 7 to 10 intervals # Create histograms with density curves for each variable before and during social isolation # SCREEN_TIME before and during isolation hist_screen_time_before <- ggplot(data %>% filter(SOCIAL_ISOLATION == 0), aes(x = SCREEN_TIME)) + geom_histogram(aes(y = after_stat(density)), bins = num_bins, fill = "blue", alpha = 0.4) + geom_density(alpha = 0.5, fill = "blue") + ggtitle("SCREEN_TIME Distribution (Before)") + xlab("Screen Time") + ylab("Density") hist_screen_time_during <- ggplot(data %>% filter(SOCIAL_ISOLATION == 1), aes(x = SCREEN_TIME)) + geom_histogram(aes(y = after_stat(density)), bins = num_bins, fill = "blue", alpha = 0.4) + geom_density(alpha = 0.5, fill = "blue") + ggtitle("SCREEN_TIME Distribution (During)") + xlab("Screen Time") + ylab("Density") # SPECIFIC_SCENARIOS before and during isolation hist_specific_scenarios_before <- ggplot(data %>% filter(SOCIAL_ISOLATION == 0), aes(x = SPECIFIC_SCENARIOS)) + geom_histogram(aes(y = after_stat(density)), bins = num_bins, fill = "red", alpha = 0.4) + geom_density(alpha = 0.5, fill = "red") + ggtitle("SPECIFIC_SCENARIOS Distribution (Before)") + xlab("Specific Scenarios") + ylab("Density") hist_specific_scenarios_during <- ggplot(data %>% filter(SOCIAL_ISOLATION == 1), aes(x = SPECIFIC_SCENARIOS)) + geom_histogram(aes(y = after_stat(density)), bins = num_bins, fill = "red", alpha = 0.4) + geom_density(alpha = 0.5, fill = "red") + ggtitle("SPECIFIC_SCENARIOS Distribution (During)") + xlab("Specific Scenarios") + ylab("Density") # TOTAL_SCORE before and during isolation hist_total_score_before <- ggplot(data %>% filter(SOCIAL_ISOLATION == 0), aes(x = TOTAL_SCORE)) + geom_histogram(aes(y = after_stat(density)), bins = num_bins, fill = "green", alpha = 0.4) + geom_density(alpha = 0.5, fill = "green") + ggtitle("TOTAL_SCORE Distribution (Before)") + xlab("Total Score") + ylab("Density") hist_total_score_during <- ggplot(data %>% filter(SOCIAL_ISOLATION == 1), aes(x = TOTAL_SCORE)) + geom_histogram(aes(y = after_stat(density)), bins = num_bins, fill = "green", alpha = 0.4) + geom_density(alpha = 0.5, fill = "green") + ggtitle("TOTAL_SCORE Distribution (During)") + xlab("Total Score") + ylab("Density") # Arrange all histograms in the same window grid.arrange( hist_screen_time_before, hist_screen_time_during, hist_specific_scenarios_before, hist_specific_scenarios_during, hist_total_score_before, hist_total_score_during, ncol = 2 )