Skip to content

Instantly share code, notes, and snippets.

View wabarr's full-sized avatar

Andrew wabarr

View GitHub Profile
compound <- function(start, growthrate_percent, fees_percent, years=15) {
amount <- start
growthrate_proportion <- growthrate_percent/100
fees_proportion <- fees_percent/100
yearly_vals <- rep(NA, years)
for(year in 1:years) {
amount <- amount*(1+growthrate_proportion) - amount*fees_proportion
yearly_vals[year] <- amount
}
plot(x=1:years,y=yearly_vals, pch=16, cex=2, ylab="total value in dollars", xlab="year", main=sprintf("growing at %.3f%% with fees of $%.3f%%\nstarting value: $%.2f\nending value: $%.2f",growthrate_percent, fees_percent,start, amount))
library(factoextra)
PCA <- prcomp(iris[,1:4], scale=T)
fviz(PCA, "var")
fviz(PCA, "ind", repel=T, habillage = iris$Species, label='none')
fviz_ellipses(PCA,habillage = iris$Species, ellipse.type = "convex", geom="point") +
labs(title="mytitle")
from mesa.datacollection import DataCollector
class MyDataCollector(DataCollector):
## subclass DataCollector to only collect data on certain agents
## in this case, I only report them if they are NOT alive
## self.alive is an attribute that I track for my agents
def collect(self, model):
""" Collect all the data for the given model object. """
if self.model_reporters:
for var, reporter in self.model_reporters.items():
@wabarr
wabarr / export_eps.R
Last active March 15, 2016 20:19
simple function to export a plot to EPS, with possible options
## updated with input from @richfitz and @hadley - Thanks, guys!!
export_eps <- function(filename, expression, ...){
old <- setEPS()
on.exit(do.call(ps.options, old), add = TRUE)
postscript(filename, ...)
on.exit(dev.off(), add = TRUE)
expression
}
@wabarr
wabarr / kmeans.md
Created September 19, 2015 13:51
testing if kmeans clustering is deterministic or not

kmeans testing

Andrew Barr
September 19, 2015

library(ggplot2)

groups <- factor(rep(c("A", "B"), 100))
x <- rnorm(200) 
@wabarr
wabarr / timeseries.md
Last active September 8, 2015 15:18
correlation between two random walk time series, that happen to be trended

TimeSeries

Andrew Barr
July 6, 2015

create totally random time series

ts1 <- cumsum(rnorm(1000))
ts2 &lt;- cumsum(rnorm(1000))
@wabarr
wabarr / gist:850c230762814ea9b086
Last active August 29, 2015 14:23
celebratory rainbow
library(plotrix)
cols <- c(rainbow(6),"white")
radii <- seq(from=1, to=0.5, length.out = length(cols))
plot(0,0,ylim=c(0,9), type = "n")
lapply(1:length(cols), FUN=function(index){
draw.circle(0,0,radius=radii[index], col=cols[index])
})
@wabarr
wabarr / backup.sh
Last active August 29, 2015 14:01
shell script to check if sqlite database has changed, and to email the backup file if so
#get the current md5sum, and use awk to strip out filename, which is reuturned as well
#note, this assumes that md5sum_of_database_at_last_backup.txt exists and contains the hash of db at last backup
#currently no error checking
currentMD5=$(md5sum test.db | awk '{print $1}')
lastMD5=$(cat /home/user/md5sum_of_database_at_last_backup.txt)
if [ "$currentMD5" == "$lastMD5" ]
then
@wabarr
wabarr / texture.R
Created November 12, 2013 19:28
code to make an image with nice texture in ggplot
library(ggplot2)
library(reshape2)
n <- 7000
theMean <- 0
theSD <- 5
A <- rbinom(n,1,c(.5,.5)) + rnorm(n,theMean,theSD)
B <- rbinom(n,1,c(.5,.5)) + rnorm(n,theMean,theSD)
C <- rbinom(n,1,c(.5,.5)) + rnorm(n,theMean,theSD)
modelID <- 1:n