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
- To understand the concept of correlation.
- To calculate Pearson's correlation coefficient.
- To calculate Spearman's rank correlation coefficient.
- To calculate Kendall's rank correlation coefficient.
- To perform a correlation significance test.
- To visualize the relationship using scatter plots.
- 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 Coefficient | Interpretation |
|---|---|
| +1 | Perfect positive correlation |
| +0.8 to +0.99 | Strong positive correlation |
| +0.5 to +0.79 | Moderate positive correlation |
| +0.2 to +0.49 | Weak positive correlation |
| 0 | No correlation |
| -0.2 to -0.49 | Weak negative correlation |
| -0.5 to -0.79 | Moderate negative correlation |
| -0.8 to -0.99 | Strong negative correlation |
| -1 | Perfect negative correlation |
Types of Correlation
1. Positive Correlation
As one variable increases, the other variable also increases.
Example:
| Hours Studied | Marks |
|---|---|
| 2 | 45 |
| 3 | 55 |
| 4 | 60 |
| 5 | 70 |
| 6 | 80 |
2. Negative Correlation
As one variable increases, the other decreases.
Example:
| Temperature | Heater Sales |
|---|---|
| 20 | 80 |
| 25 | 70 |
| 30 | 60 |
| 35 | 45 |
| 40 | 30 |
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
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.
| Student | Hours Studied | Marks |
|---|---|---|
| 1 | 2 | 45 |
| 2 | 3 | 50 |
| 3 | 4 | 55 |
| 4 | 5 | 65 |
| 5 | 6 | 70 |
| 6 | 7 | 75 |
| 7 | 8 | 85 |
| 8 | 9 | 90 |
| 9 | 10 | 95 |
| 10 | 11 | 98 |
Algorithm
- Create two vectors.
- Display the data.
- Compute Pearson correlation.
- Compute Spearman correlation.
- Compute Kendall correlation.
- Perform correlation test.
- Plot scatter graph.
- Add regression line.
- Display correlation matrix.
- 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
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
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
| Function | Purpose |
|---|---|
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
Post a Comment