Skip to content

Instantly share code, notes, and snippets.

# Helper function
# gets a fitted glmer model
# Example:
# m0 <- glmer(acc ~ cond + (0 + rt | subject_nr) + (1 | item) + (1 | subject_nr), family=binomial(link=logit), data = datos.x.rt)
# Wald is fast and innacurate, better try the longer "profile"
plot.glmer <- function(m0, my.method = "Wald") {
# that could take a while
confint_m0 <- confint(m0, level = 0.95, method=my.method)
#---------------------------------------------------------------
# INSTALL MY PACKAGES SNIPPET install.R
#---------------------------------------------------------------
# Script to detach all previous packages loaded, install those who you
# use and import them. Change the needed argument to install others or paste to your script
# the install_my_packages() fucntion and give other argument
#
# USEFUL:
Temp N Site Biomass
1 0.0299128126353025 -0.622625980526209 Site1 6.04249980071763
2 -0.772925975732505 -1.7547097876668 Site1 3.97444006575064
3 -0.292369334027171 -0.524080764502287 Site1 5.95226209266369
4 0.77240832336247 -0.369625758379698 Site1 9.78922753055458
5 -1.65945612452924 1.65734702162445 Site1 13.0060362943157
6 -1.09825353417546 1.55482099205256 Site2 14.548852128531
7 -0.901877908036113 1.93492870498449 Site2 14.9286928372332
8 -0.910779735073447 -0.0912188850343227 Site2 10.0389281593592
9 0.463317231275141 0.7740329336375 Site2 13.729899888062
# -------------------------------------------------------
# READ IMAGES: Script that reads the images (png) of a folder and
# create a csv with number, size y bytes, x and y length
# -------------------------------------------------------
setwd("~/Dropbox/p1/")
source("./utils/install.R") #source("https://raw.githubusercontent.com/guidocor/R_utils/master/install.R")
install_and_detach("png",clean = F,load = T)
# my own script (install.R), but you can use
# install.packages("png"); library(png)
# Cleaning the session
rm(list=ls());gc()
# Useul function to load and install al needed packages
# we are reading it from my gist on GitHub.
source("https://raw.githubusercontent.com/guidocor/R_utils/master/install.R")
install_and_detach(c("dplyr","ggplot2", "doBy"),load= T, clean = T)
theme_set(theme_bw()) # My favourite ggplot2 theme : )
parts = 30 # Our participants
trials = 30 # Our Trials
# helper function that makes a correlation
# Computes correlation between x and y and returns a list with the
# correlation object and the text as APA states to report it
# Also, the function prints the result, you can change this behavior with silent = FALSE
# Correlation reporting in APA Style: http://my.ilstu.edu/~jhkahn/apastats.html
# Brackets of CI in APA Style: http://blog.apastyle.org/apastyle/2010/06/formatting-statistics-using-brackets.html
correlation <- function(x,y, silent = TRUE, my.method="pearson") {
@guidocor
guidocor / get_iqr_1_5.R
Created September 28, 2016 12:53
Gets the IQR and 1.5 range of the distribution of each member or of class
iqr_1.5 <- function(df, rt, conds, FUN = function(x) {
c(iqr = IQR(x),
lower = quantile(x, c(0.25))-1.5*(quantile(x, c(0.75))-quantile(x, c(0.25))),
upper = quantile(x, c(0.75))+1.5*(quantile(x, c(0.75))-quantile(x, c(0.25))))
}){
# This function calculates the 1.5 * IQR of each participant and condition
# returns a dataframe with the columns formatted.
# Is used to detect outliers in reaction times at participant per condition level.
# Also, yo can use it with custom functions
###"Plotting model estimates in base R graphics"
#There are many ways of displaying data and graphs in R,
#here I propose a base graph solution to plot estimates and confidence
#intervals using a Generalized Linear Mixed Effect Model, but this code
#could be used for other kind of models. So, we have
#a Generalized Linear Effect Model and we want to plot the
#model estimates and its confidence interval. First we install
# Snippet to call and install all libraries required with pacman package
if (!require('pacman')) install.packages('pacman'); library('pacman')
p_load(tidyverse, yarrr, trimr, lattice, doBy, Rmisc, reshape2)
# Dataset taken from trimr package. Check it out and
# don't miss this other way to do Reaction Time outlier detection
# at https://cran.r-project.org/web/packages/trimr/vignettes/trimr-vignette.html
data(exampleData)
head(exampleData)
#############################################################
## 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