Correlation
The difference between correlation and regression is the first doesn’t assign any dependency to either of the variables, and in the second a dependent variable (plotted on the Y axis) is assumed to change with changes in the independent varaible (plotted on the X axis).
R does have some functions for returning the correlation values between variables (eg. cor). The use of this function is shown in the chunk below. Note the use of the use=“complete” arguement is required to deal with the NAs in the dataset.
In the code below change the text in between the inverted commas to your directory and file name.
meas <- read.csv("~/ENG403/Weeks/5/meas.csv")
corres<-cor(meas[,8:10], use="complete")
print(corres)
L W D
L 1.0000000 -0.6710455 0.1309648
W -0.6710455 1.0000000 -0.1384193
D 0.1309648 -0.1384193 1.0000000
#The following code allows me to access the variables with the maximum correlation and use in the text document.
av<-abs(corres[])
mv<-max(abs(corres[corres<1]))
mvid<-which(av[]==mv)[1]
if(mvid<ncol(corres))
{cn=mvid[1]}else
{cn=ncol-mvid[1]}
rn<-which(av[]==mv)[2]-ncol(corres)
lc<-corres[mvid[1]]
var1<-colnames(corres)[cn]
var2<-colnames(corres)[rn]
Apart from the values on the diagonal (which is the correlation of each variable on itself) the strongest relationship exists between W and L. The correlation coefficient (r) measures the strength of the relationship that exists between the two variables. Unfortunately there is no base test in r to test the significance level of the correlation coefficient. However th rcorr function in the Hmisc package does produce that output. The way this function requires the input columns is a little odd though!
print(cormat)
[,1] [,2] [,3]
[1,] 1.00 -0.66 0.13
[2,] -0.66 1.00 -0.11
[3,] 0.13 -0.11 1.00
n
[,1] [,2] [,3]
[1,] 209 209 194
[2,] 209 279 259
[3,] 194 259 261
P
[,1] [,2] [,3]
[1,] 0.0000 0.0687
[2,] 0.0000 0.0662
[3,] 0.0687 0.0662
From the output can you interpret the correlations that are significantly significant?
EXERCISE
Experiment with the output for other types of correlation tests. What is the cov test?
LS0tCnRpdGxlOiAiVHV0b3JpYWwgaW4gUiAtIENvcnJlbGF0aW9uIC0gc2lnbmlmaWNhbmNlIHRlc3RpbmciCmF1dGhvcjogIkRyIEhlbGVuIEZhaXJ3ZWF0aGVyIgpkYXRlOiAnYHIgZm9ybWF0KFN5cy50aW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