#This function creates a scatter plot of weight vs. height #by gender. It also gives the simple linear regression lines. # myplot<-function(data) { #Extract the data I want from "studdata" gender<-data[,2] height<-data[,6] weight<-data[,7] female<-gender=="F" #Calculate SLR lines SLR.F<-lsfit(x=height[female],y=weight[female])$coef SLR.M<-lsfit(x=height[!female],y=weight[!female])$coef #Set up an empty plot (i.e. no data) plot(height,weight,type="n", xlab="Height (in.)",ylab="Weight (ibs.)", main="Weight vs. Height by Gender", sub="Spring 2004 STAT330 Data") #Add the points to the plot points(height[female],weight[female],pch="F",cex=0.5) points(height[!female],weight[!female],pch="M",cex=0.5) #Add the regression lines to the plot abline(coef=SLR.F,lty="solid") abline(coef=SLR.M,lty="dotted") #Add a legend (for the regression lines) to the plot legend(x=c(60,65),y=c(270,290),legend=c("Females","Males"), lty=c("solid","dotted"),cex=0.75) #Add some text to the plot slr.f=paste("Wght = ",round(SLR.F[1],1)," + ",round(SLR.F[2],1),"(Hght)", sep="") slr.m=paste("Wght = ",round(SLR.M[1],1)," + ",round(SLR.M[2],1),"(Hght)", sep="") text(x=c(60,60),y=c(130,180),labels=c(slr.f,slr.m),cex=0.5) p<-which(data[,7]>270) text(x=75,y=280,labels=paste("Obs.",p),cex=0.75) arrows(x0=73.9,y0=280,x1=data[p,6]+.1,y1=data[p,7]-.1,length=0.1) }