################################################### ### chunk number 1: setup ################################################### #line 31 "/home/jimrc/classes/stat505/notes/introR3.Rnw" options(width=60) ################################################### ### chunk number 2: ################################################### #line 40 "/home/jimrc/classes/stat505/notes/introR3.Rnw" methods(plot) ################################################### ### chunk number 3: ################################################### #line 45 "/home/jimrc/classes/stat505/notes/introR3.Rnw" getAnywhere("stack.data.frame") getS3method("plot","factor") require(arm) ## load the books functions getMethod("display","lm") ## See G&H p 38-9 ################################################### ### chunk number 4: ################################################### #line 68 "/home/jimrc/classes/stat505/notes/introR3.Rnw" Summrize <- function(x){ ## verbs are preferred function names ## function to plot BWplot, return fivenum, mean, sd boxplot(x, horizontal=TRUE) c( fivenum(x), mean(x), sd(x)) ## last thing created is returned } Summrize(rexp(50)) ################################################### ### chunk number 5: ################################################### #line 83 "/home/jimrc/classes/stat505/notes/introR3.Rnw" Summrize <- function(x){ s## function to plot BWplot, return fivenum, IQR, mean, sd ## check for numeric data if(!is.numeric(x)) stop("Data must be numeric") ## or convert it boxplot(x, horizontal=TRUE) list(fiveNum=fivenum(x),IQR=IQR(x),mean=mean(x),SD=sd(x)) } # Summrize(rnorm(50,20,5)) ################################################### ### chunk number 6: ################################################### #line 97 "/home/jimrc/classes/stat505/notes/introR3.Rnw" Summrize <- function(x,...){ ## function to plot BWplot, return fivenum, IQR, mean, sd x <- as.numeric(x) ## forced conversion to numeric boxplot(x, ... ) ## pass the extras on to boxplot list(fiveNum=fivenum(x),IQR=IQR(x),mean=mean(x),SD=sd(x)) } # Summrize(rnorm(50,20,5), col="yellow") # Summrize(rnorm(50,20,5), col="yellow",horizontal=TRUE) ################################################### ### chunk number 7: ################################################### #line 113 "/home/jimrc/classes/stat505/notes/introR3.Rnw" test.means <- function(y1, y2=NULL, paired=FALSE) { ## see t.test for a better function to test means n1 <- length(y1) if(is.null(y2)){ tst <- mean(y1)/sd(y1) * sqrt(n1) } else if(paired) tst <- test.means(y1-y2,NULL) else { n2 <- length(y2) yb1 <- mean(y1); yb2 <- mean(y2) v1 <- var(y1); v2 <- var(y2) sd <- sqrt((n1-1)*v1 + (n2-1)*v2)/(n1+n2-2) tst <- (yb1 - yb2)/(sd *sqrt(1/n1 + 1/n2)) } tst } # test.means(rnorm(20)) # test.means(rnorm(20,3,4),rnorm(20,7,4)) # test.means(rnorm(20,3,4),rnorm(20,7,4),TRUE) ################################################### ### chunk number 8: ################################################### #line 142 "/home/jimrc/classes/stat505/notes/introR3.Rnw" ruby <- read.csv("../data/Ruby-AllFish.csv",head=TRUE) ## ruby.Ghrn.RBT2006 <- subset(ruby, species=="RBT" & site=="Ghorn" & year==2006 & length >100 ) #summary(rubyRBT2006) out1 <- with(ruby.Ghrn.RBT2006, table(cut(length, seq(100,475,25)),mark,trip)) ## problem: trip 1 is never marked cbind(out1[,,1],out1[,,2]) ################################################### ### chunk number 9: ################################################### #line 159 "/home/jimrc/classes/stat505/notes/introR3.Rnw" ruby.Ghrn.RBT2006$type <- with(ruby.Ghrn.RBT2006, ifelse(trip == 1,"pass1", ifelse(mark == 1,"both","pass2"))) with(ruby.Ghrn.RBT2006, table(cut(length, seq(100,475,25)),type)) ################################################### ### chunk number 10: fun1 ################################################### #line 187 "/home/jimrc/classes/stat505/notes/introR3.Rnw" fishTable <- function(data, yr, specs, ste, minLength=100, maxLength=500, increment=25){ myData <- subset(data, species==specs & site==ste & year==yr & length > minLength & length < maxLength ) type <- with(myData, ifelse(trip == 1, "pass1", ifelse(mark == 1, "both", "pass2"))) results <- with(myData, table(cut(length, seq(minLength, maxLength, increment)), type))[,c(2,3,1)] ## reorder columns ## marks are included in pass1, not pass2 results[,2] <- results[,3] + results[,2] results } ################################################### ### chunk number 11: fun2 ################################################### #line 204 "/home/jimrc/classes/stat505/notes/introR3.Rnw" head( ruby.Canyon.RBT2006 <- fishTable(ruby, specs ="RBT" , ste="Can", yr=2006))