transfer from old repo
This commit is contained in:
parent
cfa4a5f9cc
commit
277e2b8cf3
1111 changed files with 83736 additions and 0 deletions
74
1 PA Decline/archive/generation_1/PCA.R
Normal file
74
1 PA Decline/archive/generation_1/PCA.R
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
## ItMLiHSmar2022
|
||||
## PCA.R, child script
|
||||
## Principal components analysis for data visualisation
|
||||
## Andreas Gammelgaard Damsbo, agdamsbo@clin.au.dk
|
||||
|
||||
|
||||
## ====================================================================
|
||||
## Step 0: data wrangling
|
||||
## ====================================================================
|
||||
# source("data_format.R")
|
||||
|
||||
X1<-X %>% dplyr::mutate(across(where(is.factor), as.numeric))
|
||||
|
||||
pc.out<-prcomp(X1, center=TRUE, scale = TRUE)
|
||||
|
||||
pc.sum<-summary(pc.out)
|
||||
|
||||
## ====================================================================
|
||||
## Step 1: plotting
|
||||
## ====================================================================
|
||||
Xy$group<-factor(Xy$group,labels = c("No decline", "Decline"))
|
||||
|
||||
library(ggfortify)
|
||||
ppc12 <- autoplot(pc.out,
|
||||
data = Xy,
|
||||
x=1,
|
||||
y=2,
|
||||
colour = 'group')+
|
||||
labs(title = "PC1 and PC2",
|
||||
colour = "Outcome")
|
||||
ppc13 <- autoplot(pc.out,
|
||||
data = Xy,
|
||||
x=1,
|
||||
y=3,
|
||||
colour = 'group')+
|
||||
labs(title = "PC1 and PC3",
|
||||
colour = "Outcome")
|
||||
ppc23 <- autoplot(pc.out,
|
||||
data = Xy,
|
||||
x=2,
|
||||
y=3,
|
||||
colour = 'group')+
|
||||
labs(title = "PC2 and PC3",
|
||||
colour = "Outcome")
|
||||
|
||||
# Scree plot
|
||||
pscr<-tibble(x=1:dim(pc.sum$importance)[2],
|
||||
Proportion=pc.sum$importance[2,],
|
||||
Cumulative=pc.sum$importance[3,])%>%
|
||||
pivot_longer(cols=-x)%>%
|
||||
ggplot(aes(x=x,y=value,color=name))+
|
||||
geom_line()+
|
||||
geom_point()+
|
||||
ylim(0,1)+
|
||||
labs(title = "Scree plot",
|
||||
color= "Variance")+
|
||||
ylab("Variance")+
|
||||
xlab("Principal components")
|
||||
|
||||
## ====================================================================
|
||||
## Step 2: merge plots
|
||||
## ====================================================================
|
||||
library(patchwork)
|
||||
pca22<-ppc12+
|
||||
theme(legend.position="none")+
|
||||
ppc13+
|
||||
ppc23+theme(legend.position="none")+
|
||||
pscr+
|
||||
plot_layout(ncol=2)+
|
||||
plot_annotation(title = 'Principal component visualisation',
|
||||
tag_levels = "A")
|
||||
|
||||
# pca22
|
||||
|
||||
Loading…
Reference in a new issue