library(psych) library(tidyverse) library(GGally) library(qgraph) # Načtení dat df <- haven::read_sav("https://is.muni.cz/go/j6bdrc") df <- df %>% select(id:neshody) glimpse(df) # Extracting metadata metadata <- tibble( var_names = names(df[-1]), var_labels = map_chr(df[-1], ~attributes(.)$label) ) metadata %>% print(n = Inf) # Korelace mezi optimismem a životní spokojeností - grafy df %>% ggplot(aes(optim, ziv_sp)) + geom_point() df %>% ggplot(aes(optim, ziv_sp)) + geom_point() + theme_classic(base_size = 15) # geom_point() a position_jitter df %>% ggplot(aes(optim, ziv_sp)) + geom_point(position = position_jitter(width = .4, height = .1, seed = 123)) # geom_jitter() df %>% ggplot(aes(optim, ziv_sp)) + geom_jitter(height = .1, width = .3) # theme_classic(base_size = 15) df %>% ggplot(aes(optim, ziv_sp)) + geom_jitter(height = .1, width = .3) + theme_classic(base_size = 15) theme_set(theme_classic(base_size = 15)) df %>% ggplot(aes(optim, ziv_sp)) + geom_jitter(height = .1, width = .3) # Adding a regression line df %>% ggplot(aes(optim, ziv_sp)) + geom_jitter(height = 0.1, width = .3) + geom_smooth() # Customizing the regression line df %>% ggplot(aes(optim, ziv_sp)) + geom_jitter(height = 0.1, width = .3) + geom_smooth(method = "lm", level = .99, color = "red", fill = "red", alpha = .2) # Adding labels df %>% ggplot(aes(optim, ziv_sp)) + geom_jitter(height = 0.1, width = .3) + geom_smooth(method = "lm", level = .99, color = "red", fill = "red", alpha = .2) + labs(x = "Optimismus", y = "Životní spokojenost") # Customizing scales df %>% ggplot(aes(optim, ziv_sp)) + geom_jitter(height = 0.1, width = .3) + geom_smooth(method = "lm", level = .99, color = "red", fill = "red", alpha = .2) + labs(x = "Optimismus", y = "Životní spokojenost") + scale_x_continuous(breaks = seq(5, 25, by = 2)) + scale_y_continuous(breaks = seq(1, 4, by = .5)) # Base R functions cor.test(df$optim, df$ziv_sp) cor.test(df$optim, df$ziv_sp, method = "spearman") cor.test(df$optim, df$ziv_sp, conf.level = .99) variables <- c("ziv_sp", "optim", "selfe", "effi", "zdravi", "deprese") df_cor <- select(df, all_of(variables)) cor(df_cor) cor(df_cor, use = "pairwise") cor(df_cor, use = "pairwise") %>% round(digits = 2) # GGally a funkce ggpairs() ggpairs(df, columns = variables) plot_scatter <- function(data, mapping){ ggplot(data = data, mapping = mapping) + geom_jitter(alpha = .05) + geom_smooth(color = "blue", se = FALSE, method = "lm", linetype = "11") + geom_smooth(color = "red", fill = "red", alpha = .2) } ggpairs(df, columns = variables, lower = list(continuous = plot_scatter)) plot_hist <- function(data, mapping){ ggplot(data = data, mapping = mapping) + geom_histogram(fill = "grey", color = "black") } ggpairs(df, columns = variables, lower = list(continuous = plot_scatter), diag = list(continuous = plot_hist)) # Q-Q plots and histograms df_long <- df %>% select(all_of(variables)) %>% pivot_longer(everything(), names_to = "variable") %>% mutate(value = as.double(scale(value)), .by = variable) df_long %>% ggplot(aes(value)) + geom_histogram(aes(y = after_stat(density)), binwidth = .25) + facet_wrap(~variable) + stat_function(fun = dnorm, color = "red", linewidth = 1) df_long %>% ggplot(aes(sample = value)) + geom_qq_line() + geom_qq() + facet_wrap(~variable) # Funce corr.test() z balíčku psych out <- psych::corr.test(df_cor) print(out, short = FALSE) str(out) out$stars # Use complete cases only out <- psych::corr.test(df_cor, use = "complete") print(out, short = FALSE) psych::corr.test(df_cor) psych::corr.test(df_cor, adjust = "holm", # jakou korekci p-hodnot použít method = "spearman", # typ korelace alpha = 0.005) %>% # hladina spolehlivosti bude 1 - alpha print(short = FALSE) str(out) out$stars print(out, short = FALSE) # Let's say you have 3 comparisons and want 95% overall confidence (α = 0.05): # First interval: 1 - (0.05/3) = 0.983 → 98.3% CI # Second interval: 1 - (0.05/2) = 0.975 → 97.5% CI # Third interval: 1 - (0.05/1) = 0.95 → 95% CI # Korelogramy pomocí balíčku corrplot a stejnojmenné funkce corplot() r_mat <- cor(df_cor, use = "pairwise") r_mat # korelační matice corrplot::corrplot(r_mat) corrplot::corrplot(r_mat, method = "number", order = "hclust") corrplot::corrplot.mixed(r_mat, lower = "number", upper = "ellipse", lower.col = "black", order = "hclust") # Network plots df_cor <- df %>% select(warm_m:neshody) names(df_cor) r_mat <- cor(df_cor, use = "pairwise", method = "spearman") diag(r_mat) <- 0 qgraph(r_mat, layout = "spring", theme = "colorblind", threshold = .1, repulsion = .7, groups = list(VŘE = 1:2, DŮV = 4:5, IND = 7:9, NEG = 10:12, CRO = 14:16, CSK = 17:20, VM = 26:28), labels = c("VŘ.M", "VŘ.O", "MON", "DUV.R", "DUV.V", "VZsR", "IND.1", "IND.2", "IND.3", "NEG.1", "NEG.2", "NEG.3", "ROD.E", "ROD.P", "ROD.A", "R.S", "ŠK.E", "ŠK.P", "ŠK.A", "ŠK.S", "OPT", "ZSP", "EST", "EFF", "PSS", "VM.U", "VM.S", "VM.N", "DEP", "NESH"), GLratio = 9)