Professional Documents
Culture Documents
R Companion - Correlation and Linear Regression
R Companion - Correlation and Linear Regression
Examples
The species diversity example is shown below in the “How to do the test” section.
Data = read.table(textConnection(Input),header=TRUE)
https://rcompanion.org/rcompanion/e_01.html 1/7
27/03/2024, 11:13 R Companion: Correlation and Linear Regression
plot(Species ~ Latitude,
data=Data,
pch=16,
xlab = "Latitude",
ylab = "Species")
Correlation
Correlation can be performed with the cor.test function in the native stats package. It can perform
Pearson, Kendall, and Spearman correlation procedures. Methods for multiple correlation of several
variables simultaneously are discussed in the Multiple regression chapter.
Pearson correlation
Pearson correlation is the most common form of correlation. It is a parametric test, and assumes that
the data are linearly related and that the residuals are normally distributed.
cor.test( ~ Species + Latitude,
data=Data,
method = "pearson",
conf.level = 0.95)
cor
-0.4628844
Kendall correlation
Kendall rank correlation is a non-parametric test that does not assume a distribution of the data or that
the data are linearly related. It ranks the data to determine the degree of correlation.
cor.test( ~ Species + Latitude,
data=Data,
method = "kendall",
continuity = FALSE,
conf.level = 0.95)
Spearman correlation
https://rcompanion.org/rcompanion/e_01.html 2/7
27/03/2024, 11:13 R Companion: Correlation and Linear Regression
Spearman rank correlation is a non-parametric test that does not assume a distribution of the data or
that the data are linearly related. It ranks the data to determine the degree of correlation, and is
appropriate for ordinal measurements.
cor.test( ~ Species + Latitude,
data=Data,
method = "spearman",
continuity = FALSE,
conf.level = 0.95)
rho
-0.3626323
Linear regression
Linear regression can be performed with the lm function in the native stats package. A robust regression
can be performed with the lmrob function in the robustbase package.
model = lm(Species ~ Latitude,
data = Data)
summary(model) # shows parameter estimates,
# p-value for model, r-square
library(car)
https://rcompanion.org/rcompanion/e_01.html 3/7
27/03/2024, 11:13 R Companion: Correlation and Linear Regression
plot(fitted(model),
residuals(model))
https://rcompanion.org/rcompanion/e_01.html 4/7
27/03/2024, 11:13 R Companion: Correlation and Linear Regression
A plot of residuals vs. predicted values. The residuals should be unbiased and
homoscedastic. For an illustration of these properties, see this diagram by Steve Jost
at DePaul University: condor.depaul.edu/sjost/it223/documents/resid-plots.gif.
### additional model checking plots with: plot(model)
### alternative: library(FSA); residPlot(model)
Robust regression
The lmrob function in the robustbase package produces a linear regression which is not sensitive to
outliers in the response variable. It uses MM-estimation.
library(robustbase)
model.null = lmrob(Species ~ 1,
data = Data)
plot(Species ~ Latitude,
data = Data,
pch=16,
xlab = "Latitude",
ylab = "Species")
abline(int, slope,
lty=1, lwd=2, col="blue") # style and color of line
https://rcompanion.org/rcompanion/e_01.html 5/7
27/03/2024, 11:13 R Companion: Correlation and Linear Regression
# # #
Input = ("
Weight Eggs
5.38 29
7.36 23
6.13 22
4.75 20
8.10 25
8.62 25
6.30 17
7.44 24
7.26 20
7.17 27
7.78 24
6.23 21
5.42 22
7.87 22
5.25 23
7.37 35
8.01 27
4.92 23
7.03 25
6.45 24
5.06 19
6.72 21
7.00 20
9.39 33
6.49 17
6.34 21
6.16 25
5.74 22
")
Data = read.table(textConnection(Input),header=TRUE)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
https://rcompanion.org/rcompanion/e_01.html 6/7
27/03/2024, 11:13 R Companion: Correlation and Linear Regression
(Intercept) 12.6890 4.2009 3.021 0.0056 **
Weight 1.6017 0.6176 2.593 0.0154 *
Multiple R-squared: 0.2055, Adjusted R-squared: 0.175
F-statistic: 6.726 on 1 and 26 DF, p-value: 0.0154
### Neither the r-squared nor the p-value agrees with what is reported
### in the Handbook.
library(car)
Power analysis
Power analysis for correlation
### --------------------------------------------------------------
### Power analysis, correlation, p. 208
### --------------------------------------------------------------
pwr.r.test(n = NULL,
r = 0.500,
sig.level = 0.05,
power = 0.80,
alternative = "two.sided")
# # #
https://rcompanion.org/rcompanion/e_01.html 7/7