"confident"<-function(x,y= NULL,z,type = c("means","medians","proportions"),paired=FALSE,k=1,na.rm = FALSE) { if (!is.numeric(x) || is.complex(x)) stop("x is not numeric or x is complex.",call. = FALSE) if (!is.null(y) && (!is.numeric(y) || is.complex(y) )) stop("y is not numeric or y is complex.",call. = FALSE) if (z!="conf.curve" && !is.numeric(z)) stop("z has to be numeric or conf.curve.",call. = FALSE) if (!type=="means" && !type=="medians" && !type=="proportions") stop("Type has to be one of means, medians or proportions.",call. = FALSE) if (is.null(y) && na.rm){ if (length(x[is.na(x)]) > 0) warning("NAs have been removed.",call. = FALSE) x=x[!is.na(x)] } if (is.null(y) && (!na.rm) && length(x[is.na(x)])>=1) stop("Vector contains NAs. Set na.rm=TRUE and repeat.",call. = FALSE) if (!is.logical(na.rm)) stop("na.rm is logical.",call. = FALSE) if (is.numeric(z) & (z<0 || z>1)) stop("Level of confidence (z) needs to be in the boundaries of 0 and 1.",call. = FALSE) if (!is.null(y) && na.rm && !paired && (length(which(is.na(x)))>=1 | length(which(is.na(y)))>=1)){ x=x[!is.na(x)] y=y[!is.na(y)] warning("NAs have been removed.",call. = FALSE) } if (!is.null(y) && na.rm && paired && (length(which(is.na(x)))>=1 | length(which(is.na(x)))>=1)){ if(length(which(is.na(x)))>=1 && length(which(is.na(y)))==0){ B=which(is.na(x)) A=0 } else if(length(which(is.na(x)))==0 && length(which(is.na(y)))>=1){ B=0 A=which(is.na(y)) } else if(length(which(is.na(x)))>=0 && length(which(is.na(y)))>=1){ B=which(is.na(x)) A=which(is.na(y)) } x=x[c(-A,-B)] y=y[c(-A,-B)] warning("NAs have been removed.",call. = FALSE) } if (!is.null(y) && paired && length(x)!=length(y)) stop("For paired=TRUE, x and y have to be of same length.",call. = FALSE) if (!is.null(y) && !na.rm && (length(x[is.na(x)])>=1 || length(y[is.na(y)])>=1)) stop("x or y contains NAs. Set na.rm=TRUE and repeat.",call. = FALSE) if (!is.null(y) && !is.logical(paired)) stop("paired is logical.",call. = FALSE) if (z=="conf.curve") c(seq(0,0.8,0.01),seq(0.801,1,0.001))->z if ((k%%2!=0 && k%%2!=1) || k<1) stop("k needs to be natural number with minimum 1.",call. = FALSE) if (length(k)>1) stop("k has to be of length one.") if (k>1) warning(c("Note: confidence intervals were adjusted for ",paste(k)," comparisons."),call. = FALSE) if (type=="means" && is.null(y)){ if(length(x)<2) stop("Length of x too small. Mean with CIs requires at least length of 2.") final=data.frame() for (i in 1:length(z)){ conf=1-(1-z[i])/(2*k) mean=round(mean(x),3) Upper=round(mean+qt(conf,length(x)-1)*sd(x)/sqrt(length(x)),3) Lower=round(mean-qt(conf,length(x)-1)*sd(x)/sqrt(length(x)),3) temp=c(z[i],Lower,round(mean,3),Upper) final=rbind(final, temp,row.names=NULL) } Name=" Mean with CI(s)" Effectsize="Effect size (ES): Mean" } else if (type=="medians" && is.null(y)){ final=data.frame() for (i in 1:length(z)){ conf=1-(1-z[i])/(2*k) median=median(x) r=round(length(x)/2-(qnorm(conf)*sqrt(length(x))/2)) s=round(1+length(x)/2+(qnorm(conf)*sqrt(length(x))/2)) sort=sort(x) Lower=sort[r] Upper=sort[s] if (r<1 | s>length(x)){ temp=c(z[i],NA,round(median,3),NA) warning("One or more desired levels of confidence were too high for length(x).",call. = FALSE) }else temp=c(z[i],Lower,round(median,3),Upper) final=rbind(final, temp,row.names=NULL) } Name=" Median with CI(s)" Effectsize="Effect size (ES): Median" } else if (type=="proportions" && is.null(y)){ if(x[2]=2.",call. = FALSE) final=data.frame() for (i in 1:length(z)){ conf=1-(1-z[i])/(2*k) d=mean(x)-mean(y) sxy=sqrt(((length(x)-1)*sd(x)^2+(length(y)-1)*sd(y)^2)/(length(x)+length(y)-2)) sexy=sxy*sqrt(1/length(x)+1/length(y)) Lower=round((d-qt(conf,(length(x)+length(y)-2))*sexy),3) Upper=round((d+qt(conf,(length(x)+length(y)-2))*sexy),3) temp=c(z[i],Lower,round(d,3),Upper) final=rbind(final, temp,row.names=NULL) } Name=" Mean difference with CI(s); unpaired" Effectsize="Effect size (ES): mean(x) - mean(y)" } else if(type=="means" && paired && !is.null(y)){ if(length(x)<2 & length(y)<2) stop("Length of x and y needs to be >=2.",call. = FALSE) final=data.frame() for (i in 1:length(z)){ conf=1-(1-z[i])/(2*k) Diff=y-x meanDiff=mean(Diff) sdDiff=sd(Diff) Upper=round((meanDiff+qt(conf,(length(Diff)-1))*sdDiff/sqrt(length(Diff))),3) Lower=round((meanDiff-qt(conf,(length(Diff)-1))*sdDiff/sqrt(length(Diff))),3) temp=c(z[i],Lower,round(meanDiff,3),Upper) final=rbind(final, temp,row.names=NULL) } Name=" Mean difference with CI(s); paired" Effectsize="Effect size (ES): mean(y - x)" } else if(type=="medians" && !paired && !is.null(y)){ if(length(x)<5 & length(y)<5) stop("Length of x and y needs to be >=5.",call. = FALSE) final=data.frame() for (i in 1:length(z)){ conf=(1-z[i])/(2*k) W=qwilcox(conf,length(x),length(y)) Out=sort(as.vector(unlist(outer(x,y,"-")))) Lower=Out[W] Upper=Out[length(Out)-W+1] median=median(Out) if(W<1){ temp=c(z[i],NA,round(median,3),NA) warning("One or more desired levels of confidence were too high for length x or y.",call. = FALSE) } else temp=c(z[i],Lower,round(median,3),Upper) final=rbind(final, temp,row.names=NULL) } Name=" Median difference with CI(s); unpaired" Effectsize="Effect size (ES): Median of all possible differences x - y" } else if(type=="medians" && paired && !is.null(y)){ Out=sort(y-x) M=outer(Out,Out,"+")/2 Final=numeric() for(i in 1:length(Out)) { temp <-M[c(1:i),i] append(Final, temp) -> Final } Final=sort(Final) final=data.frame() for (i in 1:length(z)){ conf=(1-z[i])/(2*k) W=qsignrank(conf,length(x)) Lower=Final[W] Upper=Final[length(Final)-W+1] median=median(Final) if(W<1){ Temp=c(z[i],NA,round(median,3),NA) warning("One or more desired levels of confidence were too high for length of x and y.",call. = FALSE) } else Temp=c(z[i],Lower,round(median,3),Upper) final=rbind(final, Temp,row.names=NULL) } Name=" Median difference with CI(s); paired" Effectsize="Effect size (ES): Median of all possible averages of the paired differences y - x" } else if(type=="proportions" && !paired && !is.null(y)){ if(x[2]1)) t3=length(which(x>y)) s3=length(which(y>x)) u3=length(which((x+y)<1)) A=(r3+s3)*(t3+u3)*(r3+t3)*(s3+u3) B=r3*u3-s3*t3 if(r3+s3==0 | t3+u3==0 | r3+t3==0 | s3+u3==0){0->Psi}else if(B>n/2){(B-n/2)/sqrt(A)->Psi} else if(0<=B & B<=n/2){0->Psi} else if(B<0){B/sqrt(A)->Psi} Lower3=round(D-sqrt((p-Lower)^2-2*Psi*(p-Lower)*(Upper2-p2)+(Upper2-p2)^2),3) Upper3=round(D+sqrt((p2-Lower2)^2-2*Psi*(p2-Lower2)*(Upper-p)+(Upper-p)^2),3) Prop=D temp=c(z[i],Lower3,round(Prop,3),Upper3) final=rbind(final, temp,row.names=NULL) } Name=" Difference of proportions with CI(s); paired" Effectsize="Effect size (ES): Proportion of pairewise differences x - y." } names(final)=c("CI","LCL","ES","UCL") setClass("confidence", contains="data.frame") final<-as(final,"confidence") cat("","\n") cat(Name,"\n") cat("","\n") cat(Effectsize,"\n") cat("","\n") final }