Skip to content

Instantly share code, notes, and snippets.

View richarddmorey's full-sized avatar

Richard Morey richarddmorey

  • Cardiff University
  • Cardiff, Wales
View GitHub Profile
@richarddmorey
richarddmorey / install_flexjags.R
Last active March 19, 2019 12:15
Install flexjags in R for Essex Summer School 3F (Bayesian data analysis)
## First install the devtools package if necessary
devtools::install_github("richarddmorey/EssexSummerSchoolBayes", subdir = "shiny_demo/flexjags/flexjags")
flexjags::run_flexjags()
@richarddmorey
richarddmorey / power.R
Created March 26, 2018 12:48
power graph
M = 1000
N = round(sort(rpois(M, 10)^1.5, decreasing = TRUE)) + 20
pows = c(.5,.8,.95)
setup = expand.grid(N=N, pow = pows)
es = mapply(FUN = function(n, power, alpha) power.t.test(n = n, sig.level = alpha, power = power)$delta,
n = setup$N, power = setup$pow, MoreArgs = list(alpha = .05))
dim(es) = c(setup$es,length(N), length(pows))
@richarddmorey
richarddmorey / analysis.R
Last active December 27, 2017 22:54
data from 855 t tests (from Wetzels et al 2011)
## Paper is here: http://www.ejwagenmakers.com/2011/WetzelsEtAl2011_855.pdf
## Load the three data files
one_sample = read.delim("https://gist.githubusercontent.com/richarddmorey/e44df8750d69b5c0cd8db9513e75fba4/raw/a476c41e23d944bcf1ac6d84860218900bec0fe1/data_onesample.txt")
two_sample = read.delim("https://gist.githubusercontent.com/richarddmorey/e44df8750d69b5c0cd8db9513e75fba4/raw/a476c41e23d944bcf1ac6d84860218900bec0fe1/data_twosample.txt")
paired_sample = read.delim("https://gist.githubusercontent.com/richarddmorey/e44df8750d69b5c0cd8db9513e75fba4/raw/a476c41e23d944bcf1ac6d84860218900bec0fe1/data_pairedsample.txt")
# Define N1 and N2 for one sample tests
# for convenience
colnames(one_sample)[1] = "N1"
data(attitude)
o = lm(rating ~ complaints, data = attitude)
par(mfrow=c(1,2), bg="white")
plot(rating ~ complaints, data = attitude)
abline(o, col = "red")
plot(o, ask = FALSE)
x = rnorm(1000)
y = rnorm(1000)
plot(x,y,pch=19,col=rgb(0,0,1,.2))

plot of chunk unnamed-chunk-2

@richarddmorey
richarddmorey / index.Rmd
Last active November 29, 2017 21:26
example for shinyjs
---
runtime: shiny
---
```{r echo=FALSE, message=FALSE, warning=FALSE}
require(shinyjs)
useShinyjs(rmd=TRUE)
observe({
updateTextInput(session, 'ID', value = "Updated.")
## Richard D. Morey
## 3 Nov 2017
# Redirect plots to temp directory
# We just want the variables
wd = getwd()
setwd(tempdir())
source('https://osf.io/x73zq/download?version=1')
setwd(wd)
@richarddmorey
richarddmorey / bf_inv.R
Created October 17, 2017 08:44
Inverse function of Bayes factor for t test
## Given a (scaled Cauchy) Bayes factor for the null against the
## alternative (Rouder et al 2009), yields the t statistic
## that would yield it. The ... arguments are passed to
## the ttest.tstat function.
bf.inv = Vectorize(function(b10, ...){
fn = Vectorize(function(t,...){
BayesFactor::ttest.tstat(t,...)[["bf"]]
}, "t")
t0 = optimize(function(t0, ...){
@richarddmorey
richarddmorey / for_lakens.R
Last active October 15, 2017 00:20
For Lakens
find.ncp = Vectorize(function(a=.05, b=.2, df=1){
cr = qchisq(1-a,df)
q = optimize(function(q){
(pchisq(cr,df,ncp=q/(1 - q)) - b)^2
},interval = c(0,1))$minimum
q / (1 - q)
},"b")
find.eb = Vectorize(function(a = 0.05, b = .2, df = 1){
@richarddmorey
richarddmorey / sample.eta2.R
Last active October 2, 2017 09:26
Sampling distribution of eta^2, omega^2-hat, and epsilon^2
## Explore the sampling distribution of eta^2, omega^2-hat, and epsilon^2
## Richard D. Morey
## Sept 30, 2017
## Settings
## Number of participants in group
N = 10
## Number of groups
J = 3