Canonical Correlation Analysis in R| Canonical Correlation Analysis | R Data Analysis Examples

preview_player
Показать описание
Canonical Correlation Analysis in R| Canonical Correlation Analysis | R Data Analysis Examples
In statistics, canonical-correlation analysis [CCA], also called canonical variates analysis, is a way of inferring information from cross-covariance matrices. If we have two vectors X = [X1, ..., Xn] and Y = [Y1, ..., Ym] of random variables, and there are correlations among the variables, then canonical-correlation analysis will find linear combinations of X and Y which have a maximum correlation with each other.

library(CCA)
library(tidyverse)
theme_set(theme_bw(16))

penguins = penguins %=% drop_na()

penguins %=% head()

X = penguins %=%
select(bill_depth_mm, bill_length_mm) %=%
scale()

Y = penguins %=%
select(flipper_length_mm,body_mass_g) %=%
scale()
head(Y)

cc_results =- cancor(X,Y)

str(cc_results)

cc_results$xcoef

cc_results$ycoef

cc_results$cor

cor(CC1_X,CC1_Y)

assertthat::are_equal(cc_results$cor[1],
cor(CC1_X,CC1_Y)[1])

cca_df = penguins %=%
mutate(CC1_X=CC1_X,
CC1_Y=CC1_Y,
CC2_X=CC2_X,
CC2_Y=CC2_Y)

cca_df %=%
ggplot(aes(x=CC1_X,y=CC1_Y))+
geom_point()

cca_df %=%
ggplot(aes(x=species,y=CC1_X, color=species))+
geom_boxplot(width=0.5)+
geom_jitter(width=0.15)+

cca_df %=%
ggplot(aes(x=species,y=CC1_Y, color=species))+
geom_boxplot(width=0.5)+
geom_jitter(width=0.15)

cca_df %=%
ggplot(aes(x=CC1_X,y=CC1_Y, color=species))+
geom_point()

cca_df %=%
ggplot(aes(x=CC2_X,y=CC2_Y, color=sex))+
geom_point()
Рекомендации по теме
Комментарии
Автор

Maybe you're doing this for the video, but you can run each line of code much more easily by pressing CTRL+ENTER

alexanderstramp
Автор

the library CCA masks the select function from dplyr so I can't actually run any of your code

zwan
Автор

dont complicate the code, remove the pipe %>% : KEEP IT SIMPLE and READABLE BY EVERYONE

WahranRai