knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

What is a Correlation?

Visualizing Correlation

knitr::include_graphics("pictures/correl/1.png")

Visualizing Correlation

knitr::include_graphics("pictures/correl/2.png")

Visualizing Correlation

knitr::include_graphics("pictures/correl/3.png")

Using Scatterplots

Using Scatterplots

library(rio)
library(ggplot2)
cleanup <- theme(panel.grid.major = element_blank(), 
                panel.grid.minor = element_blank(), 
                panel.background = element_blank(), 
                axis.line.x = element_line(color = "black"),
                axis.line.y = element_line(color = "black"),
                legend.key = element_rect(fill = "white"),
                text = element_text(size = 15))
exam <- import("data/exam_data.csv")
liar <- import("data/liar_data.csv")

Using Scatterplots

#from chapter 5 notes
scatter <- ggplot(exam, aes(Anxiety, Exam))
scatter +
  geom_point() +
  xlab("Anxiety Score") +
  ylab("Exam Score") +
  cleanup

Using Scatterplots

#from chapter 5 notes + coord_cartesian
scatter <- ggplot(exam, aes(Anxiety, Exam))
scatter +
  geom_point() +
  xlab("Anxiety Score") +
  ylab("Exam Score") +
  cleanup + 
  coord_cartesian(xlim = c(50,100), ylim = c(0,100))
  #just example numbers, you would want to use the real scale of the data

Modeling Relationships

Modeling Relationships

Measuring Relationships

Revision of Variance

$$SD^2 = \frac {\sum(X_i-\bar{X})^2}{N-1}$$

$$SD^2 = \frac {\sum(X_i-\bar{X})(X_i-\bar{X})}{N-1}$$

Revision of Variance

$$Cov(x,y) = \frac {\sum(X_i-\bar{X})(Y_i-\bar{Y})}{N-1} $$

Revision of Variance: Calculating Variance

var(exam$Revise)
var(exam$Exam)

Revision of Variance: Calculating Covariance

cov(exam$Revise, exam$Exam)
plot(exam$Revise, exam$Exam)

Problems with Covariance

The Correlation Coefficient

$$r = \frac{Cov(x,y)}{S_xS_y}$$

$$r = \frac{\sum(X_i-\bar{X})(Y_i-\bar{Y})}{(N-1)S_xS_y} $$

The Correlation Coefficient

The Correlation Coefficient

R versus r

Correlation: Example

cor(exam$Revise, exam$Exam)

Correlation: Example

Correlation: Understanding the NHST

Correlation: Understanding the NHST

Correlation: Understanding the NHST

Correlation: How to Calculate

Correlation: How to Calculate

Correlation: How to Calculate

cor(exam[ , -1], 
    use="pairwise.complete.obs", 
    method = "pearson")

cor(exam[ , -1], 
    use="pairwise.complete.obs", 
    method = "kendall")

Correlation: How to Calculate

library(Hmisc)
rcorr(as.matrix(exam[ , -1]), type = "pearson")

Correlation: How to Calculate

cor.test(exam$Revise,
         exam$Exam,
         method = "pearson")

Correlation Interpretation

Nonparametric Correlation

Correlation: Example

str(liar)

Correlation: Example

with(liar, cor.test(Creativity, Position, method = "spearman"))
with(liar, cor.test(Creativity, Position, method = "kendall"))

Correlation: Example

Correlation: Biserial

liar$Novice2 <- as.numeric(as.factor(liar$Novice))
str(liar) #we had to factor because of the character variable
with(liar, cor.test(Creativity, Novice2))
plot(liar$Creativity, liar$Novice2)

Comparing Correlations

Independent Correlations

Independent Correlations

library(cocor)
new <- subset(liar, Novice == "First Time")
old <- subset(liar, Novice == "Had entered Competition Before")
ind_data <- list(new, old)
cocor(~Creativity + Position | Creativity + Position,
      data = ind_data)

Dependent Correlations

Dependent Correlations

cocor(~Revise + Exam | Revise + Anxiety, 
      data = exam)

Partial and Semi-Partial Correlations

Partial and Semi-Partial Correlations

knitr::include_graphics("pictures/correl/9.png")

Partial Correlations

library(ppcor)
pcor(exam[ , -c(1)], method = "pearson")

Semipartial Correlations

spcor(exam[ , -c(1)], method = "pearson")

Summary



doomlab/learnSTATS documentation built on June 9, 2022, 12:54 a.m.