Skip to content

Instantly share code, notes, and snippets.

@thomasjensen
thomasjensen / simple.r
Created December 5, 2011 15:59
simpleR
x <- c(1,2,3)
plot(x)
@thomasjensen
thomasjensen / interaction.r
Created December 5, 2011 23:43
Interpret Interaction Terms
#generate data
library(MASS)
covvar <- matrix(c(1,.6,.6,.6,1,.6,.6,.6,1),nr = 3)
means <- c(20,25,30)
data <- as.data.frame(mvrnorm(100,means,covvar))
#dichotomize the dependent variable
data$V1 <- ifelse(data$V1 > 20,1,0)
@thomasjensen
thomasjensen / simulated_effect.r
Created December 6, 2011 11:29
Simulate confidence intervals
library(MASS)
library(ggplot2)
#generate the fake data
covvar <- matrix(c(1,.6,.6,.6,1,.6,.6,.6,1),nr = 3)
means <- c(20,25,30)
data <- as.data.frame(mvrnorm(100,means,covvar))
#dichotomize the dependent variable
data$V1 <- ifelse(data$V1 > 20,1,0)
@thomasjensen
thomasjensen / epOutliers.r
Created December 6, 2011 22:32
Outliers in the EP
#read the ggplot2 library
library(ggplot2)
#read the data
mepDat <- read.csv("ep6.csv")
#plot the density for the questions variable
plot <- ggplot(mepDat, aes(x = Questions))
plot <- plot + geom_density()
plot <- plot + geom_segment(aes(x = median(mepDat$Questions), xend = median(mepDat$Questions), y = 0, yend = .031), linetype = 2)
@thomasjensen
thomasjensen / tikzggplot.r
Created December 7, 2011 10:56
Use tkizDevice with ggplot2
library(tikzDevice)
library(ggplot2)
y <- exp(seq(1,10,.1))
x <- 1:length(y)
data <- data.frame(x = x, y = y)
tikz(file = "test.tex")
plot <- ggplot(data, aes(x = x, y = y)) + geom_line()
@thomasjensen
thomasjensen / tikz.tex
Created December 7, 2011 11:00
Include output from tikzDevice in LaTeX
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}[ht]
\input{test.tex}
\caption{Sample output from tikzDevice}
\end{figure}
@thomasjensen
thomasjensen / applyexample.r
Created December 13, 2011 16:49
How to feed a function to the apply function
data <- cbind(rnorm(100),rnorm(100),rnorm(100))
outlier <- function(xrow){
m <- mean(xrow)
devs <- abs(xrow - m)
pos <- which.max(devs)
val <- devs[pos]
out <- c(pos,val)
return(out)
}
@thomasjensen
thomasjensen / vectorize.r
Created December 21, 2011 08:05
vectorized function for getting outliers in rows of a matrix
data <- cbind(rnorm(100),rnorm(100),rnorm(100))
outlierMat <- function(mat) {
m <- rowMeans(mat)
devs <- abs(mat - m)
val <- apply(mat, 1, max)
pos <- which(mat == val, arr.ind = TRUE)
out <- cbind(pos,val)
return(out)
}
@thomasjensen
thomasjensen / ft.r
Created December 21, 2011 21:39
first look at voting data from the Danish parliament
library(plyr)
library(ggplot2)
setwd("/.../")
data <- read.csv("ft.csv")
data.final <- data[data$Amendment == 0,]
data.amendment <- data[data$Amendment == 1,]
activity <- ddply(data.final,c("Year","Month"),function(x) data.frame(count = length(unique(x$Voteid))))
@thomasjensen
thomasjensen / download.py
Created January 5, 2012 00:15
Download blog posts from R-bloggers
from BeautifulSoup import BeautifulSoup
import mechanize
import time
url = "http://www.r-bloggers.com/"
br = mechanize.Browser()
page = br.open(url)