R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree" Copyright (C) 2015 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-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(13408,7820,9079,8307,7865,10028,9054,7143,8006,7638,7600,2904,13224,8079,9678,7746,9007,8362,7458,7753,7352,7117,6971,3304,11812,6867,8296,6489,7784,7506,6514,6323,6201,7169,6744,2087,10668,6406,7730,7105,7694,7160,6820,6025,5877,7191,5778,2273,11321,6759,7150,6363,6442,6453,6228,5325,6504,6817,5789,1894,11068,7174,8269,7060,6681,8953,7815,5925,6805,7044,7169,2824,10717,5245,6237,5871,5508,15801,1236,2656,3425,3533,4287,1380,8584,5522,6423,5173,5583,5716,4752,4977,4999,5285,5747,1713,9923,6737,7433,6388,6855,7658,6585,6847,6353,7361,6929,1714,11798,8378,8131,7676,7505,8168,6455,6141,6554,6888,5339,1624,9187,5047,5289,4169,3862,4253,3768,3066,4108,3890,3420,1221,5984,4064,5151,4027,3530,4819,3855,3584,4322,4154,4656,1464,7780,5060,6084,4778,4989,4903,4142,4101,4595,5034,5407,1782,8395,5291,6116,4210,4621,5299,4293,4542,3831,4360,4088,1508,6743,4159,5105,4283,4019,4206,3948,3407,3701,4159,4208,2622,6229,4432,4986,4226,4349,4688,4002,3381,4250,4154,4350,2713) > 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] 5924.391 > sqrtn <- sqrt(length(x)) > (armse <- sd(x) / sqrtn) [1] 172.053 > (armose <- arm / armse) [1] 34.43352 > (geo <- geomean(x)) [1] 5409.435 > (har <- harmean(x)) [1] 4797.009 > (qua <- quamean(x)) [1] 6383.763 > (win <- winmean(x)) [,1] [,2] [1,] 5912.005 168.71504 [2,] 5911.589 168.05908 [3,] 5890.839 163.25954 [4,] 5891.464 163.07462 [5,] 5882.062 160.37080 [6,] 5876.938 158.60687 [7,] 5864.177 156.46603 [8,] 5864.969 155.74425 [9,] 5840.219 150.41079 [10,] 5844.802 148.26085 [11,] 5841.422 144.88231 [12,] 5832.547 138.06285 [13,] 5827.536 136.86314 [14,] 5829.870 136.13550 [15,] 5834.870 134.65399 [16,] 5837.036 133.32697 [17,] 5818.708 127.83999 [18,] 5823.302 123.41834 [19,] 5829.240 122.42880 [20,] 5830.281 121.96475 [21,] 5825.688 121.16751 [22,] 5825.000 120.97301 [23,] 5834.344 119.33816 [24,] 5822.094 117.96858 [25,] 5823.917 116.79794 [26,] 5832.719 114.50438 [27,] 5831.875 112.54646 [28,] 5820.500 109.62465 [29,] 5817.328 108.62109 [30,] 5817.641 108.44235 [31,] 5817.156 107.53584 [32,] 5826.156 106.57243 [33,] 5830.797 105.27762 [34,] 5832.568 104.88601 [35,] 5831.109 104.47601 [36,] 5831.297 103.21134 [37,] 5832.453 102.47066 [38,] 5831.464 101.90669 [39,] 5828.823 101.39995 [40,] 5827.990 100.03585 [41,] 5810.479 97.96893 [42,] 5810.260 97.94911 [43,] 5800.854 96.90186 [44,] 5795.125 96.39053 [45,] 5780.594 94.69395 [46,] 5787.302 93.71900 [47,] 5748.380 90.30344 [48,] 5744.630 89.90415 [49,] 5747.438 89.43363 [50,] 5753.688 88.87848 [51,] 5752.094 88.60880 [52,] 5757.510 87.66593 [53,] 5756.682 87.40814 [54,] 5751.057 86.65417 [55,] 5755.927 85.64052 [56,] 5750.677 83.87317 [57,] 5746.224 83.45971 [58,] 5727.193 81.41484 [59,] 5736.411 78.44917 [60,] 5757.974 74.46385 [61,] 5768.141 72.50092 [62,] 5772.661 71.48384 [63,] 5781.521 70.31002 [64,] 5783.188 68.70531 > (tri <- trimean(x)) [,1] [,2] [1,] 5897.163 163.99386 [2,] 5882.005 158.89280 [3,] 5866.737 153.74474 [4,] 5858.353 150.09909 [5,] 5849.621 146.22965 [6,] 5842.700 142.75401 [7,] 5836.545 139.40012 [8,] 5832.239 136.20786 [9,] 5827.724 132.89414 [10,] 5826.174 130.18911 [11,] 5824.071 127.59576 [12,] 5822.268 125.27137 [13,] 5821.277 123.64153 [14,] 5820.713 122.03162 [15,] 5819.938 120.38159 [16,] 5818.744 118.76593 [17,] 5817.354 117.16373 [18,] 5817.256 115.98753 [19,] 5816.838 115.13415 [20,] 5816.013 114.29728 [21,] 5815.100 113.42865 [22,] 5814.446 112.55328 [23,] 5813.815 111.61685 [24,] 5812.625 110.73383 [25,] 5812.092 109.88370 [26,] 5811.443 109.04905 [27,] 5810.304 108.31710 [28,] 5809.176 107.66490 [29,] 5808.597 107.16779 [30,] 5808.159 106.68772 [31,] 5807.692 106.16288 [32,] 5807.234 105.64292 [33,] 5806.333 105.13037 [34,] 5805.185 104.64868 [35,] 5803.918 104.13238 [36,] 5802.675 103.57919 [37,] 5801.381 103.04814 [38,] 5799.991 102.50049 [39,] 5798.596 101.92059 [40,] 5797.268 101.30027 [41,] 5795.927 100.69982 [42,] 5795.296 100.17453 [43,] 5794.651 99.57084 [44,] 5794.385 98.96055 [45,] 5794.353 98.29902 [46,] 5794.940 97.67444 [47,] 5795.265 97.03133 [48,] 5797.260 96.55916 [49,] 5799.500 96.02998 [50,] 5801.717 95.44369 [51,] 5803.767 94.80056 [52,] 5805.977 94.06526 [53,] 5808.058 93.28783 [54,] 5810.274 92.39595 [55,] 5812.841 91.41881 [56,] 5815.325 90.37289 [57,] 5818.167 89.32082 [58,] 5821.355 88.11691 [59,] 5825.568 86.90619 [60,] 5829.597 85.80700 [61,] 5832.871 84.95520 [62,] 5835.868 84.14422 [63,] 5838.833 83.26454 [64,] 5841.562 82.32505 > (midr <- midrange(x)) [1] 8511 > midm <- array(NA,dim=8) > for (j in 1:8) midm[j] <- midmean(x,j) > midm [1] 5780.876 5797.260 5780.876 5797.260 5797.260 5780.876 5797.260 5795.265 > postscript(file="/var/wessaorg/rcomp/tmp/129qr1452124398.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/22p2u1452124398.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/3j1at1452124398.tab") > > try(system("convert tmp/129qr1452124398.ps tmp/129qr1452124398.png",intern=TRUE)) character(0) > try(system("convert tmp/22p2u1452124398.ps tmp/22p2u1452124398.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 1.485 0.192 1.677