install.packages("ggplot2") library(ggplot2) #================================================ # 1. One Sample T-test bu<-c(1283,1102,1149,1473,1263,1082,976,1154,1217, 1344) Ho: U=1200 Ha: U#1200 alpha=0.05 (conf.level=1-0.05) t.test(x=bu,y=NULL,alternative="two.sided",mu=1200, conf.level=0.95) #=============================================================== # 2. Paired T-Test x<-c(247, 231, 246, 216, 242, 253, 254, 233, 253, 244) y<-c(331, 369, 366, 358, 353, 341, 341, 380, 356, 409) Ho: Ux-Yy=0 (Pre and post trx average CD4 T cells are the same) Ha: Ux-Uy#0 (Pre and post trx average CD4 T cells are not the same) #ploting data is.factor(Time) Time<-as.factor(Time) Patient<-c(as.factor(1:10),as.factor(1:10)) Cd4<-c(x,y) dat2<-data.frame(Patient,Time,Cd4) dim(dat2) head(dat2) pair<-ggplot(dat2, aes(x =Time, y =Cd4)) +geom_line(aes(group=Patient, size=0.10,color=factor(Patient)))+ scale_x_discrete(limits=rev(levels((dat2$Time))))+ylab("CD4+ T Cell Counts") pair+theme(legend.position="none")#removing title #Save the Graph # getwd() #check your current working directory #setwd("C:/Users/musie/Courses/Biostat1") #change the working directory # pdf('pairt.pdf', width=8, height=12) # save the plot as pdf #Testing data t.test(x,y,alternative="two.sided",mu=0,paired=TRUE, conf.level=0.95) ############################################################################# 3. Two Sample T-test # CD4+ T-cell counts data z1<-c(631, 508, 435, 578, 618, 557, 611, 568, 537, 656) z2<-c(435, 552, 616, 507, 553, 741, 656, 555, 501, 493) Ho: Uz1=Uz2 (Average CD4 T cells are the same in both groups) Ha: Uz1#Uz2 (Average CD4 T cells are not the same in both groups) # Plot the data Group<-c(rep("Standard",10),rep("Experimental",10)) cd4<-c(z1,z2) dat<-data.frame(cd4,Group) dim(dat) head(dat) t.2<-ggplot(dat, aes(x=Group, y=cd4,color=Group))+geom_boxplot(aes(fill=Group,color=Group),size=3,width=0.4, alpha=1)+ scale_color_manual(values=c("orange","purple"))+scale_fill_manual(values=c("orange", "purple"))+xlab("")+ ylab("CD4+ T Cell Counts") t.2+theme(legend.position="none") # Testing t.test(z1,z2,alternative="two.sided",mu=0,paired=FALSE, var.equal=TRUE, conf.level=0.95) ######################################################################## # Anova #Plotting data in tabl12.1 JH<-c(3.23,3.47,1.86,2.47,3.01,1.69,2.10,2.81,3.28,3.36,2.61,2.91,1.98, 2.57,2.08,2.47,2.47,2.74,2.88,2.63,2.53) RLA<-c(3.22,2.88,1.71,2.89,3.77,3.29,3.39,3.86,2.64,2.71,2.71,3.41,2.87, 2.61,3.39,3.17) SL<-c(2.79,3.22,2.25,2.98,2.47,2.77,2.95,3.56,2.88,2.63,3.38,3.07,2.81, 3.17,2.23,2.19,4.06,1.98,2.81,2.85,2.43,3.20,3.53) Ho: The means in the three groups are the sample Ha: The means in the three groups are not the sample # Plot Data Volume<-c(JH,RLA,SL) Centers<-c(rep("JH",21), rep("RLA",16), rep("SL",23)) Table12.1Data<-data.frame(Volume, Centers) head(Table12.1Data) q<-ggplot(Table12.1Data, aes(x=Centers, y=Volume,color=Centers))+geom_boxplot(aes(fill=Centers, color=Centers),size=1, width = 0.3)+geom_jitter(position = position_jitter(width =0),size=3)+scale_color_manual(values=c("orange", "purple", "green"))+scale_fill_manual(values=c("orange", "purple", "green"))+xlab("Center")+ylab("Forced Expiratory Volume")+geom_boxplot(aes(fill=Centers, color=Centers),size=3, width = 0.4) q+theme(legend.position="none") #Test anova.test<-aov(Volume~Centers, data=Table12.1Data) summary(anova.test) #################################################### #anova #Another Example A<-c(6.9,5.4,5.8,4.6,4.0) B<-c(8.3,6.8,7.8,9.2,6.5) C<-c(8,10.5,8.1,6.9,9.3) # Create a vector with all the distances from the three groups Distance<-c(A,B,C) # Create a vector with a list of 5 A's, 5 B's \& 5 C's Teams<-c(rep("A",5),rep("B",5), rep("C",5)) # Create a data frame with distance \& team information dat<-data.frame(Distance, Teams) head(dat) # Plot data q<-ggplot(dat, aes(x=Teams, y=Distance,color=Teams))+geom_boxplot(aes(fill=Teams, color=Teams),size=1, width = 0.3)+geom_jitter(position = position_jitter(width =0),size=3)+scale_color_manual(values=c("orange", "purple", "green"))+scale_fill_manual(values=c("orange", "purple", "green"))+xlab("Teams")+ylab("Distance")+geom_boxplot(aes(fill=Teams, color=Teams),size=3, width = 0.4) q+theme(legend.position="none") # Test hypotheses results<-aov(Distance~Teams, data=dat) summary(results) # where is the difference coming from? # Adj for multiple test pairwise.t.test(Distance, Teams, p.adjust="bonferroni")