In [1]:
# rm(list=ls()) 
options(OutDec = ",")
#==============================================================================
# Exemplo 
#==============================================================================
# Dados da bolsa de valores de 'New York' em d07_acoes.txt
#==============================================================================
dados  <- read.table("../dados/d07_acoes.txt",header=F,sep="	", # Tab
          col.names=c("JP","Citi","Fargo","Royal","Exxon"))
print(head(dados))
          JP       Citi      Fargo      Royal      Exxon
1  0,0130338 -0,0078431 -0,0031889 -0,0447693  0,0052151
2  0,0084862  0,0166886 -0,0062100  0,0119560  0,0134890
3 -0,0179153 -0,0086393  0,0100360  0,0000000 -0,0061428
4  0,0215589 -0,0034858  0,0174353 -0,0285917 -0,0069534
5  0,0108225  0,0037167 -0,0101345  0,0291900  0,0409751
6  0,0101713 -0,0121978 -0,0083768  0,0137083  0,0029895
In [2]:
#==============================================================================
# Estatisticas
#==============================================================================
print("==== Médias ====")
print(round(apply(dados,2,"mean"),digits=5))
print("==== Correlações ====")
print(round(cor(dados), digits=3))
[1] "==== Médias ===="
     JP    Citi   Fargo   Royal   Exxon 
0,00106 0,00066 0,00163 0,00405 0,00404 
[1] "==== Correlações ===="
         JP  Citi Fargo Royal Exxon
JP    1,000 0,632 0,510 0,115 0,154
Citi  0,632 1,000 0,574 0,322 0,213
Fargo 0,510 0,574 1,000 0,182 0,146
Royal 0,115 0,322 0,182 1,000 0,683
Exxon 0,154 0,213 0,146 0,683 1,000
In [3]:
#==============================================================================
# Analise de componentes principais via funcao 'prcomp' do R
#==============================================================================
pcvar  <- prcomp(dados,scale=T)
P      <- pcvar$rotation 
sdev   <- pcvar$sdev  # raiz quadrada dos autovalores
print(pcvar)
print("==== Resumo ====")
print(summary(pcvar))
Standard deviations (1, .., p=5):
[1] 1,5611768 1,1861756 0,7074693 0,6324805 0,5051434

Rotation (n x k) = (5 x 5):
             PC1        PC2         PC3        PC4         PC5
JP    -0,4690832 -0,3680070 -0,60431522 -0,3630228  0,38412160
Citi  -0,5324055 -0,2364624 -0,13610618  0,6292079 -0,49618794
Fargo -0,4651633 -0,3151795  0,77182810 -0,2889658  0,07116948
Royal -0,3873459  0,5850373  0,09336192  0,3812515  0,59466408
Exxon -0,3606821  0,6058463 -0,10882629 -0,4934145 -0,49755167
[1] "==== Resumo ===="
Importance of components:
                          PC1    PC2    PC3     PC4     PC5
Standard deviation     1,5612 1,1862 0,7075 0,63248 0,50514
Proportion of Variance 0,4874 0,2814 0,1001 0,08001 0,05103
Cumulative Proportion  0,4874 0,7689 0,8690 0,94897 1,00000
In [4]:
#==============================================================================
# Correlacoes de Y_j com as variaveis X's
#==============================================================================
print(round(P[,1]*sdev[1],3))  # Correl. de Y_1 com X1,..., ou X_5
print(round(P[,2]*sdev[2],3))  # Correl. de Y_2 com X1,..., ou X_5
print(round(P[,3]*sdev[3],3))  # Correl. de Y_3 com X1,..., ou X_5
print(round(P[,4]*sdev[4],3))  # Correl. de Y_3 com X1,..., ou X_5
print(round(P[,5]*sdev[5],3))  # Correl. de Y_3 com X1,..., ou X_5
    JP   Citi  Fargo  Royal  Exxon 
-0,732 -0,831 -0,726 -0,605 -0,563 
    JP   Citi  Fargo  Royal  Exxon 
-0,437 -0,280 -0,374  0,694  0,719 
    JP   Citi  Fargo  Royal  Exxon 
-0,428 -0,096  0,546  0,066 -0,077 
    JP   Citi  Fargo  Royal  Exxon 
-0,230  0,398 -0,183  0,241 -0,312 
    JP   Citi  Fargo  Royal  Exxon 
 0,194 -0,251  0,036  0,300 -0,251 
In [5]:
#==============================================================================
# Scree plot
#==============================================================================
par(mfrow=c(1,1),lwd=2,cex.lab=1.5,cex.axis=1.5,lab=c(10,10,5),
    mar=c(4.5,5,2,2),bty="n")
par(mfrow=c(1,1),lwd=2.0,cex.lab=1.5,cex.axis=1.5,
     lab=c(5,7,5),mar=c(4.5,4.5,1,1),xpd=T,cex.main=2.0)
plot(1:5,sdev^2,type="l",xlab=expression(j),ylab="Variâncias",lwd=3,
    ylim=c(0,3))
points(1:5,sdev^2,pch=16,cex=2,col="blue")
No description has been provided for this image