R version 2.13.0 (2011-04-13) Copyright (C) 2011 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(80,111,122,131,192,188,216,238,173,160,93,67,60,32,126,131,134,162,230,232,200,143,85,66,54,81,100,126,204,218,227,220,220,120,110,67,81,52,106,156,187,204,204,196,204,124,53,77,77,50,105,125,165,194,263,225,263,140,127,86,71,95,95,133,178,160,250,251,250,173,103,21,29,39,71,148,144,199,206,224,206,152,88,35,23,92,117,120,173,202,217,256,217,143,95,77,76,100,108,132,195,198,204,212,204,129,73,77,80,64,109,138,185,198,237,223,237,146,102,77,70,86,98,141,195,205,191,226,191,147,100,74,56,77,80,120,186,196,229,229,229,176,104,61,72,99,113,140,174,209,205,229,215,136,113,57,55,66,125,149,176,230,238,245,238,124,111,72,63,78,100,149,166,201,214,231,214,151,97,68,81,55,99,146,170,218,218,207,218,178,105,67,47,55,73,124,185,213,278,205,278,171,125,92,96,92,118,185,183,215,207,214,207,142,102,66,87,90,90,133,205,201,220,210,220,136,95,52,40,60,100,169,184,202,226,239,226,149,121,50) > 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] 145.2917 > sqrtn <- sqrt(length(x)) > (armse <- sd(x) / sqrtn) [1] 4.103515 > (armose <- arm / armse) [1] 35.40664 > (geo <- geomean(x)) [1] 129.113 > (har <- harmean(x)) [1] 110.6902 > (qua <- quamean(x)) [1] 158.5375 > (win <- winmean(x)) [,1] [,2] [1,] 145.3000 4.102467 [2,] 145.2250 4.080360 [3,] 145.2625 4.075945 [4,] 145.1958 4.056478 [5,] 145.1750 4.035487 [6,] 145.1750 4.030014 [7,] 145.3792 4.008382 [8,] 145.3125 3.980403 [9,] 145.0875 3.957443 [10,] 145.1292 3.945017 [11,] 145.1292 3.945017 [12,] 145.1792 3.940101 [13,] 145.1792 3.929503 [14,] 145.2375 3.923865 [15,] 144.9250 3.893954 [16,] 144.8583 3.887745 [17,] 144.8583 3.874381 [18,] 144.9333 3.867215 [19,] 145.0917 3.837635 [20,] 145.0917 3.837635 [21,] 145.1792 3.829552 [22,] 145.3625 3.812836 [23,] 145.2667 3.786751 [24,] 145.3667 3.759937 [25,] 145.3667 3.759937 [26,] 145.3667 3.759937 [27,] 145.3667 3.739979 [28,] 145.2500 3.729629 [29,] 145.1292 3.718998 [30,] 144.8792 3.675571 [31,] 145.1375 3.653151 [32,] 145.2708 3.641725 [33,] 145.2708 3.641725 [34,] 145.1292 3.605525 [35,] 145.1292 3.605525 [36,] 145.2792 3.592847 [37,] 145.2792 3.592847 [38,] 145.2792 3.566196 [39,] 145.6042 3.539234 [40,] 145.6042 3.511541 [41,] 145.4333 3.497267 [42,] 145.4333 3.497267 [43,] 145.2542 3.482411 [44,] 145.2542 3.482411 [45,] 145.2542 3.482411 [46,] 145.2542 3.450950 [47,] 145.4500 3.403167 [48,] 145.0500 3.370677 [49,] 144.8458 3.354277 [50,] 144.6375 3.304332 [51,] 144.6375 3.304332 [52,] 144.6375 3.304332 [53,] 145.3000 3.216650 [54,] 145.5250 3.199067 [55,] 145.2958 3.181005 [56,] 145.5292 3.162872 [57,] 145.7667 3.144550 [58,] 146.2500 3.107671 [59,] 146.0042 3.088290 [60,] 146.5042 3.050630 [61,] 146.5042 3.050630 [62,] 146.5042 3.050630 [63,] 146.7667 3.031077 [64,] 147.3000 2.991779 [65,] 146.7583 2.949074 [66,] 146.7583 2.949074 [67,] 146.4792 2.927256 [68,] 146.7625 2.906479 [69,] 146.7625 2.863112 [70,] 146.7625 2.819301 [71,] 146.7625 2.775049 [72,] 146.7625 2.775049 [73,] 146.4583 2.706376 [74,] 146.4583 2.706376 [75,] 146.1458 2.682501 [76,] 146.1458 2.682501 [77,] 145.8250 2.658113 [78,] 145.8250 2.562254 [79,] 145.4958 2.537494 [80,] 145.8292 2.513659 > (tri <- trimean(x)) [,1] [,2] [1,] 145.2563 4.066635 [2,] 145.2119 4.028942 [3,] 145.2051 4.001183 [4,] 145.1853 3.973568 [5,] 145.1826 3.949934 [6,] 145.1842 3.929798 [7,] 145.1858 3.909583 [8,] 145.1562 3.891875 [9,] 145.1351 3.877333 [10,] 145.1409 3.864997 [11,] 145.1422 3.853379 [12,] 145.1435 3.840956 [13,] 145.1402 3.828212 [14,] 145.1368 3.815672 [15,] 145.1286 3.802799 [16,] 145.1442 3.791761 [17,] 145.1650 3.780436 [18,] 145.1863 3.769380 [19,] 145.2030 3.758054 [20,] 145.2100 3.748163 [21,] 145.2172 3.737454 [22,] 145.2194 3.726472 [23,] 145.2113 3.715794 [24,] 145.2083 3.706046 [25,] 145.2000 3.697261 [26,] 145.1915 3.687654 [27,] 145.1828 3.677171 [28,] 145.1739 3.667044 [29,] 145.1703 3.656647 [30,] 145.1722 3.645962 [31,] 145.1854 3.636969 [32,] 145.1875 3.628427 [33,] 145.1839 3.619667 [34,] 145.1802 3.609978 [35,] 145.1824 3.601433 [36,] 145.1845 3.591939 [37,] 145.1807 3.582168 [38,] 145.1768 3.571347 [39,] 145.1728 3.560949 [40,] 145.1562 3.550983 [41,] 145.1392 3.541492 [42,] 145.1282 3.531715 [43,] 145.1169 3.520791 [44,] 145.1118 3.509492 [45,] 145.1067 3.496910 [46,] 145.1014 3.482942 [47,] 145.0959 3.469325 [48,] 145.0833 3.457008 [49,] 145.0845 3.445155 [50,] 145.0929 3.432822 [51,] 145.1087 3.421927 [52,] 145.1250 3.409592 [53,] 145.1418 3.395690 [54,] 145.1364 3.385375 [55,] 145.1231 3.374622 [56,] 145.1172 3.363417 [57,] 145.1032 3.351699 [58,] 145.0806 3.339411 [59,] 145.0410 3.327624 [60,] 145.0083 3.315305 [61,] 144.9576 3.303482 [62,] 144.9052 3.289777 [63,] 144.8509 3.273992 [64,] 144.7857 3.257193 [65,] 144.7000 3.240552 [66,] 144.6296 3.224538 [67,] 144.5566 3.206016 [68,] 144.4904 3.186363 [69,] 144.4118 3.165234 [70,] 144.3300 3.144245 [71,] 144.2449 3.123419 [72,] 144.1562 3.102783 [73,] 144.0638 3.078728 [74,] 143.9783 3.056678 [75,] 143.8889 3.030863 [76,] 143.8068 3.002996 [77,] 143.7209 2.970473 [78,] 143.6429 2.935071 [79,] 143.5610 2.903346 [80,] 143.4875 2.868698 > (midr <- midrange(x)) [1] 149.5 > midm <- array(NA,dim=8) > for (j in 1:8) midm[j] <- midmean(x,j) > midm [1] 144.5935 145.4959 144.5935 145.4959 145.4959 144.5935 145.4959 144.5935 > postscript(file="/var/wessaorg/rcomp/tmp/133hs1324140145.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/2lddi1324140145.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/3wyfl1324140145.tab") > > try(system("convert tmp/133hs1324140145.ps tmp/133hs1324140145.png",intern=TRUE)) character(0) > try(system("convert tmp/2lddi1324140145.ps tmp/2lddi1324140145.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 1.260 0.137 1.394