Skip to content

Instantly share code, notes, and snippets.

View jeroen's full-sized avatar

Jeroen Ooms jeroen

View GitHub Profile
#!/bin/bash
IPADDR=""
if [ -f /usr/bin/ec2metadata ]
then
IPADDR=`timeout 1 ec2metadata --public-hostname`
fi
if [ "$IPADDR" = "" ]
then
@jeroen
jeroen / 001-minimal.Rmd
Last active December 18, 2015 17:09
knitr examples
# A minimal R Markdown example
A quote:
> Markdown is not LaTeX.
To compile me, run this in R:
library(knitr)
knit('001-minimal.Rmd')
@jeroen
jeroen / glmnet.R
Created July 16, 2013 18:35
Example from glmnet package
# Example taken from glmnet
library(glmnet)
# Gaussian
x=matrix(rnorm(100*20),100,20)
y=rnorm(100)
fit1=glmnet(x,y)
print(fit1)
coef(fit1,s=0.01) # extract coefficients at a single value of lambda
predict(fit1,newx=x[1:10,],s=c(0.01,0.005)) # make predictions
library(quantmod)
symetf = c('XLY','XLP','XLE','XLF','XLV','XLI','XLB','XLK','XLU','SPY', "GOOG")
end<- format(Sys.Date(),"%Y-%m-%d")
start<-format(Sys.Date() - 365,"%Y-%m-%d")
dat0 = (getSymbols(symetf[1], src="google", from=start, to=end, auto.assign = F, warnings = FALSE,symbol.lookup = F))
n = NROW(dat0) ; l = length(symetf)
dat = array(dim = c(n,NCOL(dat0),l)) ; ret = matrix(nrow = n, ncol = l)
for (i in 1:l){
dat0 = (getSymbols(symetf[i], src="google", from=start, to=end, auto.assign = F,warnings = FALSE,symbol.lookup = F))
dat[1:n,,i] = dat0
@jeroen
jeroen / test.Rmd
Created August 28, 2013 13:23
Markdown test with math and figure
The Normal Distribution
=======================
The normal distribution is defined as follows:
$$latex
f(x;\mu,\sigma^2) = \frac{1}{\sigma\sqrt{2\pi}} e^{ -\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2 }
$$
To generate random draws from a normal distribution we use the **rnorm** function:
@jeroen
jeroen / test1.txt
Created November 14, 2013 20:08
nested gist
This is a test
<!DOCTYPE html>
<html lang="en">
<head>
<title>OpenCPU demo app</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- ocpu library -->
<script src="//code.jquery.com/jquery-1.10.2.min.js"> </script>
<script src="//public.opencpu.org/js/opencpu-0.4.js"> </script>
#Assume httr 0.4
library(httr)
stopifnot(packageVersion("httr") >= "0.4")
#submit a package to CRAN, step 1
submit <- function(name, email, pkg, comment = "Thank you", popup = TRUE){
stopifnot(file.exists(pkg))
req1 = httr::POST (
url = "http://xmpalantir.wu.ac.at/cransubmit/index2.php",
encode = "multipart",
@jeroen
jeroen / weather.R
Last active August 29, 2015 14:05
Batch parsing JSON streams with jsonlite
# The data stream is not JSON itself. It contains lines with JSON data. Therefore
# this example manually collapses it into a JSON array. The file is also quite
# large, so we would need to do some batch processing by splitting the file before
# feeding it to R. This example shows how to read first 20 records.
library(jsonlite)
gzdata <- gzcon(url("http://78.46.48.103/sample/hourly_14.json.gz"))
records <- readLines(gzdata, n = 20)
close(gzdata)
json <- paste0("[", paste0(records, collapse=","), "]")
@jeroen
jeroen / deparse_vector.R
Created September 1, 2014 17:00
Performance comparison of deparsing character vectors
# Version 1
deparse_vector1 <- function(x) {
stopifnot(is.character(x))
vapply(x, deparse, character(1), USE.NAMES=FALSE)
}
# Version 2
deparse_vector2 <- function(x) {
stopifnot(is.character(x))
if(!length(x)) return(x)