R/PCA7.R

#' A PCA main function
#'
#' This function allows you to run main function
#' @keywords PCA7
#' @export
#' @examples
#' PCA7()
#' @import ca
#' @import schoolmath

PCA7<-function(){
  set_directory=readline(prompt="Have you set your working director? Type y/n:")
  if(set_directory =="y"|set_directory=="Y"){
    print("Choose the data file in your browser.")
    the_file = read(file.choose())
    NXL = min(dim(the_file[[2]])[1]-1, dim(the_file[[2]])[2]-1,7)
    PR = matrix(NA,nrow=dim(the_file[[2]])[1],ncol = 7*NXL)
    PC = matrix(NA,nrow=dim(the_file[[2]])[2],ncol = 7*NXL)
    print("Read data. Analyses begin.")
    NCOL= the_file$attr[[1]]
    NAME_COL= the_file$attr[[2]]
    NROW=the_file$attr[[3]]
    NAME_ROW = the_file$attr[[4]]
    TITLE=the_file$attr[[5]]
  # MAIN FUNCTION STARTS HERE
    for(i in 1:7){
      anova_result = data_transformation(the_file[[2]],i)
      data = anova_result$C
      if (i==7){
        if (sum(data<0)!=0){
          print("Correspondence analysis is not available because some data are negative.")
          next
        }
     }
      pca_result = PCA(data,i)
      pr = pca_result$PR
      pc = pca_result$PC
      low = (NXL*i-NXL+1)
      upper = (NXL*i)
      if(is.null(dim(pr))){
        PR[,low:upper]= pr
      }
      else{
        PR[,low:upper]= pr[,1:NXL]
      }
      if(is.null(dim(pc))){
        PC[,low:upper]= pc
      }
      else{
        PC[,low:upper]= t(pc[1:NXL,])
      }
      write_table(i,anova_result,pca_result,NCOL,NAME_COL,NROW,NAME_ROW,TITLE,NXL)
      if (i==1){
        title="Grand Mean Centered"
      }else if(i==2){
        title=paste("Column"," Centered",sep="")
      }else if(i==3){
        title=paste("Row"," Centered",sep="")
      }else if(i==4){
        title= "Double Centered (AMMI)"
      }else if(i==5){
        title=paste("Column"," Standardized",sep="")
      }else if(i==6){
        title= paste("Row"," Standardized",sep="")
      }else if(i==7) {
        title="Correspondence Analysis" 
      }
      print(paste("Finished analysis",i,"of 7:",title))
    }
    print("Finished all 7 analyses.")
    print("Tables are ready in the working directory.")
  
    # Make Graphs
    flag=TRUE
    plot_request(flag,PC,PR,NCOL,NAME_COL,NROW,NAME_ROW,TITLE,NXL)
  }
  else if (set_directory == "n"|set_directory=="N"){
      print("Please set your working directory!")
      flag=FALSE
    }
  else{
      print("Invalid command")
    }
  }
sq77/PCA7 documentation built on May 15, 2019, 4:49 p.m.