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(283411 + ,234585 + ,207695 + ,204000 + ,220791 + ,222335 + ,303510 + ,201602 + ,218279 + ,207253 + ,217639 + ,231043 + ,302988 + ,221415 + ,213989 + ,190507 + ,199397 + ,201077 + ,261048 + ,241508 + ,194637 + ,240044 + ,253534 + ,269220 + ,270820 + ,354121 + ,315233 + ,297098 + ,258289 + ,276246 + ,246652 + ,242945 + ,210838 + ,246343 + ,234636 + ,229011 + ,238756 + ,283696 + ,226656 + ,229790 + ,219831 + ,232331 + ,315447 + ,251031 + ,297019 + ,208424 + ,311626 + ,352320 + ,375214 + ,419501 + ,664867 + ,483142 + ,312717 + ,228228 + ,230978 + ,218033 + ,225566 + ,207708 + ,241861 + ,208381 + ,209071 + ,214514 + ,195868 + ,190208 + ,187651 + ,215934 + ,213012 + ,236845 + ,154595 + ,193485 + ,231875 + ,192259 + ,191609 + ,212911 + ,238596 + ,203033 + ,258272 + ,314681 + ,289789 + ,297541 + ,239578 + ,207798 + ,254091 + ,242544 + ,218067 + ,217463 + ,229438 + ,216485 + ,238410 + ,223108 + ,221267 + ,224802 + ,233630 + ,235134 + ,309660 + ,253229 + ,293530 + ,273323 + ,297578 + ,341589 + ,389911 + ,398685 + ,629637 + ,522502 + ,358941 + ,252783 + ,236585 + ,224995 + ,225721 + ,223338 + ,228952 + ,212759 + ,200270 + ,211925 + ,191206 + ,200511 + ,205944 + ,289288 + ,186209 + ,197515 + ,182038 + ,181606 + ,223337 + ,201213 + ,220700 + ,194043 + ,224593 + ,249907 + ,238613 + ,266200 + ,274197 + ,288601 + ,242662 + ,232105 + ,267842 + ,219037 + ,198404 + ,203910 + ,209685 + ,214274 + ,215376 + ,217711 + ,224819 + ,217142 + ,221445 + ,213483 + ,282063 + ,236194 + ,297296 + ,255885 + ,264502 + ,311580 + ,416595 + ,356762 + ,537286 + ,532855) > 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] 256672.9 > sqrtn <- sqrt(length(x)) > (armse <- sd(x) / sqrtn) [1] 6322.25 > (armose <- arm / armse) [1] 40.59835 > (geo <- geomean(x)) [1] 248078 > (har <- harmean(x)) [1] 241698.4 > (qua <- quamean(x)) [1] 268470.6 > (win <- winmean(x)) [,1] [,2] [1,] 256620.2 6215.690 [2,] 255441.8 5798.902 [3,] 255436.8 5765.934 [4,] 255208.3 5681.590 [5,] 254028.7 5307.613 [6,] 251592.5 4678.867 [7,] 251493.4 4646.190 [8,] 250595.6 4440.197 [9,] 250126.9 4329.775 [10,] 249263.4 4132.088 [11,] 248155.3 3910.718 [12,] 248033.4 3876.163 [13,] 247915.9 3827.489 [14,] 247902.1 3785.810 [15,] 246955.7 3598.989 [16,] 244376.3 3166.209 [17,] 244448.2 3154.174 [18,] 244412.3 3142.459 [19,] 244242.0 3101.968 [20,] 244119.6 3080.555 [21,] 244165.7 3074.988 [22,] 244096.8 3019.002 [23,] 243319.3 2882.984 [24,] 243252.9 2870.964 [25,] 242697.4 2729.206 [26,] 242909.4 2709.666 [27,] 242943.5 2697.681 [28,] 242910.3 2692.867 [29,] 242912.4 2689.551 [30,] 242353.5 2593.832 [31,] 241618.7 2499.556 [32,] 241648.6 2475.467 [33,] 241633.2 2446.459 [34,] 240815.4 2294.576 [35,] 240995.4 2266.492 [36,] 240876.8 2213.186 [37,] 239533.1 2047.455 [38,] 239058.6 1988.050 [39,] 238957.9 1953.260 [40,] 238445.8 1870.050 [41,] 238100.2 1817.002 [42,] 237793.8 1770.721 [43,] 237578.8 1701.298 [44,] 237257.3 1636.338 [45,] 236419.9 1517.187 [46,] 235800.1 1416.444 [47,] 235891.6 1407.726 [48,] 235211.3 1328.572 [49,] 234670.5 1270.553 [50,] 234595.1 1244.107 [51,] 234506.5 1233.373 [52,] 234428.5 1212.761 > (tri <- trimean(x)) [,1] [,2] [1,] 254685.1 5786.148 [2,] 252699.1 5290.099 [3,] 251273.0 4987.804 [4,] 249810.0 4658.739 [5,] 248368.0 4312.217 [6,] 247141.5 4033.072 [7,] 246326.5 3883.306 [8,] 245504.1 3723.545 [9,] 244784.6 3587.832 [10,] 244103.7 3457.527 [11,] 243503.0 3347.291 [12,] 243003.2 3260.943 [13,] 242500.2 3170.428 [14,] 241992.4 3076.882 [15,] 241469.8 2978.377 [16,] 241009.7 2894.794 [17,] 240740.7 2857.400 [18,] 240457.1 2817.155 [19,] 240166.7 2773.715 [20,] 239878.2 2729.911 [21,] 239588.0 2683.462 [22,] 239284.4 2632.076 [23,] 238974.2 2580.631 [24,] 238701.3 2538.162 [25,] 238422.2 2491.705 [26,] 238165.7 2454.602 [27,] 237886.6 2414.357 [28,] 237594.4 2369.887 [29,] 237292.2 2319.866 [30,] 236977.3 2263.180 [31,] 236977.3 2209.778 [32,] 236409.7 2160.059 [33,] 236126.0 2105.571 [34,] 235830.1 2046.077 [35,] 235564.2 1996.217 [36,] 235276.0 1941.277 [37,] 234980.0 1883.827 [38,] 234740.0 1839.124 [39,] 234512.7 1794.636 [40,] 234278.8 1746.725 [41,] 234059.2 1701.562 [42,] 233845.6 1655.447 [43,] 233636.1 1607.237 [44,] 233425.8 1559.478 [45,] 233425.8 1511.847 [46,] 233046.6 1473.071 [47,] 232896.0 1441.404 [48,] 232730.3 1403.576 [49,] 232591.3 1370.425 [50,] 232473.1 1339.271 [51,] 232350.4 1304.664 [52,] 232223.6 1263.058 > (midr <- midrange(x)) [1] 409731 > midm <- array(NA,dim=8) > for (j in 1:8) midm[j] <- midmean(x,j) > midm [1] 234240.6 234512.7 234240.6 234512.7 234512.7 234240.6 234512.7 234740.0 > postscript(file="/var/wessaorg/rcomp/tmp/14vvv1457101052.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/272w21457101052.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/3vv711457101052.tab") > > try(system("convert tmp/14vvv1457101052.ps tmp/14vvv1457101052.png",intern=TRUE)) character(0) > try(system("convert tmp/272w21457101052.ps tmp/272w21457101052.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 1.252 0.145 1.404