imply <- function() { testJ<-1 while(testJ==1) { testJ<-0 J <- readline("How many means? ") J <- suppressWarnings(as.integer(J)) if(is.na(J)) { cat("The number of means must be a positive integer value\n") testJ<-1 } else if(J<=0) { cat("The number of means must be a positive integer value\n") testJ<-1 } } testM<-1 while(testM==1) { testM<-0 m <- readline("How many Primary Contrasts? ") m <- suppressWarnings(as.integer(m)) if(is.na(m)) { cat("The number of Primary Contrasts must be a positive integer value\n") testM<-1 } else if(m<=0) { cat("The number of Primary Contrasts must be a positive integer value\n") testM<-1 } } C<-matrix(nrow=J,ncol=m) Wc<-array(dim=m) Mc<-array(dim=m) CI<-matrix(nrow=m,ncol=2) lab<-array(dim=m) cat("\nFor each Primary Contrast: 1. Enter a label/name for the contrast and then press enter 2. Enter the contrast coefficients, separated by commas, and then press enter 3. Enter the scaling option and then press enter \t0 - No rescaling \t1 - Mean difference scaling \t2 - Two-factor interaction \t3 - Three-factor interaction \t ... \tn - n-factor interaction 4. Enter the lower and upper limits of its CI, separated by a comma, and then press enter\n") testInverse<-TRUE while(testInverse) { testInverse<-FALSE for(i in 1:m) { cat("\nPRIMARY CONTRAST ", i,"\n") ### GET LABELS lab[i]<-readline("Label: ") ## GET COEFFICIENTS testCoef<-1 while(testCoef==1) { testCoef<-0 contrast<-readline("Coefficients: ") contrast<-suppressWarnings(as.numeric(unlist(strsplit(contrast, ",")))) if(length(contrast)!=J) { cat("The number of coefficients must equal", J, "(the number of means)\n") testCoef<-1 } else for(j in 1:J) { if(is.na(contrast[j])) { cat("The coefficients must be real numbers\n") testCoef<-1 } break } } ## GET SCALING CODE testCode<-1 while(testCode==1) { testCode<-0 scaleCode<-readline("Scaling Option: ") scaleCode<-suppressWarnings(as.integer(scaleCode)) if(is.na(scaleCode)) { cat("The Scaling Option must be either zero or a positive integer\n") testCode<-1 } else if(scaleCode<0) { cat("The Scaling Option must be either zero or a positive integer\n") testCode<-1 } } if(scaleCode!=0) { sumPos<-0 for(j in 1:J) if(contrast[j]>0) sumPos<-sumPos+contrast[j] scalingFact<-(sumPos/scaleCode) contrast<-contrast/scalingFact } C[,i]<-contrast ## GET CIs testCI<-1 while(testCI==1) { testCI<-0 ci<-readline("CI: ") ci<-suppressWarnings(as.numeric(unlist(strsplit(ci, ",")))) if(length(ci)!=2) { cat("Please enter exactly two numbers, separated by a comma\n") testCI<-1 } else for(j in 1:2) { if(is.na(ci[j])) { cat("Please enter exactly two numbers, separated by a comma\n") testCI<-1 } break } if(testCI==0) { if(ci[1]>=ci[2]) { cat("The lower limit must be smaller than the upper limit\n") testCI<-1 } } } Wc[i]<-ci[2]-ci[1] Mc[i]<-ci[2]-0.5*Wc[i] CI[i,1]<-ci[1] CI[i,2]<-ci[2] } inverse <- try(solve(t(C)%*%C), TRUE) if(inherits(inverse, "try-error")) { cat("\n****WARNING!****\n C is not invertable. Please re-enter the Primary Contrast information\n") testInverse<-TRUE } } colnames(C)<-lab colnames(CI)<-c("Lower","Upper") rownames(CI)<-lab Wc<-as.matrix(Wc) Mc<-as.matrix(Mc) rownames(Wc)<-lab rownames(Mc)<-lab testR<-1 while(testR==1) { testR<-0 r <- readline("\nHow many Secondary Contrasts? ") r <- suppressWarnings(as.integer(r)) if(is.na(r)) { cat("The number of Secondary Contrasts must be a positive integer value\n") testR<-1 } else if(r<=0) { cat("The number of Secondary Contrasts must be a positive integer value\n") testR<-1 } } D <- matrix(nrow=J,ncol=r) lab2<-array(dim=r) cat("\nFor each Secondary Contrast 1. Enter a label/name for the contrast and then press enter 2. Enter the contrast coefficients, separated by commas, and then press enter 3. Enter the scaling option and then press enter \t0 - No rescaling \t1 - Mean difference scaling \t2 - Two-factor interaction \t3 - Three-factor interaction \t ... \tn - n-factor interaction \n") for(i in 1:r) { cat("\nSECONDARY CONTRAST ", i,"\n") ### GET LABELS lab2[i]<-readline("Label: ") ## GET COEFFICIENTS testDed<-1 while(testDed==1) { testDed<-0 deduce<-readline("Coefficients: ") deduce<-suppressWarnings(as.numeric(unlist(strsplit(deduce, ",")))) if(length(deduce)!=J) { cat("The number of coefficients must equal", J, "(the number of means)\n") testDed<-1 } else for(j in 1:J) { if(is.na(deduce[j])) { cat("The coefficients must be real numbers\n") testDed<-1 } break } } ## GET SCALING CODE testCode<-1 while(testCode==1) { testCode<-0 scaleCode<-readline("Scaling Option: ") scaleCode<-suppressWarnings(as.integer(scaleCode)) if(is.na(scaleCode)) { cat("The Scaling Option must be either zero or a positive integer\n") testCode<-1 } else if(scaleCode<0) { cat("The Scaling Option must be either zero or a positive integer\n") testCode<-1 } } if(scaleCode!=0) { sumPos<-0 for(j in 1:J) if(deduce[j]>0) sumPos<-sumPos+deduce[j] scalingFact<-(sumPos/scaleCode) deduce<-deduce/scalingFact } D[,i]<-deduce } colnames(D)<-lab2 B<-solve(t(C)%*%C)%*%t(C)%*%D Md<-t(B)%*%Mc Wd<-abs(t(B))%*%Wc rownames(B)<-lab colnames(B)<-lab2 rownames(Md)<-lab2 rownames(Wd)<-lab2 DI<-matrix(nrow=r,ncol=2) for(i in 1:r) { DI[i,1]<-Md[i]-Wd[i]/2 DI[i,2]<-Md[i]+Wd[i]/2 } colnames(DI)<-c("Lower","Upper") rownames(DI)<-lab2 cat("\nRescaled Primary Contrasts:\n") print(t(C)) cat("\nConfidence Intervals:\n") print(CI) cat("\nMid-points of CIs:\n") print(Mc) cat("\nWidths of CIs:\n") print(Wc) cat("\nRescaled Secondary Contrasts:\n") print(t(D)) cat("\nSCs as linear combinations of PCs:\n") print(t(B)) cat("\nDeduced Intervals:\n") print(DI) cat("\nMid-points of DIs:\n") print(Md) cat("\nWidths of DIs:\n") print(Wd) }