[R] Adressing list-elements

Thomas Schwander r_lover at web.de
Wed Jan 14 20:27:48 CET 2009


Dear all,
I'm using R 2.8.1 under Vista.

I programmed a Simulation with the code enclosed at the end of the eMail.

After the simulation I want to analyse the columns of the single 
simulation-runs, i.e. e.g. Simulation[[1]][,1] sth. like that but I 
cannot address these columns...

Can anybody please help?

Best,
Thomas

############################ CODE ############################
analyse<-read.csv2("C:\\Users\\Thomas\\Desktop\\PCA_Kohle_2007.csv",sep=";")
rownames(analyse)<-analyse[,1]
analyse<-analyse[,-1]
require(tseries)
require(fSeries)
st_rendite<-apply(apply(analyse,2,log),2,diff)*100

pca<-princomp(st_rendite)
summary(pca)
summary(pca)$loadings
screeplot(pca,type="l",main="Screeplot der Prinzipalkomponenten")


loadings_pca1<--summary(pca)$loadings[1:6]
loadings_pca2<--summary(pca)$loadings[7:12]
loadings_pca3<--summary(pca)$loadings[13:18]
loadings_pca4<--summary(pca)$loadings[19:24]
loadings_pca5<--summary(pca)$loadings[25:30]
loadings_pca6<--summary(pca)$loadings[31:36]
loadmatrix<-as.matrix(cbind(loadings_pca1,loadings_pca2,loadings_pca3,loadings_pca4,loadings_pca5,loadings_pca6),princomp,dim(analyse)[2])
eigen<-summary(pca)$sdev

#Monte-Carlo-Simulation
trials<-1
Step<-1/365
princomp<-3
input1<-matrix(0,365,dim(analyse)[2])
Simulation<-list(input1)
shocklist<-list(input1)
Initialwerte<-analyse[dim(analyse)[1],]
days<-365

shockmatrix<-matrix(0,365,princomp)
for(i in 1:trials){
if(i==1) now<-Sys.time()
FP<-matrix(0,365,dim(analyse)[2])
  for(k in 1:days){
  shocks<-rnorm(princomp,0,1)
  shockmatrix[k,]<-shocks
  #shocks<-shocks_fak
  #  for(j in 1:1){
    for(j in 1:dim(analyse)[2]){
      if(k==1) {
        FP[k,j]<-as.numeric(Initialwerte[j])*
        exp(-0.5*Step*sum((loadmatrix[j,1:princomp]*eigen[1:princomp])^2)
        
+(sqrt(Step)*sum(loadmatrix[j,1:princomp]*eigen[1:princomp]*shocks)))
                } else {
        FP[k,j]<-FP[k-1,j]*
        exp(-0.5*Step*sum((loadmatrix[j,1:princomp]*eigen[1:princomp])^2)
        
+(sqrt(Step)*sum(loadmatrix[j,1:princomp]*eigen[1:princomp]*shocks)))
        }
    }
  }
  Simulation[[i]]<-list(FP)
  shocklist[[i]]<-list(shockmatrix)
}
endzeit<-Sys.time()
Berechnungsdauer<-endzeit-now
Berechnungsdauer

Simulation[[1]]
shocklist[[1]]




More information about the R-help mailing list