Skip to content

Instantly share code, notes, and snippets.

library(ggplot2)
library(dplyr)
set.seed(123)
# Problem: I want to plot the responses to certain items
# by each condition (a response on other )
# lets simulate some data.
# here we have two conditions, 10 items and 5 response options
n = 6000
df <- data.frame(
@guidocor
guidocor / plot_four_scales.R
Created July 20, 2023 14:38
ggplot snippet to show responses as density plots
if (!require("pacman")) install.packages("pacman")
pacman::p_load("tidyverse", "ggpubr", "see")
set.seed(123)
n = 100
df = data.frame(
part = paste0("id", 1:n),
scale_A = rnorm(n, m = 10, sd = 5),
# Imagine that you want to plot the responses of a ordinal questionnaire
# besides the common practice, is not always correct to use ordinal data as
# continous. There are good resources like https://bookdown.org/Rmadillo/likert/always-visualize.html
# which tell you how to proper represent ordinal data. However, I found that if your scale has missing
# response options in one question... the package likert does not deal with that
# so, I self-made the plot(ex_2_likert, type = "heat") graph
# Creating a fake "ordinal" data with seven questions with 0-4 responses
# Sometimes, you will find useful to discretize a variable.
# An option to do that is creating three equal sized groups
# this is, trichotomization.
# Reference http://www.stat.columbia.edu/~gelman/research/published/thirds5.pdf
library(Hmisc)
# We use cut2 to create three equal groups
# then, by taking the number with as.numeric
# we will have a column filled with the group membership
@guidocor
guidocor / datosejemplo.csv
Created April 2, 2021 12:19
datos_ejemplo
cond_1 cond_2 measure_1 measure_2 measure_3
316 1 0 26 20 34
476 0 0 14 10 12
339 1 1 21 6 16
271 0 0 15 8 25
541 0 1 20 14 24
376 1 1 26 15 30
147 0 1 26 24 22
471 1 0 14 7 23
328 0 0 19 10 20
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
id;edad;LETRA FAVORITA;humor;calida de su letra ñ
A1;21;a;bueno;1
A2;22;B;bueno;2
A3;333;C;bueno;3
A4;89;D;bueno;4
A5;90;c;malo;5
A6;45;d;malo;6
A7;65;c;malo;7
A8;34;a;malo;8
A9;23;b;malo;9
@guidocor
guidocor / clusters_valoracion_politicos.R
Created April 24, 2019 07:30
Clusters con la valoración media de los políticos. Encuesta preelectoral 2019
######################################################################
# Cluster Analysis de la valoración
######################################################################
rm(list=ls()); gc()
options(scipen=20)
if (!require("pacman")) install.packages("pacman")
pacman::p_load("tidyverse", "psych", "haven", "reshape2", "qgraph", "Rmisc", "Hmisc", "mclust", "corrplot")
# Sacamos del CIS la encuesta
@guidocor
guidocor / simpatia_analisis_Factorial.R
Created April 22, 2019 08:49
Análisis Factorial de la simpatía por los políticos en R
Creados para tuitear sobre la encuesta preelectoral y hacer este tuit https://twitter.com/GuidoBCor/status/1120248040848334849
@guidocor
guidocor / plot_individual_estimates.R
Last active June 18, 2018 15:48
Plotting individual estimates
if (!require("pacman")) install.packages("pacman")
pacman::p_load("ggplot2", "Rmisc")
#############################################################
# Preparing data (you can skip this and go to the plot : )
#############################################################
# Simulate some date by trial
parts = 30 # Our participants
trials = 60 # Our Trials
df=data.frame()
#############################################################
## Estimating and plotting Generalized Linear Mixed Models ##
#############################################################
# This script will
# 1) Simulate some experimental data
# 2) Run a glmer with afex
# 3) Estimate predicted marginal means with emmeans
# 4) Plot estimates