R version 2.9.0 (2009-04-17) Copyright (C) 2009 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(2430.47 + ,2516.3 + ,2633.63 + ,2799.84 + ,3001.93 + ,3229.29 + ,3173.02 + ,3322.08 + ,3417.88 + ,3486.95 + ,3016.22 + ,2709.61 + ,2914.87 + ,3203.08 + ,3320.25 + ,3446.25 + ,3456.85 + ,3566.53 + ,3763.67 + ,3607.75 + ,3747.38 + ,3623.91 + ,3699.76 + ,3629.61 + ,3911.52 + ,4281.47 + ,4742.42 + ,4522.42 + ,4879.79 + ,5059.11 + ,5093.19 + ,4941.81 + ,4832.67 + ,4876.18 + ,5018.07 + ,4780.34 + ,4953.59 + ,4622.32 + ,4557.13 + ,4560.03 + ,4105.66 + ,4004.89 + ,4277.26 + ,4245.98 + ,4057.64 + ,3931.42 + ,3637.15 + ,3339.91 + ,3465.74 + ,3571.25 + ,3706.93 + ,3584.17 + ,3552.11 + ,3695.24 + ,3510 + ,3357.7 + ,3060.91 + ,2736.98 + ,2709.45 + ,2314.96 + ,2561.29 + ,2663.49 + ,2407.87 + ,2237.74 + ,2165.44 + ,2098.89 + ,2318.54 + ,2315.49 + ,2395.47 + ,2474.07 + ,2479.57 + ,2386.92 + ,2537.84 + ,2567.13 + ,2660.37 + ,2696.28 + ,2748.5 + ,2663.32 + ,2707.69 + ,2669.36 + ,2687.68 + ,2650.24 + ,2620.03 + ,2668.47 + ,2692.06 + ,2737.67 + ,2774.77 + ,2819.19 + ,2892.56 + ,2866.08 + ,2817.41 + ,2934.75 + ,3036.54 + ,3139.5 + ,3114.31 + ,3261.3 + ,3201.79 + ,3264.53 + ,3349.1 + ,3446.17 + ,3469.48 + ,3507.13 + ,3536.2 + ,3359.05 + ,3378.85 + ,3449.15 + ,3522.89 + ,3551.04 + ,3669.15 + ,3602 + ,3697.22 + ,3760.9 + ,3665.08 + ,3708.8 + ,3858.21 + ,3933.16 + ,3946.98 + ,3794.29 + ,3765.56 + ,3820.33 + ,3885.12 + ,3752.67 + ,3683.79 + ,3240.75 + ,3188.82 + ,3017.98 + ,3237.2 + ,3182.53 + ,2906.42 + ,2881.35 + ,2915.64 + ,2635.13 + ,2331.43 + ,2159.04 + ,2065.46 + ,1983.48 + ,1770.41 + ,1815.99 + ,2026.97 + ,2124.81 + ,2098.28 + ,2291.39 + ,2401.57 + ,2453.89 + ,2409.53) > 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] 3257.925 > sqrtn <- sqrt(length(x)) > (armse <- sd(x) / sqrtn) [1] 63.50177 > (armose <- arm / armse) [1] 51.30447 > (geo <- geomean(x)) [1] 3170.117 > (har <- harmean(x)) [1] 3083.630 > (qua <- quamean(x)) [1] 3345.856 > (win <- winmean(x)) [,1] [,2] [1,] 3258.004 63.40461 [2,] 3259.748 62.94873 [3,] 3259.314 62.56901 [4,] 3260.051 62.36479 [5,] 3259.044 61.82078 [6,] 3258.920 61.79031 [7,] 3258.070 61.24995 [8,] 3257.072 60.49943 [9,] 3255.115 60.04114 [10,] 3251.819 58.03201 [11,] 3251.163 56.78029 [12,] 3252.874 56.51495 [13,] 3249.810 56.01459 [14,] 3226.840 52.51557 [15,] 3227.738 52.29542 [16,] 3230.409 51.10121 [17,] 3214.960 48.80548 [18,] 3209.757 47.97337 [19,] 3203.670 47.04350 [20,] 3195.911 46.09199 [21,] 3196.943 45.50929 [22,] 3200.232 45.06744 [23,] 3200.276 44.34774 [24,] 3196.817 43.76217 [25,] 3198.510 42.54587 [26,] 3195.580 41.39596 [27,] 3195.098 40.41347 [28,] 3190.678 39.72616 [29,] 3200.880 38.55764 [30,] 3203.121 38.20673 [31,] 3201.682 37.99574 [32,] 3203.849 37.53483 [33,] 3197.374 36.42907 [34,] 3197.627 36.31560 [35,] 3195.938 36.14336 [36,] 3196.543 35.95605 [37,] 3196.265 35.88407 [38,] 3198.066 35.10787 [39,] 3195.306 34.61259 [40,] 3195.348 34.38853 [41,] 3190.676 33.32147 [42,] 3189.002 33.06749 [43,] 3187.359 32.90664 [44,] 3190.761 31.62362 [45,] 3189.191 31.43922 [46,] 3186.970 30.58385 [47,] 3191.297 29.36345 [48,] 3198.034 28.41016 > (tri <- trimean(x)) [,1] [,2] [1,] 3255.493 62.22203 [2,] 3252.910 60.93254 [3,] 3249.344 59.78499 [4,] 3245.827 58.68204 [5,] 3242.007 57.53804 [6,] 3238.293 56.42626 [7,] 3234.487 55.21225 [8,] 3230.701 53.98751 [9,] 3226.937 52.77873 [10,] 3223.305 51.52510 [11,] 3219.944 50.46762 [12,] 3216.543 49.48811 [13,] 3212.854 48.44021 [14,] 3209.330 47.34883 [15,] 3207.753 46.61211 [16,] 3206.044 45.82078 [17,] 3204.055 45.08520 [18,] 3203.201 44.53881 [19,] 3202.708 44.02140 [20,] 3202.638 43.54297 [21,] 3203.111 43.10560 [22,] 3203.533 42.67208 [23,] 3203.753 42.22644 [24,] 3203.979 41.79447 [25,] 3204.434 41.36156 [26,] 3204.803 40.99095 [27,] 3205.369 40.67883 [28,] 3205.988 40.41228 [29,] 3206.900 40.16391 [30,] 3207.254 39.98440 [31,] 3207.495 39.79731 [32,] 3207.830 39.58639 [33,] 3208.059 39.37327 [34,] 3208.668 39.22074 [35,] 3209.296 39.03391 [36,] 3210.054 38.81261 [37,] 3210.821 38.55292 [38,] 3211.647 38.23460 [39,] 3212.421 37.92939 [40,] 3213.400 37.60313 [41,] 3214.439 37.21874 [42,] 3215.816 36.86903 [43,] 3217.385 36.45351 [44,] 3219.162 35.94445 [45,] 3220.863 35.48802 [46,] 3222.789 34.92413 [47,] 3225.003 34.33219 [48,] 3227.125 33.77386 > (midr <- midrange(x)) [1] 3431.8 > midm <- array(NA,dim=8) > for (j in 1:8) midm[j] <- midmean(x,j) > midm [1] 3203.288 3210.054 3210.054 3210.054 3210.054 3202.668 3210.054 3210.054 > postscript(file="/var/www/html/rcomp/tmp/1udx11260381224.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/html/rcomp/tmp/2i3371260381224.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/html/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/www/html/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/html/rcomp/tmp/3gcj11260381224.tab") > > system("convert tmp/1udx11260381224.ps tmp/1udx11260381224.png") > system("convert tmp/2i3371260381224.ps tmp/2i3371260381224.png") > > > proc.time() user system elapsed 0.844 0.358 1.124