存储矩阵的元素

时间:2015-04-30 09:06:39

标签: r for-loop

我想存储在以下R代码NBoot = 1000次结束时计算的matirces UE和RE的元素。所以我以后可能会回想起它们,比如UE [i]和RE [i]次i = 1到1000.我试图通过构造外部循环来解决,但有些我无法存储值。请建议我一些出路。 以下是我正在实施的示例代码:

set.seed(20531)
################ Setting up various quantities ################
NBoot=1000    
alpha=0.05
    c=0.5
    q=4
    ni=100
    n=q*ni;n
    ################ Setting Population Means and Standard Deviations ################
    mu0=c(5,5,5,5)
    sigma=c(1,1.5,2,2.5)
    ################ Creating Empty Matrix ################
    Dat=matrix(data=NA,nrow=ni,ncol=q)
    ################ Setting Up Loops ################
    for(j in 1:q){
       for(i in 1:ni){
          Dat[i,j]=rnorm(1,mu0[j],sigma[j])
       }
    }
    ################ Giving Names to Rows and Columns of DATA Matrix ################
    dimnames(Dat) <- list(rownames(Dat, do.NULL = FALSE, prefix = "Obs "),colnames(Dat, do.NULL = FALSE, prefix = "Sample "))
    ################ Visual Comparison via Box Plots ################
    boxplot(Dat)
    ################ Computation of Means and Variances of q Samples ################
    mx=apply(Dat,2,mean);mx
    vx=apply(Dat,2,var);vx
    ################ Computing Some Matrix Quantities ################
    Dn=diag(q,x=ni/vx);Dn
    Vn=diag(q,x=vx/(ni/n));Vn
    ################ Computing Vector and Matrix of 1's  and Associated Quantities################
    onq=matrix(rep(1,q),nrow=q)
    Iq=diag(q)
    Jq=onq%*%t(onq)
    Vn_1=solve(Vn)
    Wn=sum(ni/vx)
    Hn=Wn^(-1)*(Jq%*%Dn)
    Cn=Iq-Hn
    ################ Computing Unrestricted and Restricted Mean Vectors ################
    UE=as.matrix(mx);UE
    RE=Hn%*%UE;RE

1 个答案:

答案 0 :(得分:0)

可能你想要这样的东西(我删除了boxplot并优化了内部以生成值):

set.seed(20531)
NBoot=1000    

GenerateSamples <- function (){
  ################ Setting up various quantities ################
  alpha=0.05
  #c=0.5
  q=4
  ni=100
  n=q*ni;n
  ################ Setting Population Means and Standard Deviations ################
  mu0=c(5,5,5,5)
  sigma=c(1,1.5,2,2.5)
  ################ Creating Empty Matrix ################
  Dat=matrix(data=NA,nrow=ni,ncol=q)
  ################ Setting Up Loops ################
  for(j in 1:q){
    Dat[,j]<- rnorm(ni,mu0[j],sigma[j])
  }

  ################ Giving Names to Rows and Columns of DATA Matrix ################
  dimnames(Dat) <- list(rownames(Dat, do.NULL = FALSE, prefix = "Obs "),colnames(Dat, do.NULL = FALSE, prefix = "Sample "))
  ################ Visual Comparison via Box Plots ################
  #boxplot(Dat)
  ################ Computation of Means and Variances of q Samples ################
  mx=apply(Dat,2,mean)
  vx=apply(Dat,2,var)
  ################ Computing Some Matrix Quantities ################
  Dn=diag(q,x=ni/vx)
  Vn=diag(q,x=vx/(ni/n))
  ################ Computing Vector and Matrix of 1's  and Associated Quantities################
  onq=matrix(rep(1,q),nrow=q)
  Iq=diag(q)
  Jq=onq%*%t(onq)
  Vn_1=solve(Vn)
  Wn=sum(ni/vx)
  Hn=Wn^(-1)*(Jq%*%Dn)
  Cn=Iq-Hn
  ################ Computing Unrestricted and Restricted Mean Vectors ################
  UE=as.matrix(mx)
  RE=Hn%*%UE

  return (list(UE, RE))
}

UEs <- array(NA, 1000)
REs <- array(NA, 1000)
for (i in 1:NBoot){
   my_result <- GenerateSamples()
   UEs[i] <- my_result[1]
   REs[i] <- my_result[2]
}