R version 2.12.0 (2010-10-15) Copyright (C) 2010 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i486-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(61,81,87,87,136,147,168,185,137,125,64,45,35,-4,88,85,95,128,186,182,151,106,60,44,30,54,72,88,153,168,181,180,149,84,85,42,54,30,96,110,141,159,164,155,135,93,28,56,56,22,76,83,121,151,208,179,139,99,103,57,44,70,58,91,126,146,199,194,145,131,74,-3,7,10,34,94,105,151,162,175,128,115,62,11,-7,64,80,77,127,158,173,206,147,103,73,52,52,68,77,94,147,160,166,167,155,104,44,53,56,36,76,99,142,150,190,176,175,112,73,52,48,61,68,97,146,160,155,175,163,117,82,55,32,48,53,82,139,150,184,185,138,147,77,32,48,72,76,94,133,164,174,187,149,102,86,35,31,28,75,102,133,178,190,190,147,83,83,46,40,50,61,102,117,158,170,190,155,117,68,40,56,28,66,103,122,166,176,164,160,139,75,44,22,32,42,86,140,163,222,166,183,140,98,69,75,63,81,126,139,171,170,173,144,105,75,41,68,53,61,87,155,159,180,175,138,105,73,26,12,35,64,115,138,138,182,191,155,113,98,29) > ylimmax = '' > ylimmin = '' > main = 'Robustness of Central Tendency' > #'GNU S' R Code compiled by R2WASP v. 1.0.44 () > #Author: Prof. Dr. P. Wessa > #To cite this work: AUTHOR(S), (YEAR), YOUR SOFTWARE TITLE (vNUMBER) in Free Statistics Software (v$_version), Office for Research Development and Education, URL http://www.wessa.net/rwasp_YOURPAGE.wasp/ > #Source of accompanying publication: Office for Research, Development, and Education > #Technical description: Write here your technical program description (don't use hard returns!) > 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] 106.3708 > sqrtn <- sqrt(length(x)) > (armse <- sd(x) / sqrtn) [1] 3.427863 > (armose <- arm / armse) [1] 31.03124 > (geo <- geomean(x)) [1] NaN Warning message: In log(x) : NaNs produced > (har <- harmean(x)) [1] 93.68706 > (qua <- quamean(x)) [1] 118.8405 > (win <- winmean(x)) [,1] [,2] [1,] 106.3250 3.418407 [2,] 106.3167 3.415232 [3,] 106.3542 3.388843 [4,] 106.3208 3.373491 [5,] 106.2792 3.364322 [6,] 106.2792 3.358751 [7,] 106.5708 3.326101 [8,] 106.5708 3.326101 [9,] 106.7208 3.310470 [10,] 106.6792 3.289083 [11,] 106.6333 3.284424 [12,] 106.5833 3.279395 [13,] 106.6375 3.273993 [14,] 106.6375 3.262417 [15,] 106.5750 3.256248 [16,] 106.5750 3.243201 [17,] 106.6458 3.236330 [18,] 106.5708 3.229060 [19,] 106.4917 3.221463 [20,] 106.6583 3.205497 [21,] 106.6583 3.188893 [22,] 106.5667 3.180235 [23,] 106.3750 3.162400 [24,] 106.4750 3.153002 [25,] 106.7875 3.105171 [26,] 106.7875 3.105171 [27,] 106.9000 3.095098 [28,] 107.0167 3.084757 [29,] 106.8958 3.073667 [30,] 107.0208 3.040398 [31,] 107.0208 3.040398 [32,] 106.7542 3.016408 [33,] 106.6167 3.004212 [34,] 106.7583 2.991917 [35,] 106.6125 2.953779 [36,] 106.9125 2.928272 [37,] 106.7583 2.914878 [38,] 106.6000 2.901250 [39,] 106.9250 2.874049 [40,] 107.2583 2.846699 [41,] 106.9167 2.817466 [42,] 106.9167 2.817466 [43,] 107.0958 2.802926 [44,] 106.9125 2.787422 [45,] 106.9125 2.787422 [46,] 106.9125 2.755884 [47,] 106.5208 2.723413 [48,] 106.7208 2.707351 [49,] 106.9250 2.691108 [50,] 106.7167 2.673990 [51,] 106.7167 2.673990 [52,] 106.5000 2.656339 [53,] 106.7208 2.638851 [54,] 106.2708 2.566872 [55,] 106.7292 2.531136 [56,] 106.9625 2.513196 [57,] 106.9625 2.513196 [58,] 106.9625 2.513196 [59,] 106.9625 2.513196 [60,] 106.7125 2.454379 [61,] 106.4583 2.395208 [62,] 106.7167 2.375684 [63,] 106.7167 2.375684 [64,] 106.4500 2.354968 [65,] 106.9917 2.314468 [66,] 107.2667 2.252679 [67,] 107.2667 2.252679 [68,] 106.7000 2.209086 [69,] 106.7000 2.209086 [70,] 106.9917 2.187801 [71,] 107.2875 2.166400 [72,] 107.8875 2.123579 [73,] 107.5833 2.100219 [74,] 107.8917 2.078461 [75,] 107.5792 2.054566 [76,] 107.2625 2.030514 [77,] 106.9417 1.959595 [78,] 106.9417 1.912537 [79,] 106.6125 1.888096 [80,] 106.6125 1.888096 > (tri <- trimean(x)) [,1] [,2] [1,] 106.3613 3.388817 [2,] 106.3983 3.357690 [3,] 106.4402 3.326646 [4,] 106.4698 3.303670 [5,] 106.5087 3.283768 [6,] 106.5570 3.264869 [7,] 106.6062 3.246000 [8,] 106.6116 3.231627 [9,] 106.6171 3.216396 [10,] 106.6045 3.202403 [11,] 106.5963 3.190238 [12,] 106.5926 3.177833 [13,] 106.5935 3.165175 [14,] 106.5896 3.152249 [15,] 106.5857 3.139605 [16,] 106.5865 3.126693 [17,] 106.5874 3.114075 [18,] 106.5833 3.101185 [19,] 106.5842 3.088011 [20,] 106.5900 3.074536 [21,] 106.5859 3.061363 [22,] 106.5816 3.048508 [23,] 106.5825 3.035363 [24,] 106.5938 3.022541 [25,] 106.6000 3.009433 [26,] 106.5904 2.998613 [27,] 106.5806 2.986929 [28,] 106.5652 2.974985 [29,] 106.5440 2.962760 [30,] 106.5278 2.950275 [31,] 106.5056 2.938884 [32,] 106.4830 2.926535 [33,] 106.4713 2.914657 [34,] 106.4651 2.902519 [35,] 106.4529 2.890086 [36,] 106.4464 2.878885 [37,] 106.4277 2.868180 [38,] 106.4146 2.857270 [39,] 106.4074 2.846137 [40,] 106.3875 2.835534 [41,] 106.3544 2.825462 [42,] 106.3333 2.816079 [43,] 106.3117 2.805686 [44,] 106.2829 2.795041 [45,] 106.2600 2.784190 [46,] 106.2365 2.772182 [47,] 106.2123 2.760786 [48,] 106.2014 2.750090 [49,] 106.1831 2.739126 [50,] 106.1571 2.727864 [51,] 106.1377 2.716344 [52,] 106.1176 2.703485 [53,] 106.1045 2.690258 [54,] 106.0833 2.676589 [55,] 106.0769 2.665816 [56,] 106.0547 2.655830 [57,] 106.0238 2.645570 [58,] 105.9919 2.633892 [59,] 105.9590 2.620653 [60,] 105.9250 2.605697 [61,] 105.8983 2.592778 [62,] 105.8793 2.582032 [63,] 105.8509 2.570938 [64,] 105.8214 2.558147 [65,] 105.8000 2.544936 [66,] 105.7593 2.532546 [67,] 105.7075 2.522530 [68,] 105.6538 2.510723 [69,] 105.6176 2.500206 [70,] 105.5800 2.487755 [71,] 105.5306 2.474707 [72,] 105.4688 2.460965 [73,] 105.3830 2.448000 [74,] 105.3043 2.434608 [75,] 105.2111 2.420341 [76,] 105.1250 2.405519 [77,] 105.0465 2.390068 [78,] 104.9762 2.378063 [79,] 104.9024 2.367577 [80,] 104.8375 2.356814 > (midr <- midrange(x)) [1] 107.5 > midm <- array(NA,dim=8) > for (j in 1:8) midm[j] <- midmean(x,j) > midm [1] 104.4758 105.9250 104.4758 105.9250 105.9250 104.4758 105.9250 106.8077 > postscript(file="/var/www/rcomp/tmp/1n5z31321536146.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/www/rcomp/tmp/22ei31321536146.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/www/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/www/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/www/rcomp/tmp/39fmq1321536146.tab") > > try(system("convert tmp/1n5z31321536146.ps tmp/1n5z31321536146.png",intern=TRUE)) character(0) > try(system("convert tmp/22ei31321536146.ps tmp/22ei31321536146.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 1.580 0.060 1.633