rm(list=ls()) data = read.csv("ILE-2013.csv",header=TRUE) namevar = c("score","property","corrupt","fiscal","govern","busin","labor", "monet","trade","invest","finan") attach(data) n = nrow(data) indicadores = data[,3:13] boxplot(indicadores,names=namevar) pairs(indicadores,labels=namevar) round(cor(indicadores),2) par(mfrow=c(1,2)) plot(indicadores[,2],indicadores[,3],xlab=namevar[2],ylab=namevar[3], xlim=c(0,100),ylim=c(0,100),pch=16) plot(indicadores[,10],indicadores[,11],xlab=namevar[10],ylab=namevar[11], xlim=c(0,100),ylim=c(0,100),pch=16) continente = unique(Continente) cont = rep(1,n) cont[Continente=="America"]=2 cont[Continente=="Asia"]=3 cont[Continente=="Europa"]=4 cont[Continente=="Oceania"]=5 par(mfrow=c(1,2)) plot(indicadores[,2]+0.5*rnorm(176),indicadores[,3]+0.5*rnorm(176),xlab=namevar[2],ylab=namevar[3], xlim=c(0,100),ylim=c(0,100),col=cont,pch=16) legend(10,100,legend=continente,col=1:5,bty="n",pch=16) plot(indicadores[,10]+0.5*rnorm(176),indicadores[,11]+0.5*rnorm(176),xlab=namevar[10],ylab=namevar[11], xlim=c(0,100),ylim=c(0,100),col=cont,pch=16) # calculando distancias de Mahalanobis # ------------------------------------ x = as.matrix(data[,4:13]) n = nrow(x) xbar = apply(x,2,mean) S = var(x) iS = solve(S) dist = x-matrix(xbar,n,10,byrow=TRUE) iS = matrix(as.double(iS),10,10) Dm = diag(dist%*%iS%*%t(dist)) pval = round(1-pchisq(Dm,3),4) rDm = round(sqrt(Dm),2) par(mfrow=c(1,1)) plot(Dm,xlab="Observation",ylab="Mahalanobis distance") abline(h=qchisq(0.95,3),col=2,lwd=2) abline(h=qchisq(0.99,3),col=4,lwd=2) abline(h=qchisq(0.999,3),col=6,lwd=2) legend(100,80,c("Cut-off for 5% level","Cut-off for 1% level","Cut-off for 0.1% level"), col=c(2,4,6),bty="n",lwd=2)