# libs -------------------------------------------------------------------- library(pacman) p_load(magrittr, stringr, kirkegaard, ggplot2, psych) # data -------------------------------------------------------------------- #load data d_r = read.csv("GCSE_cors.csv", encoding = "UTF-8", stringsAsFactors = F, na.strings = "") #remove CIs d_r %<>% unlist() %>% #make it into a long vector str_replace_all(pattern = " \\(.*?\\)", replacement = "") %>% #remove the CIs with regex matrix(., nrow = nrow(d_r), ncol = ncol(d_r)) %>% #back to matrix format as.data.frame(stringsAsFactors = F) #to data.frame #split manually l_v = list(P = d_r[1:8, -1] %>% MAT_get_half() %>% as.numeric(), A = d_r[11:18, -1] %>% MAT_get_half() %>% as.numeric(), C = d_r[21:28, -1] %>% MAT_get_half() %>% as.numeric(), E = d_r[31:38, -1] %>% MAT_get_half() %>% as.numeric()) #to one df d_r_long = stack(l_v) #describe describeBy(d_r_long$values, group = d_r_long$ind) # plot -------------------------------------------------------------------- ggplot(d_r_long, aes(values, group = ind, fill = ind)) + geom_density(alpha = .2) + xlab("Intercorrelations between GCSE tests and general cognitive ability") + xlim(0, 1) ggsave("GCSE_intercors_1.png") # repeat for the second dataset ------------------------------------------- d_r2 = read.csv("GCSE_cors2.csv", encoding = "UTF-8", stringsAsFactors = F, na.strings = "") #remove CIs d_r2 %<>% unlist() %>% #make it into a long vector str_replace_all(pattern = " \\(.*?\\)", replacement = "") %>% #remove the CIs with regex matrix(., nrow = nrow(d_r2), ncol = ncol(d_r2)) %>% #back to matrix format as.data.frame(stringsAsFactors = F) #to data.frame #split manually l_v2 = list(P = d_r2[1:10, -1] %>% MAT_get_half() %>% as.numeric(), A = d_r2[13:22, -1] %>% MAT_get_half() %>% as.numeric(), C = d_r2[25:34, -1] %>% MAT_get_half() %>% as.numeric(), E = d_r2[37:46, -1] %>% MAT_get_half() %>% as.numeric()) #to one df d_r_long2 = stack(l_v2) #describe describeBy(d_r_long2$values, group = d_r_long2$ind) #by variable l_v2$E %>% MAT_vector2full(diag_value = NA) %>% as.data.frame() %>% lapply(describe) # plot -------------------------------------------------------------------- ggplot(d_r_long2, aes(values, group = ind, fill = ind)) + geom_density(alpha = .2) + xlab("Intercorrelations between general cognitive ability, scholastic achievement\n and other predictors") + xlim(-.20, 1) ggsave("GCSE_intercors_2.png")