Skip to content

Instantly share code, notes, and snippets.

View Protonk's full-sized avatar
🗯️
Après moi, la brisure

Adam Hyland Protonk

🗯️
Après moi, la brisure
View GitHub Profile
@Protonk
Protonk / piday.R
Created March 15, 2011 06:12
Simple, but not too simple estimation of pi
pi.ratio<- function(n) {
x<- runif(n, min=-1,max=1)
y<- runif(n, min=-1,max=1)
return(4*sum(x^2 + y^2 <= 1.0)/n)
}
pi.est<- function(iter) {
sum.pi<-stor.rat<-numeric(0)
for (i in 1:iter) {
stor.rat[i]<- pi.ratio(1000)
@Protonk
Protonk / pi day plus 1.R
Created March 15, 2011 20:58
More fooling around with pi
#Same idea as yesterday, but in one function with two arguments
pi.est<- function(iter,n) {
pi.ratio<- function(n) {
x<- runif(n, min=-1,max=1)
y<- runif(n, min=-1,max=1)
return(4*sum(x^2 + y^2 <= 1.0)/n)
}
sum.pi<-stor.rat<-numeric(0)
for (i in 1:iter) {
stor.rat[i]<- pi.ratio(n)
@Protonk
Protonk / fukushima.R
Created March 24, 2011 19:53
simple plot for radiation data
rad<-read.csv(file="~/Downloads/radtext.csv",as.is=TRUE)
radplot<- ggplot(data=rad,aes(x=1:619,y=cumsum(microSv))) + geom_line(size=I(1),linetype=I(2)) + scale_y_log10(name="microSieverts") + scale_x_continuous(labels=c(rad$time[seq(from=100,to=600,by=100)]),breaks=c(seq(from=100,to=600,by=100)),name='')
radplot<- radplot + geom_line(aes(x=1:619,y=microSv),colour="blue") + geom_hline(aes(yintercept=50000),colour="red")
radplot<- radplot + annotate(geom="text",x=575,y=35000,label="Yearly Limit",colour="red") + annotate(geom="text",x=575,y=1000,label="Hourly Dose",colour="blue") + annotate(geom="text",x=575,y=200000,label="Cumulative")
@Protonk
Protonk / redditplot.R
Created March 27, 2011 03:02
plot for a reddit question
####Context: http://www.reddit.com/r/statistics/comments/gc7zn/how_do_you_quantify_the_goodness_of_a_fit/
#dput() is freakin' awesome
slope<-structure(list(x0 = c(90L, 90L, 90L, 75L, 75L, 75L, 75L, 60L,
60L, 60L, 30L, 30L, 30L, 30L, 90L, 90L, 90L, 75L, 75L, 75L, 75L,
60L, 60L, 60L, 30L, 30L, 30L, 30L), y0 = c(0.87355252855632,
0.96510036804794, 0.99961155254449, 0.83250156448838, 0.90994111936252,
0.81220807703055, 0.88450841597365, 0.93325151195878, 0.88359117143352,
0.85099464726141, 0.88641984414472, 0.88585837018634, 0.86312678397544,
0.87395793611538, 0.95257033724407, 0.96405226327959, 1.24511295032759,
@Protonk
Protonk / ivest.R
Created March 29, 2011 22:12
Very simple (and flawed) IV estimation
library(AER)
library(lmtest)
data("CollegeDistance")
cd.d<-CollegeDistance
simple.ed.1s<- lm(education ~ distance,data=cd.d)
cd.d$ed.pred<- predict(simple.ed.1s)
simple.ed.2s<- lm(wage ~ urban + gender + ethnicity + unemp + ed.pred , data=cd.d)
simple.comp<- encomptest(wage ~ urban + gender + ethnicity + unemp + ed.pred , wage ~ urban + gender + ethnicity + unemp + education , data=cd.d)
1s.ftest<- encomptest(education ~ tuition + gender + ethnicity + urban , education ~ distance , data=cd.d)
@Protonk
Protonk / religion reddit.R
Created April 1, 2011 01:03
Another plot for a reddit question
###Simple regression ####
#Read.csv just turns a csv into a data-frame and I assign it to the name "religion" w/
# the <- command. Your file path will be different, of course. I cleaned up the
#data in google refine first, removing a wierd leading character in the
#country column and the % sign in the religion column
religion<-read.csv(file="~/Downloads/Religion.csv",as.is=TRUE)
#assigns names to the columns
names(religion)<- c("country", "hdi", "religion", "murder")
@Protonk
Protonk / weedprice.R
Created April 12, 2011 16:53
Scraped data on user reported Marijuana Prices
##Libraries needed for graphing and scraping. Reshape is loaded when ggplot2 is loaded
library(XML)
library(ggplot2);
#There are probably smarter ways to do this, but this works well enough
url.prefix<-"http://www.priceofweed.com/prices/United%20States/"
url.suffix<-".html"
state.uri<- paste(url.prefix,state.name,url.suffix,sep="");
@Protonk
Protonk / scrapeweed.R
Created April 12, 2011 17:53
just the facts ma'am
library(XML)
#There are probably smarter ways to do this, but this works well enough
url.prefix<-"http://www.priceofweed.com/prices/United%20States/"
url.suffix<-".html"
state.uri<- paste(url.prefix,state.name,url.suffix,sep="");
#This function is quick and dirty (emphasis on dirty). It works as well
#as it does because priceofweed.com has very structured web pages. The first
@Protonk
Protonk / redditreg.R
Created April 27, 2011 01:36
another reddit regression
set.seed(20900)
#Same stuff as in the reddit thread.
x1 <- rt(500,df=5)
x2 <- 3 + 5*x1 + rnorm(500,0,3)
x3 <- 6 + rnorm(500)
x4 <- 4 + 4*rt(500,df=15)
w <- rnorm(500,0,10)
#Create the "dependent" variable
y <- 10 + 2*x1 + 7*x2 + 17*x3 + w
@Protonk
Protonk / dlmap.R
Created May 3, 2011 04:48
zomg where'd mah DL go
library(maps)
dl.d<-read.csv(file="~/Downloads/dl dummy.csv",as.is=TRUE)
map('usa')
map("state", boundary = FALSE, lty = 2, add = TRUE)
map("state",region=c(dl.d[dl.d[,2]>0,1]),fill=TRUE,col=3,add=TRUE)