R version 2.11.1 (2010-05-31) Copyright (C) 2010 The R Foundation for Statistical Computing ISBN 3-900051-07-0 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(2.4,2.4,2.5,2.6,2.4,2.6,2.4,2.3,2.4,2.4,2.4,2.4,2.4,2.4,2.4,2.4,2.5,2.1,2.1,2,2,2,1.9,1.9,2,1.8,1.6,1.3,1.4,1.4,1.5,1.7,1.6,1.5,1.6,1.5,1.1,1.1,1.1,1.4,1.3,1.4,1.3,1.1,1,0.9,0.8,0.8,0.8,0.8,1,1.1,1,0.9,1.1,1.2,1.2,1.4,1.5,1.7,1.9,1.9,1.9,1.7,1.7,2.1,2,2,2.5,2.4,2.5,2.5,2,1.9,2.2,2.7,3.1,2.8,2.6,2.3,2.2,2.2,2,2,2.6,2.5,2.5,2.3,2,1.9,2,2.1,2.1,2.3,2.3,2.3,2.1,2.4,2.5,2.1,1.8,1.9,1.9,2.1,2.2,2,2.2,2,1.9,1.6,1.7,2,2.5,2.4,2.3,2.3,2.1,2.4,2.2,2.4,1.9,2.1,2.1,2.1,2,2.1,2.2,2.2,2.6,2.5,2.3,2.2,2.4,2.3,2.2,2.5,2.5,2.5,2.4,2.3,1.7,1.6,1.9,1.9,1.8,1.8,1.9,1.9,1.9,1.9,1.8,1.7,2.1,2.6,3.1,3.1,3.2,3.3,3.6,3.3,3.7,4,4,3.8,3.6,3.2,2.1,1.6,1.1,1.2,0.6,0.6,0,-0.1,-0.6,-0.2,-0.3,-0.1,0.5,0.9) > ylimmax = '' > ylimmin = '' > main = 'Robustness of Central Tendency' > 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] 1.962778 > sqrtn <- sqrt(length(x)) > (armse <- sd(x) / sqrtn) [1] 0.05629261 > (armose <- arm / armse) [1] 34.86741 > (geo <- geomean(x)) [1] NaN Warning message: In log(x) : NaNs produced > (har <- harmean(x)) [1] 0 > (qua <- quamean(x)) [1] 2.102314 > (win <- winmean(x)) [,1] [,2] [1,] 1.964444 0.05589197 [2,] 1.963333 0.05521191 [3,] 1.963333 0.05455025 [4,] 1.961111 0.05416478 [5,] 1.963889 0.05358509 [6,] 1.970556 0.04884575 [7,] 1.974444 0.04820890 [8,] 1.970000 0.04754625 [9,] 1.980000 0.04602377 [10,] 1.974444 0.04522598 [11,] 1.974444 0.04522598 [12,] 1.974444 0.04522598 [13,] 1.960000 0.04144054 [14,] 1.952222 0.04059960 [15,] 1.943889 0.03978655 [16,] 1.952778 0.03852036 [17,] 1.952778 0.03852036 [18,] 1.952778 0.03852036 [19,] 1.963333 0.03710421 [20,] 1.963333 0.03710421 [21,] 1.951667 0.03604841 [22,] 1.951667 0.03604841 [23,] 1.951667 0.03604841 [24,] 1.951667 0.03604841 [25,] 1.951667 0.03604841 [26,] 1.966111 0.03418978 [27,] 1.966111 0.03418978 [28,] 1.966111 0.03418978 [29,] 1.982222 0.03222704 [30,] 1.982222 0.03222704 [31,] 1.982222 0.03222704 [32,] 2.000000 0.03018667 [33,] 2.000000 0.03018667 [34,] 1.981111 0.02853542 [35,] 1.981111 0.02853542 [36,] 1.981111 0.02853542 [37,] 2.001667 0.02626649 [38,] 2.001667 0.02626649 [39,] 2.001667 0.02626649 [40,] 2.001667 0.02626649 [41,] 2.024444 0.02391821 [42,] 2.024444 0.02391821 [43,] 2.024444 0.02391821 [44,] 2.024444 0.02391821 [45,] 2.024444 0.02391821 [46,] 2.024444 0.02391821 [47,] 2.050556 0.02142498 [48,] 2.050556 0.02142498 [49,] 2.050556 0.02142498 [50,] 2.050556 0.02142498 [51,] 2.050556 0.02142498 [52,] 2.050556 0.02142498 [53,] 2.021111 0.01885929 [54,] 2.051111 0.01611737 [55,] 2.051111 0.01611737 [56,] 2.051111 0.01611737 [57,] 2.051111 0.01611737 [58,] 2.051111 0.01611737 [59,] 2.083889 0.01342066 [60,] 2.083889 0.01342066 > (tri <- trimean(x)) [,1] [,2] [1,] 1.965730 0.05385564 [2,] 1.967045 0.05163208 [3,] 1.968966 0.04959889 [4,] 1.970930 0.04764787 [5,] 1.973529 0.04563467 [6,] 1.975595 0.04358226 [7,] 1.976506 0.04245066 [8,] 1.976829 0.04135225 [9,] 1.977778 0.04028120 [10,] 1.977500 0.03937353 [11,] 1.977848 0.03851245 [12,] 1.978205 0.03757865 [13,] 1.978205 0.03656321 [14,] 1.980263 0.03594853 [15,] 1.982667 0.03537902 [16,] 1.985811 0.03484806 [17,] 1.988356 0.03441268 [18,] 1.990972 0.03393846 [19,] 1.993662 0.03342134 [20,] 1.995714 0.03300427 [21,] 1.997826 0.03254869 [22,] 2.000735 0.03214375 [23,] 2.003731 0.03169910 [24,] 2.006818 0.03121032 [25,] 2.010000 0.03067225 [26,] 2.010000 0.03007886 [27,] 2.015873 0.02960937 [28,] 2.018548 0.02909075 [29,] 2.021311 0.02851682 [30,] 2.023333 0.02807387 [31,] 2.025424 0.02758277 [32,] 2.027586 0.02703727 [33,] 2.028947 0.02662871 [34,] 2.030357 0.02617383 [35,] 2.032727 0.02580187 [36,] 2.035185 0.02538437 [37,] 2.037736 0.02491505 [38,] 2.039423 0.02461118 [39,] 2.041176 0.02426768 [40,] 2.043000 0.02387911 [41,] 2.044898 0.02343895 [42,] 2.045833 0.02317537 [43,] 2.046809 0.02287414 [44,] 2.047826 0.02252982 [45,] 2.048889 0.02213591 [46,] 2.050000 0.02168454 [47,] 2.051163 0.02116602 [48,] 2.051190 0.02084655 [49,] 2.051190 0.02047640 [50,] 2.051250 0.02004692 [51,] 2.051282 0.01954729 [52,] 2.051282 0.01896381 [53,] 2.051351 0.01827869 [54,] 2.052778 0.01779867 [55,] 2.052857 0.01758993 [56,] 2.052941 0.01733705 [57,] 2.053030 0.01703140 [58,] 2.053125 0.01666202 [59,] 2.053226 0.01621474 [60,] 2.051667 0.01603169 > (midr <- midrange(x)) [1] 1.7 > midm <- array(NA,dim=8) > for (j in 1:8) midm[j] <- midmean(x,j) > midm [1] 2.068571 2.068571 2.068571 2.068571 2.068571 2.068571 2.068571 2.068571 > postscript(file="/var/www/rcomp/tmp/1wn9h1289489555.ps",horizontal=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/2wn9h1289489555.ps",horizontal=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/3rz011289489555.tab") > > try(system("convert tmp/1wn9h1289489555.ps tmp/1wn9h1289489555.png",intern=TRUE)) character(0) > try(system("convert tmp/2wn9h1289489555.ps tmp/2wn9h1289489555.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 1.340 0.260 1.565