R/write_table.R

#' A making tables function
#'
#' This function allows you to make tables
#' @keywords table
#' @examples
#' write_table()

write_table <- function(variants,anova_result,pca_result,NCOL,NAME_COL,NROW,NAME_ROW,TITLE,NXL){
  options(scipen=20)
  data = anova_result$C
  result = pca_result
  # Grand Mean Centered
  if(variants == 1){
    SSTOTAL=rep(0,NXL);ROWSUM=rep(0,NXL);COLSUM=rep(0,NXL);INTERSUM=rep(0,NXL)
    df=rep(0,NXL)
    for(i in 1:NXL){
      C=as.matrix(result$PR[,i])%*%result$PC[i,]
      SSTOTAL[i]=sum(C^2)
      df[i]=NROW+NCOL-2*i+1
      ROWSUM[i] = sum(rowMeans(C)^2)*NCOL
      COLSUM[i] = sum(colMeans(C)^2)*NROW
      INTERSUM[i]=SSTOTAL[i]-ROWSUM[i]-COLSUM[i]
    }
    df_res= anova_result$DDFT-sum(df)
    SSTOTAL_RES=anova_result$DSST-sum(SSTOTAL)
    COLS_RES=anova_result$DSSC-sum(COLSUM)
    ROW_RES=anova_result$DSSR-sum(ROWSUM)
    INTER_RES=anova_result$DSSRXC-sum(INTERSUM)
    
    Source = c('Total')
    for(i in 1:NXL){
      chr = paste(" ",paste("PC",i,sep = ''))
      Source = c(Source,chr)
    }
    Source = c(Source,paste(" ","Residual"))
    
    df<-round(c(anova_result$DDFT,df,df_res),5)
    SS<-formatC(as.numeric(c(anova_result$DSST,SSTOTAL,SSTOTAL_RES)),digits = 5,format = 'f')
    COLS<-formatC(as.numeric(c(anova_result$DSSC,COLSUM,COLS_RES)),digits = 5,format = 'f')
    ROWS<-formatC(as.numeric(c(anova_result$DSSR,ROWSUM,ROW_RES)),digits = 5,format = 'f')
    CxR<-formatC(as.numeric(c(anova_result$DSSRXC,INTERSUM,INTER_RES)),digits = 5,format = 'f')
    table=format(data.frame(df,SS,COLS,ROWS,CxR,row.names = Source),justify="right",digits=5)
    colnames(table)<-c("df","SS",'SNPs','Individuals','SxI')
    name=paste("Augmented ANOVA for",TITLE,"analysis 1: Grand Mean Centered")
    cat(name,file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("-----------------------------------------------",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat(capture.output(table), file = paste(TITLE,"anova6.txt",sep=""),append= TRUE, sep = '\n')
    cat("-----------------------------------------------",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("                                               ",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("                                               ",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
  }
  
  # Column Centered
  
  if(variants == 2){
    SSTOTAL=rep(0,NXL)
    ROWSUM=rep(0,NXL)
    INTERSUM=rep(0,NXL)
    df=rep(0,NXL)
    for(i in 1:NXL){
      C=as.matrix(result$PR[,i])%*%result$PC[i,]
      SSTOTAL[i]=sum(C^2)
      df[i]=NROW+NCOL-2*i
      ROWSUM[i] = sum(rowMeans(C)^2)*NCOL
      INTERSUM[i]=SSTOTAL[i]-ROWSUM[i]
    }
    df_res= anova_result$DDFT-anova_result$DDFC-sum(df)
    SSTOTAL_RES=anova_result$DSSR+anova_result$DSSRXC-sum(SSTOTAL)
    ROW_RES=anova_result$DSSR-sum(ROWSUM)
    INTER_RES=anova_result$DSSRXC-sum(INTERSUM)
    
    Source = c('Total',paste(" ",'SNP'), paste(" ","I&SxI"))
    for(i in 1:NXL){
      chr = paste("  ",paste("PC",i,sep = ''))
      Source = c(Source,chr)
    }
    Source = c(Source,paste("  ","Residual"))
    
    df<-round(c(anova_result$DDFT,anova_result$DDFC,anova_result$DDFT-anova_result$DDFC,df,df_res),5)
    SS<-formatC(as.numeric(c(anova_result$DSST,anova_result$DSSC,anova_result$DSSR+anova_result$DSSRXC,SSTOTAL,SSTOTAL_RES)),digits = 5,format = 'f')
    ROWS<-formatC(as.numeric(c("    ","    ",anova_result$DSSR,ROWSUM,ROW_RES)),digits = 5,format = 'f')
    CxR<-formatC(as.numeric(c("   ","   ",anova_result$DSSRXC,INTERSUM,INTER_RES)),digits = 5,format = 'f')
    
    table=format(data.frame(df,SS,ROWS,CxR,row.names = Source),justify="right",digits=11)
    colnames(table)<-c("df","SS",'Individuals','SxI')
    name=paste("Augmented ANOVA for",TITLE,"analysis 2: Column Centered")
    cat(name,file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("-----------------------------------------------",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat(capture.output(table), file = paste(TITLE,"anova6.txt",sep=""),append= TRUE, sep = '\n')
    cat("-----------------------------------------------",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("                                               ",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("                                               ",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
  }
  # Row Centered
  
  if(variants == 3){
    SSTOTAL=rep(0,NXL)
    COLSUM=rep(0,NXL)
    INTERSUM=rep(0,NXL)
    df=rep(0,NXL)
    for(i in 1:NXL){
      C=as.matrix(result$PR[,i])%*%result$PC[i,]
      SSTOTAL[i]=sum(C^2)
      df[i]=NROW+NCOL-2*i
      COLSUM[i] = sum(colMeans(C)^2)*NROW
      INTERSUM[i]=SSTOTAL[i]-COLSUM[i]
    }
    df_res= anova_result$DDFT-anova_result$DDFR-sum(df)
    SSTOTAL_RES=anova_result$DSSC+anova_result$DSSRXC-sum(SSTOTAL)
    COLS_RES=anova_result$DSSC-sum(COLSUM)
    INTER_RES=anova_result$DSSRXC-sum(INTERSUM)
    
    Source = c("Total",paste(" ",'Individuals'), paste(" ","S&SxI"))
    for(i in 1:NXL){
      chr = paste("  ",paste("PC",i,sep = ''))
      Source = c(Source,chr)
    }
    Source = c(Source,paste("  ","Residual"))
    
    df<-round(c(anova_result$DDFT,anova_result$DDFR,anova_result$DDFT-anova_result$DDFR,df,df_res),5)
    SS<-formatC(as.numeric(c(anova_result$DSST,anova_result$DSSR,anova_result$DSSC+anova_result$DSSRXC,SSTOTAL,SSTOTAL_RES)),digits = 5,format = 'f')
    COLS<-formatC(as.numeric(c("    ","    ",anova_result$DSSC,COLSUM,COLS_RES)),digits = 5,format = 'f')
    CxR<-formatC(as.numeric(c("    ","    ",anova_result$DSSRXC,INTERSUM,INTER_RES)),digits = 5,format = 'f')
    
    table=format(data.frame(df,SS,COLS,CxR,row.names = Source),justify="right",digits=11)
    colnames(table)<-c("df","SS",'SNPs','SxI')
    name=paste("Augmented ANOVA for",TITLE,"analysis 3: Row Centered")
    cat(name,file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("-----------------------------------------------",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat(capture.output(table), file = paste(TITLE,"anova6.txt",sep=""),append= TRUE, sep = '\n')
    cat("-----------------------------------------------",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("                                               ",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("                                               ",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
  }
  
  
  # Double Centered (AMMI)
  
  if(variants == 4){
    EV=rep(0,NXL)
    df=rep(0,NXL)
    for (i in 1:NXL){
      EV[i]=result$EV[i]
      df[i]=NCOL+NROW-2*i-1
    }
    EV_RES=anova_result$DSSRXC-sum(EV)
    df_res=anova_result$DDFRXC-sum(df)
    
    Source = c("Total",paste(" ",'SNPs'),paste(" ",'Individuals'),paste(" ",'SxI'))
    for(i in 1:NXL){
      chr = paste("  ",paste("IPC",i,sep = ''))
      Source = c(Source,chr)
    }
    Source = c(Source,paste("  ","Residual"))
    
    df<-round(c(anova_result$DDFT,anova_result$DDFC,anova_result$DDFR,anova_result$DDFRXC,df,df_res),5)
    SS<-formatC(as.numeric(c(anova_result$DSST,anova_result$DSSC,anova_result$DSSR,anova_result$DSSRXC,EV,EV_RES)),digits = 5,format = 'f')
    table=format(data.frame(df,SS,row.names = Source),justify="right",digits=11)
    name=paste("ANOVA for",TITLE,"analysis 4: Double Centered (AMMI)")
    cat(name,file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("-----------------------------------------------",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat(capture.output(table), file = paste(TITLE,"anova6.txt",sep=""),append= TRUE, sep = '\n')
    cat("-----------------------------------------------",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("                                               ",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("                                               ",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
  }
  
  # Column Standardized
  
  if(variants == 5){
    SSTOTAL=rep(0,NXL)
    ROWSUM=rep(0,NXL)
    INTERSUM=rep(0,NXL)
    df=rep(0,NXL)
    SSR=0
    for(row in 1:NROW){
      SSR=SSR+sum((mean(data[row, ])^2))*NCOL
    }
    for(i in 1:NXL){
      C=as.matrix(result$PR[,i])%*%result$PC[i,]
      SSTOTAL[i]=sum(C^2)
      df[i]=NROW+NCOL-2*i
      ROWSUM[i] = sum(rowMeans(C)^2)*NCOL
      INTERSUM[i]=SSTOTAL[i]-ROWSUM[i]
    }
    df_res= anova_result$DDFT-anova_result$DDFC-sum(df)
    SSTOTAL_RES=sum(data^2)-sum(SSTOTAL)
    ROW_RES=SSR-sum(ROWSUM)
    INTER_RES=sum(data^2)-SSR-sum(INTERSUM)
    
    Source = c("I&SxI")
    for(i in 1:NXL){
      chr = paste(" ",paste("PC",i,sep = ''))
      Source = c(Source,chr)
    }
    Source = c(Source,paste(" ","Residual"))
    
    df<-round(c(anova_result$DDFT-anova_result$DDFC,df,df_res),5)
    SS<-formatC(as.numeric(c(sum(data^2),SSTOTAL,SSTOTAL_RES)),digits = 5,format='f')
    ROWS<-formatC(as.numeric(c(SSR,ROWSUM,ROW_RES)),digits = 5,format='f')
    CxR<-formatC(as.numeric(c(sum(data^2)-SSR,INTERSUM,INTER_RES)),digits = 5,format='f')
    
    table=format(data.frame(df,SS,ROWS,CxR,row.names = Source),justify="right",digits=11)
    colnames(table)<-c("df","SS",'Individuals',"SxI")
    name=paste("Augmented ANOVA for",TITLE,"analysis 5: Column Standardized")
    cat(name,file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("-----------------------------------------------",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat(capture.output(table), file = paste(TITLE,"anova6.txt",sep=""),append= TRUE, sep = '\n')
    cat("-----------------------------------------------",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("                                               ",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("                                               ",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
  }
  
  # Row Standardized
  
  if(variants == 6){
    SSTOTAL=rep(0,NXL)
    COLSUM=rep(0,NXL)
    INTERSUM=rep(0,NXL)
    df=rep(0,NXL)
    SSC=0
    for(col in 1:NCOL){
      SSC=SSC+sum((mean(data[,col]))^2)*NROW
    }
    for(i in 1:NXL){
      C=as.matrix(result$PR[,i])%*%result$PC[i,]
      SSTOTAL[i]=sum(C^2)
      df[i]=NROW+NCOL-2*i
      COLSUM[i] = sum(colMeans(C)^2)*NROW
      INTERSUM[i]=SSTOTAL[i]-COLSUM[i]
    }
    df_res= anova_result$DDFT-anova_result$DDFR-sum(df)
    SSTOTAL_RES=sum(data^2)-sum(SSTOTAL)
    COLS_RES=SSC-sum(COLSUM)
    INTER_RES=sum(data^2)-SSC-sum(INTERSUM)
    
    Source = c("S&SxI")
    for(i in 1:NXL){
      chr = paste(" ",paste("PC",i,sep = ''))
      Source = c(Source,chr)
    }
    Source = c(Source,paste(" ","Residual"))
    
    df<-round(c(anova_result$DDFT-anova_result$DDFR,df,df_res),5)
    SS<-formatC(as.numeric(c(sum(data^2),SSTOTAL,SSTOTAL_RES)),digits = 5,format = 'f')
    COLS<-formatC(as.numeric(c(SSC,COLSUM,COLS_RES)),digits = 5,format = 'f')
    CxR<-formatC(as.numeric(c(sum(data^2)-SSC,INTERSUM,INTER_RES)),digits = 5,format = 'f')
    
    table=format(data.frame(df,SS,COLS,CxR,row.names = Source),justify="right",digits=11)
    colnames(table)<-c("df","SS",'SNPs',"SxI")
    name=paste("Augmented ANOVA for",TITLE,"analysis 6: Row Standardized")
    cat(name,file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("-----------------------------------------------",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat(capture.output(table), file = paste(TITLE,"anova6.txt",sep=""),append= TRUE, sep = '\n')
    cat("-----------------------------------------------",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("                                               ",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
    cat("                                               ",file= paste(TITLE,"anova6.txt",sep=""),append = TRUE,sep='\n')
  }
}
sq77/PCA7 documentation built on May 15, 2019, 4:49 p.m.