R version 3.0.2 (2013-09-25) -- "Frisbee Sailing" Copyright (C) 2013 The R Foundation for Statistical Computing Platform: i686-pc-linux-gnu (32-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > x <- c(-0.236493005027621 + ,-4.93891480965441 + ,-3.95116239084637 + ,1.9753875025203 + ,-1.7798512823075 + ,2.37611897412902 + ,0.311940513005736 + ,-7.15597025619578 + ,8.46791076910932 + ,-1.41127366738798e-10 + ,4.90820820490978 + ,3.24776205176132 + ,4.99999999991935 + ,-3.06417846121247 + ,10.3119405129169 + ,-1.22014871767994 + ,-2.62388102585579 + ,-4.4402974353625 + ,-1.31194051292749 + ,4.6880594870725 + ,1.06417846121749 + ,2.84402974353625 + ,-3.22014871768125 + ,0.311940512927502 + ,-6.53208923060875 + ,2.376118974145 + ,-2 + ,1.90820820475374 + ,9.46791076939125 + ,3.6880594870725 + ,1.46791076939125 + ,11.4037323081738 + ,-4.37611897414499 + ,-5.84402974353625 + ,6.55970256463751 + ,5.22014871768124 + ,9.06417846121749 + ,-6.22014871768125 + ,-6.06417846121749 + ,-3.44029743536249 + ,-0.0917917952462566 + ,0.908208204753743 + ,9.22014871768125 + ,7.15597025646375 + ,-6.6880594870725 + ,-3.44029743536249 + ,2.09179179524626 + ,-1.09179179524626 + ,1.22014871768125 + ,2.15597025646375 + ,-4 + ,10.3119405129275 + ,-0.688059487072498 + ,5.376118974145 + ,4.55970256463751 + ,-1.15597025646375 + ,0.935821538782506 + ,-1.53208923060875 + ,7.84402974353625 + ,-6 + ,1.6880594870725 + ,-4.75223794828999 + ,0.0641784612174945 + ,2.3119405129275 + ,-7.93582153878251 + ,9.15597025646375 + ,-1.6880594870725 + ,-5.09179179524626 + ,4.40373230817376 + ,2.6880594870725 + ,0.376118974144996 + ,-3.53208923060875 + ,-8.53208923060875 + ,1.15597025646375 + ,9.376118974145 + ,-7.40373230817376 + ,-5.84402974353625 + ,1.55970256463751 + ,-8.93582153878251 + ,5.06417846121749 + ,-7 + ,2.75223794828999 + ,5.93582153878251 + ,-6.09179179524626 + ,-3.376118974145 + ,7.77985128231875 + ,6.22014871768125 + ,2.376118974145 + ,-5.90820820475374 + ,-7.90820820475374 + ,4.46791076939125 + ,-2.09179179524626 + ,-2.40373230817376 + ,2.623881025855 + ,1.84402974353625 + ,6.84402974353625 + ,-0.688059487072498 + ,-3.6880594870725 + ,3.09179179524626 + ,-0.155970256463751 + ,-0.623881025855004 + ,1.3119405129275 + ,-1 + ,2 + ,3.15597025646375 + ,-10.1559702564638 + ,-1.6880594870725 + ,-1.53208923060875) > ylimmax = '' > ylimmin = '' > main = 'Robustness of Central Tendency' > #'GNU S' R Code compiled by R2WASP v. 1.2.291 () > #Author: root > #To cite this work: Wessa, P., (2012), Central Tendency (v1.0.4) in Free Statistics Software (v$_version), Office for Research Development and Education, URL http://www.wessa.net/rwasp_centraltendency.wasp/ > #Source of accompanying publication: Office for Research, Development, and Education > # > geomean <- function(x) { + return(exp(mean(log(x)))) + } > harmean <- function(x) { + return(1/mean(1/x)) + } > quamean <- function(x) { + return(sqrt(mean(x*x))) + } > winmean <- function(x) { + x <-sort(x[!is.na(x)]) + n<-length(x) + denom <- 3 + nodenom <- n/denom + if (nodenom>40) denom <- n/40 + sqrtn = sqrt(n) + roundnodenom = floor(nodenom) + win <- array(NA,dim=c(roundnodenom,2)) + for (j in 1:roundnodenom) { + win[j,1] <- (j*x[j+1]+sum(x[(j+1):(n-j)])+j*x[n-j])/n + win[j,2] <- sd(c(rep(x[j+1],j),x[(j+1):(n-j)],rep(x[n-j],j)))/sqrtn + } + return(win) + } > trimean <- function(x) { + x <-sort(x[!is.na(x)]) + n<-length(x) + denom <- 3 + nodenom <- n/denom + if (nodenom>40) denom <- n/40 + sqrtn = sqrt(n) + roundnodenom = floor(nodenom) + tri <- array(NA,dim=c(roundnodenom,2)) + for (j in 1:roundnodenom) { + tri[j,1] <- mean(x,trim=j/n) + tri[j,2] <- sd(x[(j+1):(n-j)]) / sqrt(n-j*2) + } + return(tri) + } > midrange <- function(x) { + return((max(x)+min(x))/2) + } > q1 <- function(data,n,p,i,f) { + np <- n*p; + i <<- floor(np) + f <<- np - i + qvalue <- (1-f)*data[i] + f*data[i+1] + } > q2 <- function(data,n,p,i,f) { + np <- (n+1)*p + i <<- floor(np) + f <<- np - i + qvalue <- (1-f)*data[i] + f*data[i+1] + } > q3 <- function(data,n,p,i,f) { + np <- n*p + i <<- floor(np) + f <<- np - i + if (f==0) { + qvalue <- data[i] + } else { + qvalue <- data[i+1] + } + } > q4 <- function(data,n,p,i,f) { + np <- n*p + i <<- floor(np) + f <<- np - i + if (f==0) { + qvalue <- (data[i]+data[i+1])/2 + } else { + qvalue <- data[i+1] + } + } > q5 <- function(data,n,p,i,f) { + np <- (n-1)*p + i <<- floor(np) + f <<- np - i + if (f==0) { + qvalue <- data[i+1] + } else { + qvalue <- data[i+1] + f*(data[i+2]-data[i+1]) + } + } > q6 <- function(data,n,p,i,f) { + np <- n*p+0.5 + i <<- floor(np) + f <<- np - i + qvalue <- data[i] + } > q7 <- function(data,n,p,i,f) { + np <- (n+1)*p + i <<- floor(np) + f <<- np - i + if (f==0) { + qvalue <- data[i] + } else { + qvalue <- f*data[i] + (1-f)*data[i+1] + } + } > q8 <- function(data,n,p,i,f) { + np <- (n+1)*p + i <<- floor(np) + f <<- np - i + if (f==0) { + qvalue <- data[i] + } else { + if (f == 0.5) { + qvalue <- (data[i]+data[i+1])/2 + } else { + if (f < 0.5) { + qvalue <- data[i] + } else { + qvalue <- data[i+1] + } + } + } + } > midmean <- function(x,def) { + x <-sort(x[!is.na(x)]) + n<-length(x) + if (def==1) { + qvalue1 <- q1(x,n,0.25,i,f) + qvalue3 <- q1(x,n,0.75,i,f) + } + if (def==2) { + qvalue1 <- q2(x,n,0.25,i,f) + qvalue3 <- q2(x,n,0.75,i,f) + } + if (def==3) { + qvalue1 <- q3(x,n,0.25,i,f) + qvalue3 <- q3(x,n,0.75,i,f) + } + if (def==4) { + qvalue1 <- q4(x,n,0.25,i,f) + qvalue3 <- q4(x,n,0.75,i,f) + } + if (def==5) { + qvalue1 <- q5(x,n,0.25,i,f) + qvalue3 <- q5(x,n,0.75,i,f) + } + if (def==6) { + qvalue1 <- q6(x,n,0.25,i,f) + qvalue3 <- q6(x,n,0.75,i,f) + } + if (def==7) { + qvalue1 <- q7(x,n,0.25,i,f) + qvalue3 <- q7(x,n,0.75,i,f) + } + if (def==8) { + qvalue1 <- q8(x,n,0.25,i,f) + qvalue3 <- q8(x,n,0.75,i,f) + } + midm <- 0 + myn <- 0 + roundno4 <- round(n/4) + round3no4 <- round(3*n/4) + for (i in 1:n) { + if ((x[i]>=qvalue1) & (x[i]<=qvalue3)){ + midm = midm + x[i] + myn = myn + 1 + } + } + midm = midm / myn + return(midm) + } > (arm <- mean(x)) [1] 0.3292179 > sqrtn <- sqrt(length(x)) > (armse <- sd(x) / sqrtn) [1] 0.4796622 > (armose <- arm / armse) [1] 0.6863537 > (geo <- geomean(x)) [1] NaN Warning message: In log(x) : NaNs produced > (har <- harmean(x)) [1] -1.524176e-08 > (qua <- quamean(x)) [1] 4.972575 > (win <- winmean(x)) [,1] [,2] [1,] 0.33040644 0.4753957 [2,] 0.33788296 0.4740610 [3,] 0.33100068 0.4667908 [4,] 0.32862370 0.4660023 [5,] 0.34475822 0.4609332 [6,] 0.35495731 0.4581565 [7,] 0.35911703 0.4555537 [8,] 0.33805575 0.4443798 [9,] 0.29906319 0.4337902 [10,] 0.32200412 0.4286383 [11,] 0.27153407 0.4166973 [12,] 0.23994216 0.4109794 [13,] 0.21344278 0.4047939 [14,] 0.18132548 0.3967306 [15,] 0.15074927 0.3899034 [16,] 0.06783037 0.3787188 [17,] 0.16168732 0.3587389 [18,] 0.16117178 0.3518637 [19,] 0.18272242 0.3459803 [20,] 0.22349070 0.3361706 [21,] 0.19316315 0.3290310 [22,] 0.24363320 0.3159081 [23,] 0.23448555 0.3121164 [24,] 0.27869098 0.3030642 [25,] 0.14913020 0.2782110 [26,] 0.06523069 0.2631704 [27,] 0.04228275 0.2605923 [28,] 0.04228275 0.2566648 [29,] 0.01763495 0.2441681 [30,] 0.03546230 0.2360743 [31,] 0.14342238 0.2188506 [32,] 0.18963578 0.2091478 [33,] 0.20924587 0.1900723 [34,] 0.23814329 0.1868197 [35,] 0.30948778 0.1789306 [36,] 0.31869222 0.1733007 > (tri <- trimean(x)) [,1] [,2] [1,] 0.3236583 0.4668883 [2,] 0.3166506 0.4573543 [3,] 0.3054099 0.4474351 [4,] 0.2961972 0.4393020 [5,] 0.2872633 0.4303838 [6,] 0.2743270 0.4217022 [7,] 0.2588871 0.4125296 [8,] 0.2420784 0.4026710 [9,] 0.2276818 0.3937336 [10,] 0.2179479 0.3855487 [11,] 0.2048804 0.3770795 [12,] 0.1970897 0.3694110 [13,] 0.1923864 0.3615108 [14,] 0.1901998 0.3533642 [15,] 0.1910775 0.3451784 [16,] 0.1948981 0.3366966 [17,] 0.2064887 0.3284753 [18,] 0.2104418 0.3218822 [19,] 0.2146649 0.3151026 [20,] 0.2173350 0.3079282 [21,] 0.2168314 0.3008539 [22,] 0.2187333 0.2934527 [23,] 0.2167617 0.2865631 [24,] 0.2153746 0.2787826 [25,] 0.2104622 0.2707495 [26,] 0.2151935 0.2649710 [27,] 0.2267291 0.2601398 [28,] 0.2409173 0.2544042 [29,] 0.2562405 0.2477813 [30,] 0.2747530 0.2415420 [31,] 0.2934801 0.2350026 [32,] 0.3053615 0.2299773 [33,] 0.3146609 0.2252224 [34,] 0.3232857 0.2226085 [35,] 0.3304029 0.2194189 [36,] 0.3321956 0.2164892 > (midr <- midrange(x)) [1] 0.623881 > midm <- array(NA,dim=8) > for (j in 1:8) midm[j] <- midmean(x,j) > midm [1] 0.1600559 0.1600559 0.1600559 0.1600559 0.1600559 0.1600559 0.1600559 [8] 0.2151935 > postscript(file="/var/fisher/rcomp/tmp/1hu581387556831.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > lb <- win[,1] - 2*win[,2] > ub <- win[,1] + 2*win[,2] > if ((ylimmin == '') | (ylimmax == '')) plot(win[,1],type='b',main=main, xlab='j', pch=19, ylab='Winsorized Mean(j/n)', ylim=c(min(lb),max(ub))) else plot(win[,1],type='l',main=main, xlab='j', pch=19, ylab='Winsorized Mean(j/n)', ylim=c(ylimmin,ylimmax)) > lines(ub,lty=3) > lines(lb,lty=3) > grid() > dev.off() null device 1 > postscript(file="/var/fisher/rcomp/tmp/2t7671387556831.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > lb <- tri[,1] - 2*tri[,2] > ub <- tri[,1] + 2*tri[,2] > if ((ylimmin == '') | (ylimmax == '')) plot(tri[,1],type='b',main=main, xlab='j', pch=19, ylab='Trimmed Mean(j/n)', ylim=c(min(lb),max(ub))) else plot(tri[,1],type='l',main=main, xlab='j', pch=19, ylab='Trimmed Mean(j/n)', ylim=c(ylimmin,ylimmax)) > lines(ub,lty=3) > lines(lb,lty=3) > grid() > dev.off() null device 1 > > #Note: the /var/fisher/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/fisher/rcomp/createtable") > > a<-table.start() > a<-table.row.start(a) > a<-table.element(a,'Central Tendency - Ungrouped Data',4,TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,'Measure',header=TRUE) > a<-table.element(a,'Value',header=TRUE) > a<-table.element(a,'S.E.',header=TRUE) > a<-table.element(a,'Value/S.E.',header=TRUE) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,hyperlink('http://www.xycoon.com/arithmetic_mean.htm', 'Arithmetic Mean', 'click to view the definition of the Arithmetic Mean'),header=TRUE) > a<-table.element(a,arm) > a<-table.element(a,hyperlink('http://www.xycoon.com/arithmetic_mean_standard_error.htm', armse, 'click to view the definition of the Standard Error of the Arithmetic Mean')) > a<-table.element(a,armose) > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,hyperlink('http://www.xycoon.com/geometric_mean.htm', 'Geometric Mean', 'click to view the definition of the Geometric Mean'),header=TRUE) > a<-table.element(a,geo) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,hyperlink('http://www.xycoon.com/harmonic_mean.htm', 'Harmonic Mean', 'click to view the definition of the Harmonic Mean'),header=TRUE) > a<-table.element(a,har) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,hyperlink('http://www.xycoon.com/quadratic_mean.htm', 'Quadratic Mean', 'click to view the definition of the Quadratic Mean'),header=TRUE) > a<-table.element(a,qua) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > for (j in 1:length(win[,1])) { + a<-table.row.start(a) + mylabel <- paste('Winsorized Mean (',j) + mylabel <- paste(mylabel,'/') + mylabel <- paste(mylabel,length(win[,1])) + mylabel <- paste(mylabel,')') + a<-table.element(a,hyperlink('http://www.xycoon.com/winsorized_mean.htm', mylabel, 'click to view the definition of the Winsorized Mean'),header=TRUE) + a<-table.element(a,win[j,1]) + a<-table.element(a,win[j,2]) + a<-table.element(a,win[j,1]/win[j,2]) + a<-table.row.end(a) + } > for (j in 1:length(tri[,1])) { + a<-table.row.start(a) + mylabel <- paste('Trimmed Mean (',j) + mylabel <- paste(mylabel,'/') + mylabel <- paste(mylabel,length(tri[,1])) + mylabel <- paste(mylabel,')') + a<-table.element(a,hyperlink('http://www.xycoon.com/arithmetic_mean.htm', mylabel, 'click to view the definition of the Trimmed Mean'),header=TRUE) + a<-table.element(a,tri[j,1]) + a<-table.element(a,tri[j,2]) + a<-table.element(a,tri[j,1]/tri[j,2]) + a<-table.row.end(a) + } > a<-table.row.start(a) > a<-table.element(a,hyperlink('http://www.xycoon.com/median_1.htm', 'Median', 'click to view the definition of the Median'),header=TRUE) > a<-table.element(a,median(x)) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,hyperlink('http://www.xycoon.com/midrange.htm', 'Midrange', 'click to view the definition of the Midrange'),header=TRUE) > a<-table.element(a,midr) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > mymid <- hyperlink('http://www.xycoon.com/midmean.htm', 'Midmean', 'click to view the definition of the Midmean') > mylabel <- paste(mymid,hyperlink('http://www.xycoon.com/method_1.htm','Weighted Average at Xnp',''),sep=' - ') > a<-table.element(a,mylabel,header=TRUE) > a<-table.element(a,midm[1]) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > mymid <- hyperlink('http://www.xycoon.com/midmean.htm', 'Midmean', 'click to view the definition of the Midmean') > mylabel <- paste(mymid,hyperlink('http://www.xycoon.com/method_2.htm','Weighted Average at X(n+1)p',''),sep=' - ') > a<-table.element(a,mylabel,header=TRUE) > a<-table.element(a,midm[2]) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > mymid <- hyperlink('http://www.xycoon.com/midmean.htm', 'Midmean', 'click to view the definition of the Midmean') > mylabel <- paste(mymid,hyperlink('http://www.xycoon.com/method_3.htm','Empirical Distribution Function',''),sep=' - ') > a<-table.element(a,mylabel,header=TRUE) > a<-table.element(a,midm[3]) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > mymid <- hyperlink('http://www.xycoon.com/midmean.htm', 'Midmean', 'click to view the definition of the Midmean') > mylabel <- paste(mymid,hyperlink('http://www.xycoon.com/method_4.htm','Empirical Distribution Function - Averaging',''),sep=' - ') > a<-table.element(a,mylabel,header=TRUE) > a<-table.element(a,midm[4]) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > mymid <- hyperlink('http://www.xycoon.com/midmean.htm', 'Midmean', 'click to view the definition of the Midmean') > mylabel <- paste(mymid,hyperlink('http://www.xycoon.com/method_5.htm','Empirical Distribution Function - Interpolation',''),sep=' - ') > a<-table.element(a,mylabel,header=TRUE) > a<-table.element(a,midm[5]) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > mymid <- hyperlink('http://www.xycoon.com/midmean.htm', 'Midmean', 'click to view the definition of the Midmean') > mylabel <- paste(mymid,hyperlink('http://www.xycoon.com/method_6.htm','Closest Observation',''),sep=' - ') > a<-table.element(a,mylabel,header=TRUE) > a<-table.element(a,midm[6]) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > mymid <- hyperlink('http://www.xycoon.com/midmean.htm', 'Midmean', 'click to view the definition of the Midmean') > mylabel <- paste(mymid,hyperlink('http://www.xycoon.com/method_7.htm','True Basic - Statistics Graphics Toolkit',''),sep=' - ') > a<-table.element(a,mylabel,header=TRUE) > a<-table.element(a,midm[7]) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > mymid <- hyperlink('http://www.xycoon.com/midmean.htm', 'Midmean', 'click to view the definition of the Midmean') > mylabel <- paste(mymid,hyperlink('http://www.xycoon.com/method_8.htm','MS Excel (old versions)',''),sep=' - ') > a<-table.element(a,mylabel,header=TRUE) > a<-table.element(a,midm[8]) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.row.start(a) > a<-table.element(a,'Number of observations',header=TRUE) > a<-table.element(a,length(x)) > a<-table.element(a,'') > a<-table.element(a,'') > a<-table.row.end(a) > a<-table.end(a) > table.save(a,file="/var/fisher/rcomp/tmp/3pfkw1387556831.tab") > > try(system("convert tmp/1hu581387556831.ps tmp/1hu581387556831.png",intern=TRUE)) character(0) > try(system("convert tmp/2t7671387556831.ps tmp/2t7671387556831.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 3.597 0.890 4.451