Skip to content

Instantly share code, notes, and snippets.

@mattbaggott
mattbaggott / plyr_NOMINATE.R
Created October 27, 2012 16:18 — forked from dsparks/plyr_NOMINATE.R
Visualizing polarization in congress
# minor changes from script by dsparks
# Letting plyr do the work for us.
#
# Install and load needed packages
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("foreign", "plyr", "Hmisc", "ggplot2")
toInstall <- toInstall[!toInstall%in%library()$results[,1]] #check if present
if(doInstall&length(toInstall)!=0)
{install.packages(toInstall, repos = "http://cran.r-project.org")}
@mattbaggott
mattbaggott / Gastwirth.R
Created November 2, 2012 06:09
gastwirth estimator, weighted median
gastwirth <- function(x,...){
# gastwirth's location estimator
# discussed in Ronald Pearson's Exploring Data in Engineering, the Sciences, and Medicine
# and at http://exploringdatablog.blogspot.com/2012/03/gastwirths-location-estimator.html
ordstats = quantile(x, probs=c(1/3,1/2,2/3),...)
weights = c(0.3,0.4,0.3)
sum(weights*ordstats)
#
}
@mattbaggott
mattbaggott / watercolorplot.R
Created November 6, 2012 07:17
Visually weighted regression / Watercolor plots by Felix Schönbrodt
# Copyright 2012 Felix Schönbrodt
# All rights reserved.
#
# FreeBSD License
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
@mattbaggott
mattbaggott / Turnbull.R
Created November 12, 2012 15:53
Turnbull's nonparametric estimator for interval-censored data
cria.tau <- function(data){
# Giolo, Suely Ruiz. "Turnbull's nonparametric estimator for interval-censored data'."
# Department of Statistics, Federal University of Paraná (2004): 1-10.
l <- data$left
r <- data$right
tau <- sort(unique(c(l,r[is.finite(r)])))
return(tau)
}
@mattbaggott
mattbaggott / washer.R
Created November 16, 2012 15:33
function implementing Andrea Venturini's washer method for outlier detection of time-series
###############################################################################################
## Function washer.AV in R-language
## Original Author : Andrea Venturini ([email protected])
## Venturini, A. (2011). Time Series Outlier Detection: A New Non Parametric Methodology
## (Washer). Statistica 71: 329-344.
################################################################################################
washer.AV = function( dati ) # p t i y
{ # dati structure: phenom./date/series/values/... other
# example: Phenomenon Time Zone Value ...
# ----------- -------- -- ----- --------
@mattbaggott
mattbaggott / arch_test.R
Created November 18, 2012 04:28
Engle's AutoRegressive Conditional Heteroskedasticity (ARCH) estimator
arch_test <- function (x, order=10){
# AutoRegressive Conditional Heteroskedasticity (ARCH) estimator
xsq <- x^2
nobs <- length(x)
inds <- outer(0:(nobs - order - 1), order:1, "+")
xmat <- xsq[inds]
dim(xmat) <- dim(inds)
xreg <- lm(xsq[-1:-order] ~ xmat)
summary(xreg)$r.squared * (nobs - order)
}
@mattbaggott
mattbaggott / lsos.R
Created November 22, 2012 12:39
improved list of objects
# improved list of objects
.ls.objects <- function (pos = 1, pattern, order.by,
decreasing=FALSE, head=FALSE, n=5) {
napply <- function(names, fn) sapply(names, function(x)
fn(get(x, pos = pos)))
names <- ls(pos = pos, pattern = pattern)
obj.class <- napply(names, function(x) as.character(class(x))[1])
obj.mode <- napply(names, mode)
obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class)
obj.size <- napply(names, object.size)
@mattbaggott
mattbaggott / predictingbreaks.R
Last active October 13, 2015 17:28
short comparison of methods of finding breakpoints in a specific dataset
#######################
#
# short investigation of id'ing breakpoints in data
#
# [email protected]
library("bcp")
library("ggplot2")
library("changepoint")
library("reshape")
@mattbaggott
mattbaggott / demo_distline.R
Created December 13, 2012 18:30
demonstration of plotting distributions overlapping lineplots in R
##
## demonstration of distributions overlapping on lineplots
## [email protected]
## Dec 11, 2012
##
## in response to: http://stats.stackexchange.com/questions/45591/r-plot-time-indexed-densities/45614#45614
library(ggplot2) # for plotting
library(lubridate) # for getting year from dates in dataset
library(plyr) # for getting annual mean easily
@mattbaggott
mattbaggott / demo_timezone.R
Last active October 14, 2015 00:18
Short demo on successfully working with timezones in R
##
## Sample code to teach about timezones in R
## [email protected]
## Dec 13, 2012
## Time zones are important to understand when working with dates
## because the most common date class for R, POSIX, is actually a date/time
## class. (POSIX = "Portable Operating System Interface,
## an IEEE standard)
##