Correlation Analysis Using R

 

Experiment

Correlation Analysis Using R

Aim

To study the relationship between two variables by computing the correlation coefficient and visualizing the relationship using R.


Objectives

  1. To understand the concept of correlation.
  2. To calculate Pearson's correlation coefficient.
  3. To calculate Spearman's rank correlation coefficient.
  4. To calculate Kendall's rank correlation coefficient.
  5. To perform a correlation significance test.
  6. To visualize the relationship using scatter plots.
  7. To interpret the strength and direction of correlation.

Theory

Correlation is a statistical technique used to measure the strength and direction of the relationship between two variables.

For example,

  • Hours studied and marks obtained
  • Temperature and electricity consumption
  • Height and weight
  • Advertising expenditure and sales

The correlation coefficient is denoted by r.

The value of r always lies between −1 and +1.

Correlation CoefficientInterpretation
+1Perfect positive correlation
+0.8 to +0.99Strong positive correlation
+0.5 to +0.79Moderate positive correlation
+0.2 to +0.49Weak positive correlation
0No correlation
-0.2 to -0.49Weak negative correlation
-0.5 to -0.79Moderate negative correlation
-0.8 to -0.99Strong negative correlation
-1Perfect negative correlation

Types of Correlation

1. Positive Correlation

As one variable increases, the other variable also increases.

Example:

Hours StudiedMarks
245
355
460
570
680

2. Negative Correlation

As one variable increases, the other decreases.

Example:

TemperatureHeater Sales
2080
2570
3060
3545
4030

3. No Correlation

There is no relationship between the variables.


Pearson Correlation Coefficient

Pearson's correlation measures the linear relationship between two continuous variables.

Formula

r=∑(xi−xˉ)(yi−yˉ)∑(xi−xˉ)2∑(yi−yˉ)2r=\frac{\sum (x_i-\bar{x})(y_i-\bar{y})} {\sqrt{\sum(x_i-\bar{x})^2\sum(y_i-\bar{y})^2}}

In R

cor(x,y)

Spearman Rank Correlation

Spearman correlation measures the relationship between ranked variables.

Useful when data are not normally distributed.

cor(x,y,method="spearman")

Kendall Rank Correlation

Kendall correlation measures the association between two ranked variables.

cor(x,y,method="kendall")

Correlation Test

R provides

cor.test(x,y)

This gives

  • Correlation coefficient
  • p-value
  • Confidence interval
  • Hypothesis test

Sample Data

Consider the following data representing Hours Studied and Marks Obtained.

StudentHours StudiedMarks
1245
2350
3455
4565
5670
6775
7885
8990
91095
101198

Algorithm

  1. Create two vectors.
  2. Display the data.
  3. Compute Pearson correlation.
  4. Compute Spearman correlation.
  5. Compute Kendall correlation.
  6. Perform correlation test.
  7. Plot scatter graph.
  8. Add regression line.
  9. Display correlation matrix.
  10. Interpret the results.

Program

# Sample Data
hours <- c(2,3,4,5,6,7,8,9,10,11)

marks <- c(45,50,55,65,70,75,85,90,95,98)

# Create data frame
student <- data.frame(hours,marks)

print(student)

# Pearson Correlation
pearson <- cor(hours,
marks)

cat("Pearson Correlation =",pearson,"\n")

# Spearman Correlation
spearman <- cor(hours,
marks,
method="spearman")

cat("Spearman Correlation =",spearman,"\n")

# Kendall Correlation
kendall <- cor(hours,
marks,
method="kendall")

cat("Kendall Correlation =",kendall,"\n")

# Correlation Test
result <- cor.test(hours,
marks)

print(result)

# Correlation Matrix
cor(student)

Sample Output

Pearson Correlation = 0.996

Spearman Correlation = 1

Kendall Correlation = 1

The output indicates a very strong positive correlation between hours studied and marks obtained.


Data Visualization

1. Scatter Plot

plot(hours,
marks,
pch=19,
col="blue",
main="Hours Studied vs Marks",
xlab="Hours Studied",
ylab="Marks")



2. Scatter Plot with Regression Line

plot(hours,
marks,
pch=19,
col="red",
main="Correlation Analysis",
xlab="Hours Studied",
ylab="Marks")

abline(lm(marks~hours),
col="blue",
lwd=2)

The regression line shows the trend of the relationship.




3. Multiple Plots

par(mfrow=c(1,2))

plot(hours,
marks,
pch=19,
col="red",
main="Scatter Plot")

hist(marks,
col="skyblue",
main="Distribution of Marks")

par(mfrow=c(1,1))




4. Pairs Plot

If multiple variables are available, use:

pairs(student,
main="Pairwise Scatter Plot")

This displays pairwise relationships between all numeric variables.


Explanation of Functions Used

FunctionPurpose
cor()Computes correlation coefficient
cor(..., method="pearson")Pearson correlation (default)
cor(..., method="spearman")Spearman rank correlation
cor(..., method="kendall")Kendall rank correlation
cor.test()Tests significance of correlation
lm()Fits a linear regression model
abline()Adds the regression line to the plot
plot()Creates a scatter plot
pairs()Creates pairwise scatter plots
data.frame()Creates a data frame

Result

The correlation between hours studied and marks obtained was successfully computed using Pearson, Spearman, and Kendall correlation coefficients. The scatter plot with the fitted regression line clearly indicated a strong positive linear relationship. The cor.test() function confirmed the statistical significance of the correlation.

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Programs to try using Functions in R - Assignment 3