# Measuring the degree to which two variables co-vary: Correlation # covariance examples # all online: # (x,y); (1,1), (2,2), (3,3), (4,4) x<- c(1, 2, 3, 4) y <- c(1, 2, 3, 4) cov(x,y) # ex 2 # (x,y); (1,4), (2,3), (3,2), (4,1) x<- c(1, 2, 3, 4) y <- c(4, 3, 2, 1) cov(x,y) ########### equivalent to cos() # in R cos() function Angles are in radians, # not degrees (i.e., a right angle is pi/2) # cos 90 degrees cos(pi/2) # cos 0 degrees cos(0) ############# # from url mydataframe <- read.delim("http://www.robin-beaumont.co.uk/virtualclassroom/book2data/correlation_vo2max.dat",header=TRUE) # local file ignore: # mydataframe <- read.delim(file.choose(), header=TRUE) # mydataframe <- read.delim("D:\\web_sites_mine\\HIcourseweb # new\\book2data\\correlation_vo2max.dat", header=TRUE) names(mydataframe) # vo2max scatter matrix # vo2max data correlation_vo2max.dat in book2data folder # to find one correlation + p value + ci cor.test(mydataframe$mftvomax, mydataframe$weight) # adapted from rdmr option library(car) scatterplotMatrix(~abdcurln+height+mftvomax+weight, reg.line=lm, smooth=FALSE, spread=FALSE, span=0.5, id.n=0, diagonal = 'histogram', data=mydataframe) #### # Using result from: # cor.test(mydataframe$mftvomax, mydataframe$weight) res <- .3345 N<- 20 adj_r <- sqrt(1-((1-res^2)*(N-1))/(N-2)) adj_r # alternative model1 <-lm(mydataframe$mftvomax ~ mydataframe$weight, data=mydataframe) # r summary(model1)$r.squared sqrt(summary(model1)$r.squared) # adjusted r summary(model1)$adj.r.squared sqrt(summary(model1)$adj.r.square) # rank order # method is "kendall" or "spearman", kendallres<- cor.test(mydataframe$mftvomax, mydataframe$weight, method="kendall") kendallres str(kendallres) spearres<- cor.test(mydataframe$mftvomax, mydataframe$weight, method="spearman") str(spearres) ### examples # url: mydataframe <- read.delim("http://www.robin-beaumont.co.uk/virtualclassroom/book2data/regression1.dat") # local file ignore: # mydataframe <- read.delim("D:\\web_sites_mine\\HIcourseweb new\\book2data\\regression1.dat", # header=TRUE) names(mydataframe) rcorr.adjust(mydataframe[,c("fastingBG","HBA1C")], type="pearson", use="complete") cor.test(mydataframe$fastingBG, mydataframe$HBA1C, alternative="two.sided", method="pearson") ###################### correlation panel # url: mydataframe <- read.delim("http://www.robin-beaumont.co.uk/virtualclassroom/book2data/ais_daag.dat", header=TRUE) # local file ignore: #mydataframe <- read.delim("D:\\web_sites_mine\\HIcourseweb new\\book2data\\ais_daag.dat", # header=TRUE) names(mydataframe) attach(mydataframe) library(psych) pairs.panels(mydataframe) pairs.panels(mydataframe[1:11]) cor.plot(cor(mydataframe[1:11]), color=FALSE)