[R] Adressing list-elements

Uwe Ligges ligges at statistik.tu-dortmund.de
Fri Jan 16 12:12:31 CET 2009



Thomas Schwander wrote:
> 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...


I guess at least line

  Simulation<-list(input1)

can be omitted or at least replaced by some sensible list initialization?


I also guess that you intend to assign
   Simulation[[i]] <- FP
rather than
   Simulation[[i]] <- list(FP)

but untested without your data.

Uwe Ligges




> 
> 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]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list