Skip to content

Instantly share code, notes, and snippets.

View certifiedwaif's full-sized avatar

Mark Greenaway certifiedwaif

  • University of Sydney
  • Sydney, Australia
View GitHub Profile
@certifiedwaif
certifiedwaif / gist:3857052
Created October 9, 2012 06:55
lattice panel function to display a segmented regression
# FIXME: There's a bug where if data in one of the groups is shorter than the others, spurious predictions
# from the segmented linear model will be repeated until the number of predictions is the same as the other
# data sequences.
panel.segmented_lm <- function(x, y, groups, subscripts, ...)
{
g = groups[subscripts]
for (group in levels(g)) {
x2 = x[g==group]
y2 = y[g==group]
@certifiedwaif
certifiedwaif / gist:3903829
Created October 17, 2012 05:23
Recode survey data to be consistent across multiple time points
# Recode and score variables ----
# Input: Long data set
# Output: Long data set, with fat, fibre and psychosocial score variables consistently
# coded at baseline, three months and twelve months.
# Explanation: Different people entered the baseline and follow-up survey data, and they
# didn't talk to each other. So the data for some questions got coded differently at
# different timepoints. This is very confusing, and means we can't meaningfully compare
# the data, so I'm writing this code to fix it up.
make_coding_consistent = function(data)
{
@certifiedwaif
certifiedwaif / cronbachs_alpha.R
Created November 21, 2012 04:04
R code to compute Cronbach's alpha, using the formula interface
cronbach_alpha = function(formula, data=NULL)
{
terms = terms(formula, data=data)
variables = attr(terms, "variables")
var_list = eval(variables, envir=data)
var_mat = t(laply(var_list, identity))
var_mat = var_mat[complete.cases(var_mat),]
response_idx = attr(terms, "response")
response = var_mat[,response_idx]
covariates = var_mat[,-response_idx]
The command was find /mnt/Seagate_Expansion4_No2 -name \*sra\* -print0 | parallel --null ./sra_to_fastq.sh {}
The script sra_to_fastq.sh contains:
----
#!/bin/bash -x
outdir=/home/mark/data
echo $1
@certifiedwaif
certifiedwaif / gist:d26bc4a77f7277becd71
Created May 21, 2015 05:38
optim looping endlessly
trace: f.GVA(vtheta_prime, vy, vr, mC, mSigma.inv, gh, mR, Rinds, Dinds)
f.VGA: f 25441.43 vmu 4.18073 0.31266 -0.77725 -1.0205 -0.96467 -0.75745 -0.26577 0.08923 0.63935 0.61445 0.60797 0.30834 -0.23776 -0.40567 diag(mR) 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448
trace: f.GVA(vtheta_prime, vy, vr, mC, mSigma.inv, gh, mR, Rinds, Dinds)
f.VGA: f 25441.43 vmu 4.18073 0.31266 -0.77725 -1.0205 -0.96467 -0.75745 -0.26577 0.08923 0.63935 0.61445 0.60797 0.30834 -0.23776 -0.40567 diag(mR) 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448
trace: f.GVA(vtheta_prime, vy, vr, mC, mSigma.inv, gh, mR, Rinds, Dinds)
f.VGA: f 25441.43 vmu 4.18073 0.31266 -0.77725 -1.0205 -0.96467 -0.75745 -0.26577 0.08923 0.63935 0.61445 0.60797 0.30834 -0.23776 -0.40567 diag(mR) 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448 0.04448
trace: f.GVA(vtheta
@certifiedwaif
certifiedwaif / gist:d75cdf5005764624da7c
Created June 7, 2015 11:19
Construct a histogram of a million random numbers
-- hw3.hs
module Main where
import System.Random
import Data.Vector.Unboxed hiding (forM_, take, unfoldr, map)
import Data.Vector.Unboxed.Mutable hiding (take)
import Data.List
import Control.Monad.ST (runST)
import Control.Monad (forM_)
import Prelude hiding (read)
@certifiedwaif
certifiedwaif / greycode.R
Created February 16, 2016 05:10
I think the intent of this code is perfectly obvious</sarcasm>
greycode <- function(p)
{
A <- matrix(c(0,1),2,1)
if (p!=1) {
for (i in 2:p) {
P <- nrow(A)
inds <- rev(1:P)
A1 <- matrix(c(rep(0,P),rep(1,P)),2*P,1)
A2 <- rbind(A,matrix(A[inds,],nrow(A),ncol(A)))
A <- cbind(A1,A2)
@certifiedwaif
certifiedwaif / optimal_points.cpp
Last active April 23, 2016 08:29
optimal_points.cpp
#include <algorithm>
#include <iostream>
#include <climits>
#include <vector>
#include <algorithm>
#include <string>
// #include <boost/dynamic_bitset.hpp>
using namespace std;
// using namespace boost;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Final//EN">
<html><head>
<title>HardInfo (0.5.1) System Report</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
body { background: #fff }
.title { font: bold 130% serif; color: #0066FF; padding: 30px 0 10px 0 }
.stitle { font: bold 100% sans-serif; color: #0044DD; padding: 30px 0 10px 0 }
.sstitle{ font: bold 80% serif; color: #000000; background: #efefef }
.field { font: 80% sans-serif; color: #000000; padding: 2px; padding-left: 50px }
@certifiedwaif
certifiedwaif / maps.R
Created September 22, 2016 01:37
What's a better way to loop over all combinations of vp, vn and vR2, producing p*n, p and R2 and ending up with an n by 3 data frame or matrix?
library(purrr)
vp <- c(10, 20, 50, 100, 500, 1000)
vn <- c(1.1, 1.25, 1.5, 2, 5, 10)
vR2 <- seq(from=0.00, to=1.00, by = 1e-2)
df <- map(vp, function(p) {
map(vn, function(n) {
map(vR2, function(R2) {c(p*n, p, R2)})
})
}) %>% as.data.frame %>% t