# FactoMineR con datos de Linnerud data <- read.csv("Linnerud.csv",header=TRUE,row.names=1); head(data) library(FactoMineR) lin.pca <- PCA(data, graph = FALSE) lin.pca$eig lin.pca$var lv <- lin.pca$var tabvar <- list() for (i in 1:2) { vcum <- t(apply(lv$cos2,1,cumsum)) tvar <- cbind(lv$contrib[,i],lv$coord[,i],lv$cos2[,i],vcum[,i]) colnames(tvar) <- c("contributions","correlations","quality","cum.qual.") tabvar[[i]] <- tvar } tabvar li <- lin.pca$ind tabind <- list() for (i in 1:2) { icum <- t(apply(li$cos2,1,cumsum)) tind <- cbind(li$dist,li$contrib[,i],li$coord[,i],li$cos2[,i],icum[,i]) colnames(tind) <- c("distance","contributions","correlations","quality","cum.qual.") tabind[[i]] <- tind } tabind ######################################################################## # circle of correlations # plot of the circle of correlations with supplementary characters ccor <- function (dat1,dat2=NULL,title="PCA - circle of correlations",thrsig=NULL) { xmin = -1.05 xmax = 1.05 ymin = -1.05 ymax = 1.05 # draws the frame plot(0, 0, xlim=c(xmin,xmax),ylim=c(ymin,ymax), type="n", asp = 1, xlab = colnames(dat1)[1], ylab = colnames(dat1)[2], main = title) # draws the axes with their names arrows(0, 0, 1, 0, length = 0.1, angle = 15, code = 2, col = "black") text(1.1,0,colnames(dat1)[1]) arrows(0, 0, 0, 1, length = 0.1, angle = 15, code = 2, col = "black") text(0,1.05,colnames(dat1)[2]) # draws the circle of correlation with the active variables cercor(dat1,thrsig) # if any, draws the supplementary characters if (!is.null(dat2)) { for (v in 1:dim(dat2)[1]) { arrows(0, 0, dat2[v, 1], dat2[v, 2], length = 0.1, angle = 15, code = 2, col = "red") } # identify (dat2[,1],dat2[,2], col="red", lab=rownames(dat2)) text (dat2[,1],dat2[,2], col="red", lab=rownames(dat2),pos=4) } } ######################################################################## # plot of the circle of correlations cercor <- function (dat,thrsig=NULL) { # draws the circle and the axes x.cercle <- seq(-1, 1, by = 0.01) y.cercle <- sqrt(1 - x.cercle^2) lines(x.cercle, y = y.cercle) lines(x.cercle, y = -y.cercle) if (!is.null(thrsig)) { lines(x.cercle*thrsig, y = y.cercle*thrsig, col = "gray") lines(x.cercle*thrsig, y = -y.cercle*thrsig, col = "gray") } abline(v = 0, lty = 2) abline(h = 0, lty = 2) # draws the variables with its text for (v in 1:dim(dat)[1]) { arrows(0, 0, dat[v, 1], dat[v, 2], length = 0.1, angle = 15, code = 2, col = "blue") } text (dat[,1],dat[,2], col="blue", lab=rownames(dat),pos=4) } ######################################################################## # plot of the units on factor planes plane <- function (dat1, dat2=NULL, title="PCA units on factor planes") { # draws the frame with axes and names xmin = min(dat1[,1]) - .02 xmax = max(dat1[,1]) + .02 ymin = min(dat1[,2]) - .02 ymax = max(dat1[,2]) + .02 if (!is.null(dat2)) { xmin = min(xmin,dat2[,1]) - .02 xmax = max(xmax,dat2[,1]) + .02 ymin = min(ymin,dat2[,2]) - .02 ymax = max(ymax,dat2[,2]) + .02 } plot(0, 0, xlim=c(xmin,xmax),ylim=c(ymin,ymax), type="n", asp = 1, xlab = colnames(dat1)[1], ylab = colnames(dat1)[2], pch=".", main = title) abline(v = 0, lty = 2) abline(h = 0, lty = 2) # draws the names of the units text(dat1,lab=rownames(dat1)) if (!is.null(dat2)) { text(dat2,lab=rownames(dat2),col="red") } } ######################################################################################## ccor(lv$coord[,1:2]) plane(li$coord[,1:2]) ######################################################################################## data <- read.csv("Decathlon_cpl.csv",header=TRUE,row.names=1); head(data) library(FactoMineR) dec.pca <- PCA(data[,-13],ind.sup = c(1:13),quanti.sup = c(11:12), graph = FALSE) dec.pca$eig ccor(dec.pca$var$coord[,1:2],dec.pca$quanti.sup$coord[,1:2]) ccor(dec.pca$var$coord[,3:4],dec.pca$quanti.sup$coord[,3:4]) plane(dec.pca$ind$coord[,1:2],dec.pca$ind.sup$coord[,1:2]) plane(dec.pca$ind$coord[,3:4],dec.pca$ind.sup$coord[,3:4]) dec2.pca <- PCA(data,quanti.sup = c(11:12),quali.sup = 13, graph = FALSE) ccor(dec2.pca$var$coord[,1:2],dec2.pca$quanti.sup$coord[,1:2]) plane(dec2.pca$ind$coord[,1:2],dec2.pca$quali.sup$coord[,1:2]) ######################################################################################## dv <- dec.pca$var tabvar <- list() for (i in 1:4) { vcum <- t(apply(dv$cos2,1,cumsum)) tvar <- cbind(dv$contrib[,i],dv$coord[,i],dv$cos2[,i],vcum[,i]) colnames(tvar) <- c("contributions","correlations","quality","cum.qual.") tabvar[[i]] <- tvar } tabvar di <- dec.pca$ind tabind <- list() for (i in 1:4) { icum <- t(apply(di$cos2,1,cumsum)) tind <- cbind(di$dist,di$contrib[,i],di$coord[,i],di$cos2[,i],icum[,i]) colnames(tind) <- c("distance","contributions","coordinates","quality","cum.qual.") tabind[[i]] <- tind } tabind print(lin.pca) dimdesc(lin.pca)