This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ####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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ###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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ##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=""); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |