## 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