Skip to content

Instantly share code, notes, and snippets.

@smithdanielle
smithdanielle / ggplot_qqPlot.R
Last active January 13, 2016 13:46
Example of qqplots in ggplot2
occlusion2.data$qqplot.comparison.resid <- ddply(.data = occlusion2.data$qqplot.comparison, .variables = .(condition, transformed), function(dat){
q <- qqnorm(dat$threshold, plot = FALSE)
dat$xq <- q$x
dat
}
)
occlusion2.qqplot <- ggplot(data = occlusion2.data$qqplot.comparison.resid, aes(x = xq, y = threshold)) +
geom_smooth(method = "lm", se = FALSE, size = 1) +
geom_point() +
@smithdanielle
smithdanielle / outlierRemoval.R
Last active February 27, 2017 12:20
Outlier removal in R with MAD criterion: equations taken from [Leys et al (2013)](http://www.sciencedirect.com/science/article/pii/S0022103113000668).
require(plyr)
require(dplyr)
data.outliersRemoved<-ddply(data, .(withinIV1, withinIV2), function(d){
limits.outliers = median(d$DV) + 2.5*c(-1, 1)*mad(d$DV))
d$DV[which(((d$DV - limits.outliers[1])*(limits.outliers[2] - d$DV)) <= 0)]<-NA
return(d)
})
@smithdanielle
smithdanielle / lsmeanPlotting.R
Created November 19, 2015 12:59
Plotting in ggplot2 using lsmeans
ggplot(NULL, aes(x = x, y = NULL))+
geom_point(data = data,
aes(y = y, fill = z, color = z),
position = position_jitterdodge(jitter.width = .2, jitter.height = .2, dodge.width = .9),
alpha = .15, size = 3)+
geom_crossbar(data = data.frame(summary(lsmeans(mixed.model$full.model, ~ z|x)),
aes(y = lsmean, ymax = upper.CL, ymin = lower.CL, color = view),
size = 1, position = "dodge", fatten = 2)
@smithdanielle
smithdanielle / ssw2014LMEVariations.R
Created November 4, 2015 15:15
Variations on typical lme4 models for the ssw2014 experiment; includes residual TNO scores and PCA
ssw2014.data$behavioural$resid.log.crossedTNO<-residuals(lm(log.crossedTNO~log.uncrossedTNO, data = ssw2014.data$behavioural))
ssw2014.data$behavioural$resid.log.uncrossedTNO<-residuals(lm(log.uncrossedTNO~log.crossedTNO, data = ssw2014.data$behavioural))
ssw2014.data$behavioural$c.resid.log.crossedTNO<-scale(ssw2014.data$behavioural$resid.log.crossedTNO, center = T, scale = F)
ssw2014.data$behavioural$c.resid.log.uncrossedTNO<-scale(ssw2014.data$behavioural$resid.log.uncrossedTNO, center = T, scale = F)
ssw2014.data$behavioural$principal.component.TNO<-prcomp(ssw2014.data$behavioural[,c("log.crossedTNO", "log.uncrossedTNO")], center = T, scale. = T)$x[,"PC1"]
ssw2014.data$behavioural$c.principal.component.TNO<-scale(ssw2014.data$behavioural$principal.component.TNO, center = T, scale = F)
ssw2014.mixed.all.residUncrossed<-mixed(formula = z.score ~ view*activity*c.log.crossedTNO*c.age + view*activity*c.resid.log.uncrossedTNO*c.age + (1+activity+view|ID), data = ssw2014.data$behavioural)
ssw2014.mixed.all.resi
> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
@smithdanielle
smithdanielle / lm-beta-lmer
Created March 31, 2015 20:43
Get standardised regression coefficients from a LMER object in R
lm.beta.lmer <- function(mod) {
b <- fixef(mod)[-1]
sd.x <- apply(getME(mod,"X")[,-1],2,sd)
sd.y <- sd(getME(mod,"y"))
b*sd.x/sd.y
}
@smithdanielle
smithdanielle / sessionInfoDump.txt
Created February 25, 2015 15:54
A dump from R's `sessionInfo()` comamnd on 25/02/15
R version 3.1.0 (2014-04-10)
Platform: x86_64-apple-darwin13.1.0 (64-bit)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] splines grid stats graphics grDevices utils datasets methods base
other attached packages:
@smithdanielle
smithdanielle / expeditionNum.m
Last active August 29, 2015 14:15
Making psychophysics marginally less boring for children; find image file [here](http://imgur.com/PswTGzw)
runNum = input('run number: ');
% Load rocket image
endRocket.load = imread('rocket2.png');
endRocket.texture = Screen('MakeTexture', windowPtr, endRocket.load);
% Get the size of the image
[s1, s2, s3] = size(endRocket.load);
% Get the aspect ratio of the image. We need this to maintain the aspect
% ratio of the image when we draw it different sizes. Otherwise, if we
% don't match the aspect ratio the image will appear warped / stretched
@smithdanielle
smithdanielle / stereothresholdMinimalExample.m
Created January 21, 2015 15:42
A script that draws the dots for #stereothreshold and shows them in a figure. When finalised, ALL CHANGES HERE NEED TO BE TRANSFERRED TO STEREOTHRESHOLD.M IN STEREOTHRESHOLD SPACE-THEME BRANCH
close all
clear all;
screenParams.Res = [1024 768];
visAngPxl = 0.0018; % visual angle subtended by a single pixel
% Find center of screen:
xcen = screenParams.Res(1)/2;
ycen = screenParams.Res(2)/2;
@smithdanielle
smithdanielle / gaborDotsMinimal.m
Last active August 29, 2015 14:09
Drawing dots using PTB3's `CreateProceduralGabor`
% gaborDotsMinimal.m
% An example of how to draw dots using PTB3's `CreateProceduralGabor`
sca;
try
% PTB-3 correctly installed and functional? Abort otherwise.
AssertOpenGL;
% Select screen with maximum id for output window:
screenid = max(Screen('Screens'));