R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows" Copyright (C) 2012 The R Foundation for Statistical Computing ISBN 3-900051-07-0 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(1.6,2,2.6,3,2.6,2.9,2.5,2.4,1.5,1.1,0.6,0.9,1.1,1.5,1.7,1.2,0.4,-0.7,-1.4,-1.6,-1.2,-0.4,-0.2,-0.3,-0.5,0,-0.5,0.2,0.7,1.6,2.6,3.3,3.3,3.2,3.5,3.9,4.5,4.6,6.6,7.1,8.9,8.8,8.5,7.6,7.5,7.5,6.1,6.3,8.4,7.1,5.6,4.2,2.1,1.2,0.9,1.4,1.7,1.7,1.9,1.3,-0.7,0.3,0.8,0.9,1.1,2.5,2.7,3.3,4.2,3.8,3.8,3.2,2.9,1.9,1.7,1.6,1.7,1.2,0.7,-0.2,-1.5,-1.2,-1,0,-0.6,0.7,1.3,0.8,1,0.5,0.3,1,1,1.1,1.5,1.5,2,1.7,0.6,1.2,1.5,2.1,3.2,3.9,4.6,4.2,4.4,3.7,3.7,2.8,2.9,3.9,3.1,3,2.8,2.4,2.1,3.1,3,3.1,3.3,3.3,3.8,3.1,3.9,4,4.4,3.7,3.6,3.4,2.8,2.8,2.6,3.3,2.4,1.6,0.7,0,-1.1,-1.2,-1.3,-1.6,-1.3,-1.6,-1.1,-1,0.3,1.2,0.7,1.1,2.1,2.5,2.3,2.3,2.6,3.2,2.2,2.7,2.2,1.4,2.4,2,1.3,1.1,1.4,1.8,1.9,1.6) > 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] 2.155952 > sqrtn <- sqrt(length(x)) > (armse <- sd(x) / sqrtn) [1] 0.1667095 > (armose <- arm / armse) [1] 12.93239 > (geo <- geomean(x)) [1] NaN Warning message: In log(x) : NaNs produced > (har <- harmean(x)) [1] 0 > (qua <- quamean(x)) [1] 3.047852 > (win <- winmean(x)) [,1] [,2] [1,] 2.155357 0.16656632 [2,] 2.151786 0.16573013 [3,] 2.151786 0.16508367 [4,] 2.135119 0.16066794 [5,] 2.135119 0.15967769 [6,] 2.135119 0.15967769 [7,] 2.122619 0.15587237 [8,] 2.122619 0.15587237 [9,] 2.095833 0.15091526 [10,] 2.083929 0.14701024 [11,] 2.070833 0.14479456 [12,] 2.042262 0.13812024 [13,] 1.964881 0.12730940 [14,] 1.989881 0.12394016 [15,] 1.980952 0.12282888 [16,] 1.980952 0.12043771 [17,] 1.991071 0.11915499 [18,] 1.969643 0.11663028 [19,] 1.980952 0.11521342 [20,] 1.992857 0.11375832 [21,] 1.980357 0.10942295 [22,] 1.967262 0.10799748 [23,] 1.994643 0.10479065 [24,] 1.994643 0.10479065 [25,] 1.994643 0.10479065 [26,] 2.010119 0.09966668 [27,] 2.026190 0.09794451 [28,] 2.026190 0.09794451 [29,] 2.008929 0.09609880 [30,] 2.026786 0.09422471 [31,] 2.045238 0.09234613 [32,] 2.045238 0.08843445 [33,] 2.025595 0.08639650 [34,] 2.025595 0.08237543 [35,] 2.004762 0.08032853 [36,] 2.004762 0.08032853 [37,] 2.004762 0.08032853 [38,] 2.004762 0.08032853 [39,] 2.027976 0.07810636 [40,] 2.027976 0.07810636 [41,] 2.027976 0.07347890 [42,] 2.027976 0.07347890 [43,] 2.027976 0.07347890 [44,] 2.054167 0.07111206 [45,] 2.027381 0.06856456 [46,] 2.027381 0.06856456 [47,] 2.055357 0.06609801 [48,] 2.055357 0.06609801 [49,] 2.026190 0.06337525 [50,] 2.026190 0.06337525 [51,] 2.026190 0.06337525 [52,] 1.995238 0.06056601 [53,] 2.026786 0.05781814 [54,] 2.026786 0.05781814 [55,] 1.994048 0.05489772 [56,] 1.994048 0.05489772 > (tri <- trimean(x)) [,1] [,2] [1,] 2.137952 0.16214279 [2,] 2.120122 0.15732924 [3,] 2.103704 0.15256639 [4,] 2.086875 0.14762566 [5,] 2.074051 0.14364492 [6,] 2.060897 0.13956560 [7,] 2.047403 0.13510473 [8,] 2.035526 0.13101811 [9,] 2.023333 0.12653502 [10,] 2.014189 0.12250455 [11,] 2.006164 0.11872899 [12,] 1.999306 0.11493257 [13,] 1.995070 0.11175236 [14,] 1.997857 0.10972482 [15,] 1.998551 0.10793144 [16,] 2.000000 0.10610452 [17,] 2.001493 0.10438154 [18,] 2.002273 0.10263961 [19,] 2.004615 0.10100191 [20,] 2.006250 0.09935663 [21,] 2.007143 0.09770171 [22,] 2.008871 0.09631972 [23,] 2.011475 0.09493469 [24,] 2.012500 0.09372140 [25,] 2.013559 0.09237883 [26,] 2.014655 0.09089142 [27,] 2.014912 0.08973241 [28,] 2.014286 0.08860292 [29,] 2.013636 0.08734311 [30,] 2.013889 0.08611111 [31,] 2.013208 0.08490866 [32,] 2.011538 0.08373412 [33,] 2.009804 0.08277733 [34,] 2.009000 0.08188554 [35,] 2.008163 0.08123641 [36,] 2.008333 0.08067243 [37,] 2.008511 0.08001744 [38,] 2.008696 0.07925917 [39,] 2.008889 0.07838323 [40,] 2.007955 0.07758471 [41,] 2.006977 0.07665708 [42,] 2.005952 0.07603467 [43,] 2.004878 0.07529731 [44,] 2.003750 0.07442700 [45,] 2.001282 0.07364027 [46,] 2.000000 0.07298161 [47,] 1.998649 0.07218731 [48,] 1.995833 0.07149306 [49,] 1.992857 0.07064686 [50,] 1.991176 0.06995343 [51,] 1.991176 0.06909771 [52,] 1.987500 0.06804746 [53,] 1.987097 0.06714247 [54,] 1.985000 0.06637613 [55,] 1.982759 0.06540571 [56,] 1.982143 0.06462214 > (midr <- midrange(x)) [1] 3.65 > midm <- array(NA,dim=8) > for (j in 1:8) midm[j] <- midmean(x,j) > midm [1] 2.006977 2.006977 2.006977 2.006977 2.006977 2.006977 2.006977 2.006977 > postscript(file="/var/wessaorg/rcomp/tmp/154rc1350127875.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/wessaorg/rcomp/tmp/2v61b1350127875.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/wessaorg/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/wessaorg/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/wessaorg/rcomp/tmp/3zsj21350127875.tab") > > try(system("convert tmp/154rc1350127875.ps tmp/154rc1350127875.png",intern=TRUE)) character(0) > try(system("convert tmp/2v61b1350127875.ps tmp/2v61b1350127875.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 1.846 0.333 2.155