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 / _scatter.R
Last active May 29, 2017 18:40
APS2017 Presidential symposium slide 1
d = read.csv("https://gist.githubusercontent.com/richarddmorey/f7c3ed9fe3f9f1fc0520f332b4a8efd7/raw/900d75f765a086dba65a316504c926da1c1e894a/golf.csv")
plot(d$score, d$size, xlab = "Course score", ylab = "Perceived size", axes = FALSE, ylim = c(0,9), xlim = c(60,140), pch = 19)
axis(2, at = 1:9, las = 1)
axis(1)
abline(lm(size~score, data = d))
cor(d$size, d$score, method = "spearman")
# edge of significance
cor.test(d$size, d$score, method = "spearman")
@richarddmorey
richarddmorey / onetail_lmBF.R
Created April 5, 2017 18:03
One-tailed testing with lmBF
## For the singer dataset
library(lattice)
data(singer)
## Get data ready (recode to two factors)
singer$female = factor(with(singer, grepl("S",voice.part) | grepl("A",voice.part)))
singer$high = factor(with(singer, grepl("S",voice.part) | grepl("T",voice.part)))
## We will test the hypothesis that the main effect
## of "high voice" is such that high voiced singers are shorter
@richarddmorey
richarddmorey / delta_t.R
Last active February 27, 2017 22:20
posterior for delta given only t and N
### Data
t = 2
N = 20
rscale = sqrt(2)/2
### Begin utility functions
posterior = Vectorize(function(delta, t, N1, N2 = NULL, rscale = sqrt(2)/2, log = FALSE){
@richarddmorey
richarddmorey / missings_jags.R
Created December 23, 2016 13:27
Using JAGS to sample missing values in both DVs and IVs in Bayesian regression
set.seed(123) # make reproducible
M = 10000 # Number of posterior samples
N = 20
# sample the IV
x = rnorm(N, 10, 5)
# regression model for DVs
y = 100 + 3*x + rnorm(N,0,10)
## Delete missing data (3 in each)
### Utility functions
# Do a single t test simulation
# report the p value
ttest.sim = function(n, func, true.mean = 0, alpha = 0.05){
x = func(n) - true.mean
t.test(x)$p.value
}
# Do a sequence of M t tests, report significance
@richarddmorey
richarddmorey / getSpookyNums.R
Last active October 28, 2016 17:53
Spooky random numbers
getSpookyNums = function(M){
s = tempfile() # to ensure no caching
my.url = paste0("http://richarddmorey.org/spooky.php?n=",M,"&",s)
as.numeric(readLines(my.url))
}
x = getSpookyNums(10)
@richarddmorey
richarddmorey / normal.xml
Created October 28, 2016 09:32
example XML for distribution
<?xml version="1.0" encoding="utf-8"?>
<distribution>
<names>
<shortname>Normal</shortname>
<longname>Normal</longname>
<alternate>Gaussian</alternate>
<id>normal</id>
</names>
<type>continuous</type>
<support>
@richarddmorey
richarddmorey / solve.Rmd_
Last active November 17, 2016 02:03
Troubleshooting flexdashboard scrollbars
---
title: "Generated data demo"
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: scroll
runtime: shiny
---
```{r setup, include=FALSE}
@richarddmorey
richarddmorey / runAll.R
Last active August 11, 2016 13:16
Run the Essex Data Analysis Summer School JAGS demo in R
devtools::source_gist("2bd1b7a409fad3d71fc2d60884c85e50", filename = "runJAGSDemo.R")
@richarddmorey
richarddmorey / beta.binomial.R
Created August 10, 2016 15:41
example, Essex, beta/binomial
y = 13
N = 35
a = 1
b = 1
theta = seq(0, 1, len = 100)
likelihood = theta ^ y * (1-theta)^(N-y) #dbinom(y, N, theta)
prior = theta ^ (a-1) * (1-theta)^(b-1) #dbeta(theta, a, b)
posterior1 = prior * likelihood