Skip to content

Instantly share code, notes, and snippets.

View CerebralMastication's full-sized avatar
☠️
brilliantly executing terrible plans

JD Long CerebralMastication

☠️
brilliantly executing terrible plans
View GitHub Profile
@CerebralMastication
CerebralMastication / bristowCampbell.R
Created February 15, 2011 21:30
implementation of BC in R: calls Java code for actual logic
library(rJava)
.jinit()
.jaddClassPath(dir( "/home/jal/Documents/DSSAT/gsrad/gsrad_sample/lib/", full.names=TRUE ))
attach( javaImport( c("java.lang", "java.io")))
BristowCampbellStrategy <- function(b, ClearSkyTransmissivity, ExtraTerrestrialRadiation, SlopeAspectFactor, AirTemperatureDailyRange, AirTemperatureMonthlyRange ) {
rd <- .jnew( "cra/clima/gsrad/RadData" )
algo <- .jnew( "cra/clima/gsrad/GSRBristowCampbellStrategy" )
bcParams <- .jnew( "cra/clima/gsrad/BristowCampbellParameters" )
@CerebralMastication
CerebralMastication / convert to normal.R
Created January 25, 2011 13:09
example of converting a non-normal dist to normal
## make some non norm data
d1 <- rnorm(20, 5, 1)
d2 <- rnorm(5, 10, 2)
d <- c(d1,d2)
hist(d)
## no mean adjustment
myUniform <- ecdf( d )( d )
mean( myUniform )
## hey that mean's not .5!
n <- 1e7
myDf <- data.frame( a=rnorm(n), b=rnorm(n), c=rnorm(n) )
myDf <- rbind(myDf, myDf)
# memory use doubles every time the rbind() runs.
# see what happens if you keep running the this over and over
# I have 4GB of RAM in my laptop running 64 bit Ubuntu.
# R gets killed after 2 rbind()
myDf <- rbind(myDf, myDf)
estimatePi <- function(seed){
set.seed(seed)
numDraws <- 1e6
r <- .5 #radius... in case the unit circle is too boring
x <- runif(numDraws, min=-r, max=r)
y <- runif(numDraws, min=-r, max=r)
inCircle <- ifelse( (x^2 + y^2)^.5 < r , 1, 0)
return(sum(inCircle) / length(inCircle) * 4)
}
@CerebralMastication
CerebralMastication / awsExampleRjava.R
Created November 30, 2010 19:31
Example of how to control AWS from R
library(rJava)
.jinit()
awsAccessKeyText <- "yourKeyHere"
awsSecretKeyText <- "yourSecretKeyHere"
pathToSdk <- "/home/jal/aws-java-sdk-1.1.0/"
testFile <- "/home/jal/aws-java-sdk-1.1.0/rJavaExamples/testFile.tst"
.jaddClassPath(paste(pathToSdk, "lib/aws-java-sdk-1.1.0.jar", sep=""))
.jaddClassPath(paste(pathToSdk, "third-party/commons-logging-1.1.1/commons-logging-1.1.1.jar", sep=""))
# Thanks to @greghirson who totally knocked this out of the park!
# Based on his work, here's a simple script to pull monthly prices from the USDA NASS website.
library(RCurl)
library(XML)
x <- postForm(uri = "http://quickstats.nass.usda.gov/uuid/encode",
source_desc = "SURVEY", sector_desc="CROPS",
group_desc="FIELD CROPS", commodity_desc="CORN",
statisticcat_desc="PRICE RECEIVED",
@CerebralMastication
CerebralMastication / test.csv
Created November 8, 2010 16:13
testing how to get R to read a csv over ssl
a b
1 2
2 3
3 4
4 5
set.seed(2)
x <- 1:100
y <- 20 + 3 * x
e <- rnorm(100, 0, 60)
y <- 20 + 3 * x + e
plot(x,y)
yx.lm <- lm(y ~ x)
lines(x, predict(yx.lm), col="red")
# multivariate normal example using Cholesky decomposition
require(Matrix)
#make this reproducible
set.seed(2)
#how many draws in our starting data set?
n <- 1e4
# multivariate normal example using the MASS package
require(MASS)
#make this reproducible
set.seed(2)
#how many draws in our starting data set?
n <- 1e4