R version 2.12.2 (2011-02-25) Copyright (C) 2011 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: x86_64-redhat-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(1686,1591,2304,1712,1471,1377,1966,2453,1984,2596,4087,5179,1530,1523,1633,1976,1170,1480,1781,2472,1981,2273,3857,4551,1510,1329,1518,1790,1537,1449,1954,1897,1706,2514,3593,4524,1609,1638,2030,1375,1320,1245,1600,2298,2191,2511,3440,4923,1609,1435,2061,1789,1567,1404,1597,3159,1759,2504,4273,5274,1771,1682,1846,1589,1896,1379,1645,2512,1771,3727,4388,5434,1606,1523,1577,1605,1765,1403,2584,3318,1562,2349,3987,5891,1389,1442,1548,1935,1518,1250,1847,1930,2638,3114,4405,7242,1853,1779,2108,2336,1728,1661,2230,1645,2421,3740,4988,6757,1757,1394,1982,1650,1654,1406,1971,1968,2608,3845,4514,6694,1720,1321,1859,1628,1615,1457,1899,1605,2424,3116,4286,6047,1902,2049,1874,1279,1432,1540,2214,1857,2408,3252,3627,6153,1577,1667,1993,1997,1783,1625,2076,1773,2377,3088,4096,6119,1494,1564,1898,2121,1831,1515,2048,2795,1749,3339,4227,6410,1197,1968,1720,1725,1674,1693,2031,1495,2968,3385,3729,5999,1070,1402,1897,1862,1670,1688,2031) > 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] 2402.417 > sqrtn <- sqrt(length(x)) > (armse <- sd(x) / sqrtn) [1] 94.70794 > (armose <- arm / armse) [1] 25.36659 > (geo <- geomean(x)) [1] 2160.408 > (har <- harmean(x)) [1] 1996.682 > (qua <- quamean(x)) [1] 2727.628 > (win <- winmean(x)) [,1] [,2] [1,] 2400.358 93.98932 [2,] 2399.973 93.80233 [3,] 2396.187 92.65866 [4,] 2390.797 91.40287 [5,] 2390.663 91.15089 [6,] 2389.668 90.56117 [7,] 2387.909 90.17029 [8,] 2383.631 89.16250 [9,] 2363.850 84.54485 [10,] 2355.401 82.89278 [11,] 2349.930 81.83700 [12,] 2338.316 79.55904 [13,] 2334.144 78.73274 [14,] 2306.893 73.95439 [15,] 2304.807 73.59689 [16,] 2304.037 73.45269 [17,] 2294.310 71.85659 [18,] 2295.176 71.43403 [19,] 2285.118 69.79913 [20,] 2284.476 69.53621 [21,] 2280.096 68.69434 [22,] 2265.626 66.31466 [23,] 2266.241 66.03830 [24,] 2254.561 64.07912 [25,] 2239.053 61.46369 [26,] 2237.524 61.21879 [27,] 2224.529 58.95957 [28,] 2223.631 58.68349 [29,] 2223.786 58.61056 [30,] 2207.743 56.41923 [31,] 2202.936 55.60481 [32,] 2176.754 52.13836 [33,] 2168.283 50.79781 [34,] 2161.193 49.63978 [35,] 2157.824 49.10145 [36,] 2146.658 47.39325 [37,] 2131.027 44.91859 [38,] 2122.695 43.82077 [39,] 2122.904 43.72711 [40,] 2119.481 42.90495 [41,] 2093.171 39.75033 [42,] 2057.011 35.05579 [43,] 2021.369 31.04452 [44,] 2015.722 30.19004 [45,] 2013.556 29.83237 [46,] 2011.834 29.43164 [47,] 1994.241 27.62132 [48,] 1993.984 27.54996 [49,] 1994.508 27.46405 [50,] 1992.636 27.27510 [51,] 1985.545 26.27694 [52,] 1983.043 25.54247 [53,] 1975.674 24.67009 [54,] 1976.251 24.47632 [55,] 1973.898 23.99280 [56,] 1966.711 22.93684 [57,] 1958.176 22.12101 [58,] 1955.695 21.62088 [59,] 1946.861 20.57601 [60,] 1947.182 20.22505 [61,] 1940.984 19.31889 [62,] 1927.722 17.93763 > (tri <- trimean(x)) [,1] [,2] [1,] 2402.417 91.77660 [2,] 2383.459 89.37792 [3,] 2348.740 86.87855 [4,] 2348.740 84.61997 [5,] 2316.746 82.56007 [6,] 2300.949 80.38484 [7,] 2300.949 78.14734 [8,] 2284.965 75.78339 [9,] 2253.012 73.38639 [10,] 2239.222 71.56650 [11,] 2226.055 69.84399 [12,] 2213.135 68.12177 [13,] 2201.019 66.56393 [14,] 2201.019 64.97315 [15,] 2178.943 63.84647 [16,] 2178.943 62.66034 [17,] 2158.490 61.38105 [18,] 2148.596 60.16616 [19,] 2138.376 58.87891 [20,] 2128.551 57.64903 [21,] 2118.497 56.32519 [22,] 2108.434 54.95889 [23,] 2098.957 53.71055 [24,] 2089.173 52.35884 [25,] 2079.766 51.07344 [26,] 2070.941 49.92494 [27,] 2061.932 48.67785 [28,] 2061.932 47.52803 [29,] 2044.519 46.27711 [30,] 2035.417 44.88813 [31,] 2026.824 43.57644 [32,] 2026.824 42.19514 [33,] 2010.529 41.02951 [34,] 2003.017 39.86560 [35,] 1995.581 38.68222 [36,] 1988.043 37.40750 [37,] 1980.752 36.15953 [38,] 1973.910 35.02550 [39,] 1967.193 33.86645 [40,] 1960.215 32.55586 [41,] 1953.124 31.15184 [42,] 1946.922 29.94660 [43,] 1942.069 29.13276 [44,] 1938.586 28.64938 [45,] 1935.206 28.18495 [46,] 1931.779 27.69059 [47,] 1928.280 27.16530 [48,] 1925.396 26.76172 [49,] 1922.393 26.30329 [50,] 1919.230 25.78137 [51,] 1919.230 25.19961 [52,] 1912.928 24.64417 [53,] 1909.815 24.08241 [54,] 1906.873 23.53693 [55,] 1903.753 22.91767 [56,] 1903.753 22.25138 [57,] 1897.548 21.61202 [58,] 1894.746 20.97629 [59,] 1891.899 20.29019 [60,] 1889.299 19.63474 [61,] 1886.523 18.89447 [62,] 1883.873 18.15284 > (midr <- midrange(x)) [1] 4156 > midm <- array(NA,dim=8) > for (j in 1:8) midm[j] <- midmean(x,j) > midm [1] 1924.840 1931.779 1931.779 1931.779 1924.840 1924.840 1931.779 1931.779 > postscript(file="/var/www/wessaorg/rcomp/tmp/1sx5b1301923990.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/wessaorg/rcomp/tmp/2niny1301923990.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/wessaorg/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/www/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/www/wessaorg/rcomp/tmp/3loqt1301923991.tab") > > try(system("convert tmp/1sx5b1301923990.ps tmp/1sx5b1301923990.png",intern=TRUE)) character(0) > try(system("convert tmp/2niny1301923990.ps tmp/2niny1301923990.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 1.350 0.120 1.496