###################################################################### # R commands Stat 704 # Lab 3 University of South Carolina # Normal Data Inference ###################################################################### # This file contains the R commands for the lab. # # Lines beginning with the symbol '#' are comments in R. All other # lines contain code. # # In R for Windows, you may wish to open this file from the menu bar # (File:Display file); you can then copy commands into the command # window. (Use the mouse to highlight one or more lines; then # right-click and select "Paste to console".) ###################################################################### ####### ############# Paired T-test Tumor Example tumor<-c(1.321, 0.841, 1.423, 0.932, 2.682, 2.011, 0.934, 0.762, 1.230, 0.991, 1.670, 1.120, 3.201, 2.312) fid<-rep(c(1:7),each=2) group<-rep(c(0, 1), 7) group<-ifelse(group==0, "control", "treatment") df<-data.frame(fid, group, tumor) ggplot(df, aes(x=factor(group), y=tumor, color=factor(fid), shape=factor(group), group = fid)) + geom_point(position = position_jitter(width = .01)) + geom_smooth(method = 'lm', se = FALSE) + labs( x = "x", color = "ID", shape = 'Class' ) ###### paired t test (version1) t.test(tumor ~ group, paired=T, data=df) #### equivalently (version2) control<-df$tumor[group=="control"] trt<-df$tumor[group=="treatment"] d<-control-trt t.test(d) ################################ scores<-c(4.8, 6.4, 6.3, 6.0, 5.4, 5.8, 6.1, 6.3, 5.0, 6.2, 5.6, 5.0, 6.4, 5.8, 5.5, 6.1, 6.0, 6.0, 5.4, 5.8, 6.5, 6.0, 6.1, 4.7, 5.6, 6.1, 5.8, 4.8, 5.9, 5.4, 5.3, 6.0, 5.6, 6.3, 5.2, 6.0, 6.4, 5.8, 4.9, 4.1, 6.0, 6.4, 5.9, 6.6, 6.0, 4.4, 5.9, 6.5, 4.9, 5.4, 5.8, 5.6, 6.2, 6.3, 5.8, 5.9, 6.5, 5.4, 5.9, 6.1, 6.6, 4.7, 5.5, 5.0, 5.5, 5.7, 4.3, 4.9, 3.4, 5.1, 4.8, 5.0, 5.5, 5.7, 5.0, 5.2, 4.2, 5.7, 5.9, 5.8, 4.2, 5.7, 4.8, 4.6, 5.0, 4.9, 6.3, 5.6, 5.7, 5.1, 5.8, 3.8, 5.0, 6.1, 4.4, 3.9, 6.3, 6.3, 4.8, 6.1, 5.3, 5.1, 5.5, 5.9, 5.5, 6.0, 5.4, 5.9, 5.5, 6.0) status<-c(rep("Attended", 63), rep("NotAttend", 47)) dat<-data.frame(scores, status) str(dat) ###### plot side-by-side dotplot pdf("/Users/yen-yiho/Desktop/STAT704/Notes/Lecture2-RandomVariable/Teaching.pdf") stripchart(dat[,1] ~ dat[,2], pch=16, method="jitter", jitter=0.2, vertical=TRUE, ylab="Teaching Score", xlab="Attendance Status", ylim=c(min(dat[,1]), max(dat[,1]))) abline(v=1, col="grey", lty=2) abline(v=2, col="grey", lty=2) lines(x=c(0.9, 1.1), rep(mean(dat[keep,1]),2), col=4) lines(x=c(1.9, 2.1), rep(mean(dat[-keep,1]),2), col=4) dev.off() boxplot(dat$scores ~ dat$status, xlab="Status", ylab="Scores") ####### perfrom independent two-sample t test t.test(scores ~ status, var.equal=T) t.test(scores ~ status) ######## x<-factor(rep(c(1,2,3), each=10)) str(x) table(x) ThreeGroup<-aov(y ~ x) sum<-summary(ThreeGroup) sum