library(lattice) grades <- read.table(file = "C:/Documents and Settings/steeleb/My Documents/Stat 341/NonameGrades.txt",header=T) colnames(grades) <- c("H1","H2","H3","E1") head(grades) hmk <- rowSums(grades[,1:3]) exam <- grades$E1 mean(exam) median(exam) hist(exam,breaks=seq(from=0,to=101,by=5)) xyplot(exam~hmk,type=c("p","r"),col=1,pch=16) I <- exam > 0 xyplot(exam[I]~hmk[I],type=c("p","r"),col=1,pch=16,xmin=0) c(cor(exam,hmk),cor(exam[I],hmk[I])) summary(lm(exam[I]~hmk[I])) ############## center x and y x <- hmk[I] - mean(hmk[I]) y <- exam[I] - mean(exam[I]) xyplot(y~x,type=c("p","r"),col=1,pch=16,xmin=0) summary(lm(y~x)) summary(lm(y~-1+x)) # eliminate intercept from model ####################################### melanoma head(melanoma) Year <- melanoma$year Incidence <- melanoma$incidence xyplot(Incidence~Year,type=c("p","r"),col=1,pch=16) cor(Incidence,Year) summary(lm(Incidence~Year)) Year.post36 <- Year - 1936 xyplot(Incidence~Year.post36,type=c("p","r"),col=1,pch=16) cor(Incidence,Year.post36) summary(lm(Incidence~Year.post36)) library(DAAG) help("ais") head(ais) xyplot(ais$ssf~ais$pcBfat|ais$sex,col=1,xlab="Percent body fat",ylab="Skinfold thickness",pch=16) cor(ais$ssf,ais$pcBfat) I <- ais$sex=="f" sum(I) cor(ais$ssf[I],ais$pcBfat[I]) I <- ais$sex=="m" sum(I) cor(ais$ssf[I],ais$pcBfat[I]) ######################### # # Investigate the extent to which hemoglobin concentration, or some other measure of # blood oxygen-carrying capacity (a measure of aerobic fitness) is associated with percent body fat # (or some other measure of fat-to-muscle proportion). Be sure to examine sports individually. # Which variables show the greatest degree of linear association? Which sports show the # greatest degree of linear association? Can the association be explained if we consider the role of # body weight towards performance of specific sports? # # To answer these questions, # (i) provide a multipanel graph using the variables you selected as most informative and hence # showing a maximal degree of association # (ii) report correlation coefficients for each sport # (iii) Be sure to identify any sport with too few observation pairs to be informative in this analysis. # ############################################### # Example y <- ais$hg # choose a measure of aerobic fitness x <- ais$lbm # choose a measure of fat-to-muscle proportion xyplot(y~x,groups=ais$sex,xlab="x",ylab="y",type=c("p","r"),pch=16,col=c("red","blue")) legend("topright",col=c("red","blue"),pch=16,legend=levels(ais$sex)) xyplot(y~x|ais$sport,col=1,xlab="x",ylab="y",type=c("p","r"),pch=16) xyplot(y~x|ais$sport,groups=ais$sex,xlab="x",ylab="y",type=c("p","r"),pch=16,col=c("red","blue")) legend("topright",col=c("red","blue"),pch=16,legend=levels(ais$sex)) v <- length(levels(ais$sport)) # number of levels of sport for (i in 1:v){ sport.i <- levels(ais$sport)[i] # select a sport I <- ais$sport==sport.i print(sport.i) print(c(sum(I),cor(x[I],y[I]))) }