setwd("/home/bennylab/Documents/Benny/R_Demonstration") # The appropriate directory rm(list=ls()) install.packages("circular", dependencies = TRUE) # BASIC R: # Second chapter of "Introduction to Statistics" # Section 2.3 work.hours <- c(5,6,3,3,2,4,7,5,2,3,5,6,5,4,4,3,5,2,5,3) table(work.hours) as.data.frame(table(work.hours)) freq <- table(work.hours) freq sum(freq) freq/sum(freq) sum(freq/sum(freq)) cumsum(freq/sum(freq)) # Section 2.4 ex.1 <- read.csv(file="ex1.csv") ex.1 # Section 2.5 hist(ex.1$height) ex.1$height # Section 2.6 boxplot(c(1,11.5,6,7.2,4,8,9,10,6.8,8.3,2,2,10,1)) summary(ex.1$height) boxplot(ex.1$height) # Section 2.7 x <- c(4,5,6,6,6,7,7,7,7,7,7,8,8,8,9,10) mean(x) median(x) x <- c(4,5,6,6,6,7,7,7,7,7,7,8) mean(x) median(x) x <- c(6,7,7,7,7,7,7,8,8,8,9,10) mean(x) median(x) mean(ex.1$height) median(ex.1$height) # Section 2.8 x <- c(9,9.5,9.5,10,10,10,10,10.5,10.5,10.5,10.5,11,11,11,11,11, 11,11.5,11.5,11.5) length(x) x.bar <- mean(x) x.bar x - x.bar (x - x.bar)^2 sum((x - x.bar)^2)/(length(x)-1) sqrt(sum((x - x.bar)^2)/(length(x)-1)) var(x) sd(x) # STANDARD ANALYSIS # DEMONSTRATE USE OF A PACKAGE # Input data from tab-deliminated ascii file data <- read.table(file="data73.txt",header=TRUE) d3.temp <- data$m3d d7 <- data$m7d data <- read.table(file="data13.txt",header=TRUE) d3 <- data$m3d d1 <- data$m1d d3 <- d3[!is.na(d3)] any(d3 != d3.temp[!is.na(d3.temp)]) # Call package and format data for the functions of the library library(circular) d1 <- circular(2*pi*d1/24) d3 <- circular(2*pi*d3/24) d7 <- circular(2*pi*d7/24) # Summary (Problem with the plotting = does not plot points with the value zero) plot(d1,col="red",stack=TRUE,main="Max bloom") points(d3,col="blue",stack=TRUE) points(d7,col="green",stack=TRUE) legend(-1.15,1,pch=c(1,1,1),col=c("red","blue","green"),legend=c(paste("d",c(1,3,7),sep=""))) summary(d1) summary(d3) summary(d7) # test rao.test(d1,d3) rao.test(d1,d7) rao.test(d3,d7) # MORE COMPLEX APPLICATION # See the The American Journal of Human Genetics paper