Skip to content

Instantly share code, notes, and snippets.

@lawsofthought
lawsofthought / binomial_logistic_regression.R
Created March 12, 2018 21:49
How to do a binomial (not binary) logistic regression
library(tibble)
ilogit <- plogis # inverse logit
Df <- tibble(corpus = c('bnc_subset', 'subset'),
count = c(5242, 10092),
total = c(27449, 56355))
M <- glm(cbind(count, total-count) ~ 0 + corpus,
@lawsofthought
lawsofthought / plotrix_gannt.R
Created February 15, 2018 11:58
Make a Gantt chart using plotrix in R.
library(plotrix)
# WHat follows is a copy and paste from
# https://www.rdocumentation.org/packages/plotrix/versions/3.7/topics/gantt.chart
Ymd.format<-"%Y/%m/%d"
gantt.info<-list(labels=
c("First task","Second task","Third task","Fourth task","Fifth task"),
starts=
as.POSIXct(strptime(
@lawsofthought
lawsofthought / power_analysis.R
Last active February 15, 2018 09:41
R scripts to calculate power in an independent samples t-test
library(dplyr)
library(ggplot2)
library(tidyr)
library(pwr)
sample_sizes <- seq(5, 100, by=5)
effect_sizes <- c(0.1, 0.3, 0.5)
sample_one <- function(sequence){
sample(size=1, sequence)
@lawsofthought
lawsofthought / pyRMarkdown.Rmd
Created December 29, 2017 22:05
A RMarkdown example illustrating the limitations of Python code in RMarkdown documents
---
title: "Python code chunks in RMarkdown"
author: "Mark Andrews"
date: "29 December 2017"
output: pdf_document
---
Python and R code chunks in RMarkdown do not work the same. The R code chunks are all interpreted in one global environment, but the Python chunks are interpreted in independent environments. Also, I don't think there is any way to have Python inline code.
The following chunks try to illustrate this.
@lawsofthought
lawsofthought / readwrite_pkl.py
Created June 26, 2017 17:03
A Python script to read/write pickle files
import cPickle as pickle
ongoing_list_dict = dict(subject_1 = 7,
subject_7 = 42,
subject_101 = 3)
# Write the dictionary to file
with open('ongoing_list.pkl', 'wb') as f:
pickle.dump(ongoing_list_dict, f, protocol=2)
@lawsofthought
lawsofthought / boxbeanpirateplots.R
Created April 26, 2017 20:11
A R script to illustrate how to make box, bean, pirate, etc, plots
# Some R code to demo boxplots, beanplots, pirateplots, etc
library(psych)
library(dplyr)
library(tidyr)
library(beanplot)
library(yarrr)
# Load the cushny data
data(cushny)
@lawsofthought
lawsofthought / background_colors.R
Last active March 30, 2016 20:01
An R function to create a plot with side by side panels of different colors and a random walk line plot on top.
####################################
example.colors <- c('cyan',
'darkgoldenrod',
'cornflowerblue',
'chocolate1',
'chartreuse1')
myplot <- function(N, example.colors, drift=0.1, switch.p=0.01){
K <- length(example.colors)