1. คำสั่งในการสร้างกราฟรูปที่ 8.3 data(chickwts) cw <- chickwts cw$weight <- cw$weight*-1 windows(6,10) layout(cbind(c(1,2,3,4,5,6)),height=c(1.7,1.7,1.7,1.7,1.6,1.6)) par(oma=c(0,1,1,1),mar=c(4,0,0,0),las=3) ############# p6 <- hist(cw$weight[cw$feed=="sunflower"],prob=T, xlim=c(-500,0),ylim=c(0,0.014), breaks=-100+0:12*(30),,xlab="",ylab="",main="", cex.axis=1.5,lwd=2,xaxt="n",yaxt="n",col=rgb(0,1,1,1/4)) curve(dnorm(x,mean=mean(cw$weight[cw$feed=="sunflower"]), sd=sd(cw$weight[cw$feed=="sunflower"])),add=T,lwd=2, xlim=c(-490,-160)) lines(c(-328.92,-328.92),c(0.0082,-0.05),lwd=2,col="red") text(-328.92,0.0115,expression(bar(y)[6]),srt=90,cex=1.5) points(-322.,0.0137,pch=20,cex=1) stripchart(cw$weight[cw$feed=="sunflower"], at=0.0005,method="stack",xlim=c(-500,0), ylim=c(1,1.1), pch=21,cex=2, frame=F,add=T,bg=rgb(0,1,1,1/4)) text(7,0.002,"SF", cex=1.3,srt=90) lines(c(-261.3,-261.3),c(0.01,-0.05),lwd=2) text(-260,0.0115,expression(bar(y)),srt=90,cex=1.5) points(-255,0.013,pch=20,cex=1) points(-255,0.014,pch=20,cex=1) ############# p5 <- hist(cw$weight[cw$feed=="soybean"],prob=T, xlim=c(-500,0),ylim=c(0,0.014), breaks=-100+0:12*(-30),,xlab="",ylab="",main="", cex.axis=1.5,lwd=2,xaxt="n",yaxt="n",col=rgb(1,0,1,1/4)) curve(dnorm(x,mean=mean(cw$weight[cw$feed=="soybean"]), sd=sd(cw$weight[cw$feed=="soybean"])),add=T,lwd=2, xlim=c(-400,-100)) lines(c(-246.43,-246.43),c(0.0075,-0.05),lwd=2,col="red") text(-246.43,0.0115,expression(bar(y)[5]),srt=90,cex=1.5) points(-239,0.0135,pch=20,cex=1) stripchart(cw$weight[cw$feed=="soybean"],at=0.0005, method="stack", xlim=c(-500,0),ylim=c(1,1.1),pch=21,cex=2, frame=F,add=T, bg=rgb(1,0,1,1/4)) text(7,0.002,"SB", cex=1.3,srt=90) lines(c(-261.3,-261.3),c(0.08,-0.05),lwd=2) ############# p4 <- hist(cw$weight[cw$feed=="meatmeal"],prob=T, xlim=c(-500,0),ylim=c(0,0.014), breaks=-100+0:12*(-30),,xlab="",ylab="",main="", cex.axis=1.5,lwd=2,xaxt="n",yaxt="n",col=rgb(1,1,0,1/4)) curve(dnorm(x,mean=mean(cw$weight[cw$feed=="meatmeal"]), sd=sd(cw$weight[cw$feed=="meatmeal"])),add=T,lwd=2,xlim=c(-450,-100)) stripchart(cw$weight[cw$feed=="meatmeal"],at=0.0005, method="stack",xlim=c(0,500),ylim=c(1,1.1),pch=21, cex=2, frame=F,add=T,bg=rgb(1,1,0,1/4)) lines(c(-276.91,-276.91),c(0.006,-0.05),lwd=2,col="red") text(-276.91,0.01,expression(bar(y)[4]),srt=90,cex=1.5) points(-270,0.012,pch=20,cex=1) text(7,0.003,"MM", cex=1.3,srt=90) lines(c(-261.3,-261.3),c(0.08,-0.05),lwd=2) ############# p3 <- hist(cw$weight[cw$feed=="linseed"],prob=T, xlim=c(-500,0),ylim=c(0,0.014), breaks=-100+0:12*(-30),,xlab="",ylab="",main="", cex.axis=1.5,lwd=2,xaxt="n",yaxt="n",col=rgb(0,0,1,1/4)) curve(dnorm(x,mean=mean(cw$weight[cw$feed=="linseed"]), sd=sd(cw$weight[cw$feed=="linseed"])),add=T,lwd=2, xlim=c(-365,-80)) stripchart(cw$weight[cw$feed=="linseed"],at=0.0005, method="stack",xlim=c(0,500),ylim=c(1,1.1),pch=21,cex=2, frame=F,add=T, bg=rgb(0,0,1,1/4)) lines(c(-218.75,-218.75),c(0.0078,-0.05),lwd=2,col="red") text(-218.75,0.011,expression(bar(y)[3]),srt=90,cex=1.5) points(-211,0.013,pch=20,cex=1) text(7,0.002,"LS", cex=1.3,srt=90) lines(c(-261.3,-261.3),c(0.08,-0.05),lwd=2) ############# #plot( p2, xlim=c(0,500), add=T,) # second p2 <- hist(cw$weight[cw$feed=="horsebean"],prob=T, xlim=c(-500,0),ylim=c(0,0.014), breaks=-100+0:12*(-30),xlab="",ylab="",main="",cex.axis=1.5,lwd=2, xaxt="n",yaxt="n",col=rgb(0,1,0,1/4)) curve(dnorm(x,mean=mean(cw$weight[cw$feed=="horsebean"]), sd=sd(cw$weight[cw$feed=="horsebean"])),add=T,lwd=2, xlim=c(-275,-40)) stripchart(cw$weight[cw$feed=="horsebean"],at=0.0005 ,method="stack",xlim=c(0,500),ylim=c(1,1.1),pch=21, cex=2, frame=F,add=T,bg=rgb(0,1,0,1/4)) lines(c(-160.2,-160.2),c(0.0104,-0.05),lwd=2,col="red") text(-158,0.012,expression(bar(y)[2]),srt=90,cex=1.5) points(-151,0.0145,pch=20,cex=1) text(7,0.003,"HB", cex=1.3,srt=90) lines(c(-261.3,-261.3),c(0.08,-0.05),lwd=2) ###################### p1 <- hist(cw$weight[cw$feed=="casein"],prob=T, breaks=-100+0:12*(-30),xlim=c(-500,0),ylim=c(0,0.014), xlab="",ylab="",main="",cex.axis=1.5,lwd=2, ="n",yaxt="n",col=rgb(1,0,0,1/4)) curve(dnorm(x,mean=mean(cw$weight[cw$feed=="casein"]), sd=sd(cw$weight[cw$feed=="casein"])),add=T,lwd=2, xlim=c(-500,-150)) stripchart(cw$weight[cw$feed=="casein"],at=0.0005, method="stack",xlim=c(0,500),pch=21,cex=2,ylim=c(1,1.1),frame=F,add=T,bg=rgb(1,0,0,1/4)) lines(c(-323.58,-323.58),c(0.0063,-0.05),lwd=2,col="red") text(-323.58,0.01,expression(bar(y)[1]),srt=90,cex=1.5) points(-316,0.012,pch=20,cex=1) text(7,0.003,"CS", cex=1.3,srt=90) lines(c(-261.3,-261.3),c(0.08,-0.05),lwd=2) axis(side=1,at=c(-500,-400,-300,-200,-100,0), labels=c("500","400","300","200","100","0"),cex.axis=1.3) mtext(side=2,"weight",cex=1,las=3,line=-0.5,adj=-3) 2. คำสั่งในการสร้างกราฟรูปที่ 8.4 library(scales) windows(10,7.5) layout(rbind(c(1,1,1),c(2,2,2),c(3,3,3)),width=c(3.4,3.3,3.3), height=c(2.5,2.5,2.5)) par(oma=c(0,0,0,0),mar=c(2,0.5,0,1)) mean=0; sd=3 #lb=1.96; ub=4 x <- seq(-10,10,length=100)*sd + mean hx <- dnorm(x,mean,sd)*3.4 plot(x, hx, type="n", xlim=c(-10, 10), ylim=c(0, 0.5), ylab = "", xlab = "", axes=FALSE,mar=c(3,3,0,3)) lines(x, hx, lwd=2) axis(1, at = 0, labels = "ค่าเฉลี่ยรวม" ,lwd=2,cex.axis=2.2,tck=-0.04) lines(c(-14,14),c(-0.02,-0.02),lwd=2) lines(c(0,0),c(-0.5,0.45),lwd=2) arrows(0.05, 0.2, 0.05-3.9, 0.2, lwd=2,col="red") arrows(0.05, 0.2, 0.05+3.9, 0.2, lwd=2,col="red") text(0,0.49,expression(bar(y)),cex=2) points(0.068,0.47,pch=20,cex=1.1) points(0.18,0.47,pch=20,cex=1.1) text(8,0.3,"SST",cex=2) ############# mean=0; sd=1 lb=1.96; ub=5 x <- seq(-3.5,3.5,length=100)*sd + mean hx <- dnorm(x,mean,sd) plot(x, hx, type="n", xlim=c(-4, 13), ylim=c(0, 0.5), ylab = "", xlab = "", axes=FALSE) axis(1, at = 0, labels = "กลุ่ม 1" ,lwd=2,cex.axis=2, line=-0.5,tck=-0.05) lines(x, hx, lwd=2) text(0.14,0.44,expression(bar(y)[1]),cex=2) points(0.35,0.42,pch=20,cex=1.1) shift = qnorm(1-0.05, mean=0, sd=1)*0.5 xfit2 <- x + shift yfit2 <- dnorm(xfit2, mean=shift, sd=1) # Print null hypothesis area col_null = "#FF0000E3" lines(xfit2, yfit2, lwd=2) axis(1, at =0.85, labels = "กลุ่ม 2" ,lwd=2, cex.axis=2,line=-0.5,tck=-0.05) text(0.9,0.44,expression(bar(y)[2]),cex=2) points(1.16,0.42,pch=20,cex=1.1) shift = qnorm(1-0.05, mean=0, sd=1)*5 xfit3 <- x + shift yfit3 <- dnorm(xfit3, mean=shift, sd=1) lines(xfit3, yfit3, lwd=2) axis(1, at = 8.2, labels = "กลุ่ม 3" ,lwd=2, cex.axis=2,line=-0.5,tck=-0.05) lines(c(-3.5,13.4),c(-0.02,-0.02),lwd=2) axis(1, at = 9.9, labels = "กลุ่ม 4" , lwd=2,cex.axis=2,line=-0.5,tck=-0.05) text(8.2,0.44,expression(bar(y)[3]),cex=2) points(8.46,0.42,pch=20,cex=1.1) shift4 = qnorm(1-0.05, mean=0, sd=1)*6 xfit4 <- x + shift4 yfit4 <- dnorm(xfit4, mean=shift4, sd=1) lines(xfit4, yfit4, lwd=2) text(9.86,0.44,expression(bar(y)[4]),cex=2) points(10.12,0.42,pch=20,cex=1.1) lines(c(0,0),c(-0.5,0.4),lwd=2) lines(c(0.85,0.85),c(-0.5,0.4),lwd=2) lines(c(8.2,8.2),c(-0.5,0.4),lwd=2) lines(c(9.9,9.9),c(-0.5,0.4),lwd=2) lines(c(4.5,4.5),c(-0.5,0.5),lwd=2) arrows(ub, 0.25, ub+3.2, 0.25, lwd=2,col="red") arrows(ub, 0.25, ub-5, 0.25, lwd=2,col="red") arrows(ub, 0.15,ub+4.9, 0.15,lwd=2,col="red") arrows(ub, 0.15, ub-4.2, 0.15, lwd=2,col="red") text(11.5,0.3,"SSTr",cex=2) ################################ mean=0; sd=1 lb=1.96; ub=5 x <- seq(-3.5,3.5,length=100)*sd + mean hx <- dnorm(x,mean,sd) plot(x, hx, type="n", xlim=c(-4, 13), ylim=c(0, 0.5), ylab = "", xlab = "", axes=FALSE) axis(1, at = 0, labels = "กลุ่ม 1" ,lwd=2,cex.axis=2, line=-0.5,tck=-0.05) lines(x, hx, lwd=2) shift = qnorm(1-0.05, mean=0, sd=1)*0.5 xfit2 <- x + shift yfit2 <- dnorm(xfit2, mean=shift, sd=1) # Print null hypothesis area col_null = "#FF0000E3" lines(xfit2, yfit2, lwd=2) axis(1, at =0.85, labels = "กลุ่ม 2" ,lwd=2, cex.axis=2,line=-0.5,tck=-0.05) shift = qnorm(1-0.05, mean=0, sd=1)*5 xfit3 <- x + shift yfit3 <- dnorm(xfit3, mean=shift, sd=1) lines(xfit3, yfit3, lwd=2) axis(1, at = 8.2, labels = "กลุ่ม 3" , lwd=2,cex.axis=2,line=-0.5,tck=-0.05) lines(c(-3.5,13.4),c(-0.02,-0.02),lwd=2) axis(1, at = 9.9, labels = "กลุ่ม 4" ,lwd=2, cex.axis=2,line=-0.5,tck=-0.05) shift4 = qnorm(1-0.05, mean=0, sd=1)*6 xfit4 <- x + shift4 yfit4 <- dnorm(xfit4, mean=shift4, sd=1) lines(xfit4, yfit4, lwd=2) lines(c(0,0),c(-0.5,0.4),lwd=2) lines(c(0.85,0.85),c(-0.5,0.4),lwd=2) lines(c(8.2,8.2),c(-0.5,0.4),lwd=2) lines(c(9.9,9.9),c(-0.5,0.4),lwd=2) lines(c(4.5,4.5),c(-0.5,0.5),lwd=2) ub1 <- 0 arrows(ub1, 0.25, ub1+1, 0.25, lwd=2,col="red") arrows(ub1, 0.25, ub1-1, 0.25, lwd=2,col="red") ub2 <- 0.85 arrows(ub2, 0.2, ub2+1.2, 0.2, lwd=2,col="red") arrows(ub2, 0.2, ub2-1.2, 0.2, lwd=2,col="red") ub3 <- 8.2 arrows(ub3, 0.25,ub3+1, 0.25,lwd=2,col="red") arrows(ub3, 0.25, ub3-1, 0.25, lwd=2,col="red") ub4 <- 9.9 arrows(ub4, 0.2,ub4+1.2, 0.2,lwd=2,col="red") arrows(ub4, 0.2, ub4-1.2, 0.2, lwd=2,col="red") text(11.5,0.3,"SSE",cex=2) 3. คำสั่งในการสร้างกราฟรูปที่ 8.5 lb=3; ub=10 x <- seq(0,100,length=1000) i <- x >= lb & x <= ub hx <- df(x,5,10) windows(5,3) par(oma=c(0,0,0,0),mar=c(2,1,0,0)) plot(x, hx,type="n", ylab="",ylim=c(0,0.8),xlim=c(0,5), xlab="x",axes=FALSE,cex.lab=3,cex.axis=3) lines(x,hx,lwd=3) polygon(c(lb,x[i],ub), c(0,hx[i],0), col="grey",lwd=3) lines(0:5,0:5*0,lwd=3) abline(h=0,lwd=3) mtext(side=1,line=0.5, expression(F[alpha]["("][df[1]][","][df[2]][")"]), adj=0.6,cex=1.5) arrows(3.5,0.1,3.2,0.03,lwd=3,length=0.1,angle=20) text(3.4,0.15,"p-value",cex=1.5) 8.1.4 คำสั่งในการสร้างกราฟรูปที่ 8.8 windows(8,7.5) layout(cbind(c(1,2,3)),height=c(2.5,2.5,2.5)) par(oma=c(0,0,0,0),mar=c(2,0,0,1)) data(ToothGrowth) tg <- ToothGrowth hist(tg$len,xlim=c(-2,40),prob=T,ylim=c(0,0.09), breaks=4+0:20*1.5,xlab="",ylab="",main="", cex.axis=1.5,lwd=2,yaxt="n") curve(dnorm(x,mean=mean(tg$len),sd=sd(tg$len)), xlim=c(-2,40),add=T,lwd=2) lines(c(-2,40),c(-0.0036,-0.0036),lwd=2) stripchart(tg$len,at=0.005,method="stack", xlim=c(-2,40),pch=21,cex=2,ylim=c(1,1.1), frame=F,add=T,bg="pink") mtext(side=1,at=18.81,"ค่าเฉลี่ยรวม", cex=1.5,line=2.4) lines(c(18.81,18.81),c(0.053,0),lwd=2) arrows(18.81, 0.027,18.81+8.8, 0.027,lwd=2,col="red") arrows(18.81, 0.027,18.81-8.8, 0.027,lwd=2,col="red") hist(tg$len[tg$dose==0.5],xlim=c(-2,40),ylim=c(0,0.17), prob=T,breaks=4+0:20*1.5,xlab="",ylab="",main="", cex.axis=1.5,lwd=2,yaxt="n",col=rgb(1,0,0,1/4)) curve(dnorm(x,mean=mean(tg$len[tg$dose==0.5]), sd=sd(tg$len[tg$dose==0.5])),xlim=c(-2,24),add=T,lwd=2) stripchart(tg$len[tg$dose==0.5],at=0.005,method="stack", xlim=c(-2,40),pch=21,cex=2,ylim=c(1,1.1),frame=F, add=T,bg=rgb(1,0,0,1/4)) lines(c(-2,40),c(-0.006,-0.006),lwd=2) lines(c(10.605,10.605),c(0.09,0),lwd=2) arrows(18.81, 0.05,18.81-8.205, 0.05,lwd=2,col="red") lines(c(18.81,18.81),c(0.15,0),lwd=2) hist(tg$len[tg$dose==1],xlim=c(-2,40),prob=T,breaks=4+0:20*1.5, xlab="",ylab="",main="",cex.axis=1.5,lwd=2,add=T, yaxt="n",col=rgb(0,1,0,1/4)) curve(dnorm(x,mean=mean(tg$len[tg$dose==1]), sd=sd(tg$len[tg$dose==1])),xlim=c(7,33),add=T,lwd=2) stripchart(tg$len[tg$dose==1],at=0.005,method="stack", xlim=c(-2,40),pch=21,cex=2,ylim=c(1,1.1), frame=F,add=T,rgb(0,1,0,1/4)) lines(c(19.735,19.735),c(0.09,0),lwd=2) arrows(18.81, 0.06,18.81+0.925, 0.06,lwd=2,col="red") hist(tg$len[tg$dose==2],xlim=c(-2,40),prob=T,breaks=4+0:20*1.5, xlab="",ylab="",main="",cex.axis=1.5,lwd=2,add=T, yaxt="n",col=rgb(0,0,1,1/4)) curve(dnorm(x,mean=mean(tg$len[tg$dose==2]), sd=sd(tg$len[tg$dose==2])),xlim=c(15,37),add=T,lwd=2) stripchart(tg$len[tg$dose==2],at=0.005,method="stack", xlim=c(-2,40),pch=21,cex=2,ylim=c(1,1.1), frame=F,add=T,bg=rgb(0,0,1,1/4)) lines(c(26.10,26.10),c(0.105,0),lwd=2) arrows(18.81, 0.08,18.81+7.29, 0.08,lwd=2,col="red") ########################### hist(tg$len[tg$dose==0.5],xlim=c(-2,40),ylim=c(0,0.17),prob=T, breaks=4+0:20*1.5,xlab="",ylab="",main="",cex.axis=1.5, lwd=2,yaxt="n",col=rgb(1,0,0,1/4)) curve(dnorm(x,mean=mean(tg$len[tg$dose==0.5]), sd=sd(tg$len[tg$dose==0.5])),xlim=c(-2,24),add=T,lwd=2) stripchart(tg$len[tg$dose==0.5],at=0.005,method="stack", xlim=c(-2,40),pch=21,cex=2,ylim=c(1,1.1), frame=F,add=T,bg=rgb(1,0,0,1/4)) lines(c(-2,40),c(-0.006,-0.006),lwd=2) lines(c(10.605,10.605),c(0.09,0),lwd=2) arrows(10.605, 0.05,10.605+5, 0.05,lwd=2,col="red") arrows(10.605, 0.05,10.605-5, 0.05,lwd=2,col="red") lines(c(18.81,18.81),c(0.15,0),lwd=2) hist(tg$len[tg$dose==1],xlim=c(-2,40),prob=T,breaks=4+0:20*1.5, xlab="",ylab="",main="",cex.axis=1.5,lwd=2,add=T, yaxt="n",col=rgb(0,1,0,1/4)) curve(dnorm(x,mean=mean(tg$len[tg$dose==1]), sd=sd(tg$len[tg$dose==1])),xlim=c(7,33),add=T,lwd=2) stripchart(tg$len[tg$dose==1],at=0.005,method="stack", xlim=c(-2,40),pch=21,cex=2,ylim=c(1,1.1), frame=F,add=T,rgb(0,1,0,1/4)) lines(c(19.735,19.735),c(0.09,0),lwd=2) arrows(19.735, 0.06,19.735+4, 0.06,lwd=2,col="red") arrows(19.735, 0.06,19.735-4, 0.06,lwd=2,col="red") hist(tg$len[tg$dose==2],xlim=c(-2,40), prob=T,breaks=4+0:20*1.5,xlab="",ylab="",main="",cex.axis=1.5,lwd=2,add=T,yaxt="n",col=rgb(0,0,1,1/4)) curve(dnorm(x,mean=mean(tg$len[tg$dose==2]), sd=sd(tg$len[tg$dose==2])),xlim=c(15,37),add=T,lwd=2) stripchart(tg$len[tg$dose==2],at=0.005,method="stack", xlim=c(-2,40),pch=21,cex=2,ylim=c(1,1.1), frame=F,add=T,bg=rgb(0,0,1,1/4)) lines(c(26.10,26.10),c(0.105,0),lwd=2) arrows(26.10, 0.08,26.10+3, 0.08,lwd=2,col="red") arrows(26.10, 0.08,26.10-3, 0.08,lwd=2,col="red")