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(464,675,703,887,1139,1077,1318,1260,1120,963,996,960,530,883,894,1045,1199,1287,1565,1577,1076,918,1008,1063,544,635,804,980,1018,1064,1404,1286,1104,999,996,1015,615,722,832,977,1270,1437,1520,1708,1151,934,1159,1209,699,830,996,1124,1458,1270,1753,2258,1208,1241,1265,1828,809,997,1164,1205,1538,1513,1378,2083,1357,1536,1526,1376,779,1005,1193,1522,1539,1546,2116,2326,1596,1356,1553,1613,814,1150,1225,1691,1759,1754,2100,2062,2012,1897,1964,2186,966,1549,1538,1612,2078,2137,2907,2249,1883,1739,1828,1868,1138,1430,1809,1763,2200,2067,2503,2141,2103,1972,2181,2344,970,1199,1718,1683,2025,2051,2439,2353,2230,1852,2147,2286,1007,1665,1642,1518,1831,2207,2822,2393,2306,1785,2047,2171,1212,1335,2011,1860,1954,2152,2835,2224,2182,1992,2389,2724,891,1247,2017,2257,2255,2255,3057,3330,1896,2096,2374,2535,1041,1728,2201,2455,2204,2660,3670,2665,2639,2226,2586,2684,1185,1749,2459,2618,2585,3310,3923) > 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] 1664.064 > sqrtn <- sqrt(length(x)) > (armse <- sd(x) / sqrtn) [1] 47.45809 > (armose <- arm / armse) [1] 35.06387 > (geo <- geomean(x)) [1] 1536.175 > (har <- harmean(x)) [1] 1406.556 > (qua <- quamean(x)) [1] 1785.506 > (win <- winmean(x)) [,1] [,2] [1,] 1663.064 47.08305 [2,] 1659.578 46.29374 [3,] 1660.396 46.08849 [4,] 1655.412 45.06242 [5,] 1652.471 44.29289 [6,] 1650.930 43.85869 [7,] 1650.594 43.77091 [8,] 1647.214 43.09331 [9,] 1648.032 42.52884 [10,] 1648.353 42.25133 [11,] 1648.353 42.18181 [12,] 1647.326 41.97506 [13,] 1646.979 41.67291 [14,] 1644.733 41.36047 [15,] 1648.743 40.92874 [16,] 1644.807 40.37758 [17,] 1642.262 40.00018 [18,] 1638.316 39.48931 [19,] 1640.348 39.19954 [20,] 1640.348 38.84092 [21,] 1638.102 37.99655 [22,] 1637.984 37.91253 [23,] 1636.508 37.68191 [24,] 1634.326 37.35160 [25,] 1634.059 37.13828 [26,] 1631.973 36.84326 [27,] 1631.396 36.33439 [28,] 1628.401 36.03802 [29,] 1624.059 35.61724 [30,] 1624.059 35.58669 [31,] 1624.059 35.52362 [32,] 1625.086 35.42674 [33,] 1624.380 35.29256 [34,] 1621.107 34.94928 [35,] 1621.668 34.75596 [36,] 1621.861 34.66584 [37,] 1623.048 33.93016 [38,] 1623.251 33.79893 [39,] 1626.380 33.39820 [40,] 1626.380 33.35903 [41,] 1625.941 32.83873 [42,] 1625.267 32.73624 [43,] 1631.246 32.16199 [44,] 1632.658 31.61615 [45,] 1629.048 31.11633 [46,] 1631.262 30.70625 [47,] 1630.005 30.54864 [48,] 1631.802 30.21328 [49,] 1626.561 29.69963 [50,] 1625.225 29.20894 [51,] 1625.770 29.02018 [52,] 1630.497 28.42652 [53,] 1629.080 27.91250 [54,] 1629.369 27.64110 [55,] 1626.134 27.35979 [56,] 1626.433 27.07961 [57,] 1623.995 26.71403 [58,] 1623.064 26.58156 [59,] 1617.070 25.91075 [60,] 1618.674 25.34420 [61,] 1622.262 24.77242 [62,] 1623.920 24.58006 > (tri <- trimean(x)) [,1] [,2] [1,] 1664.064 45.92356 [2,] 1658.341 44.67208 [3,] 1650.381 43.77164 [4,] 1650.381 42.88324 [5,] 1644.644 42.23931 [6,] 1642.971 41.73805 [7,] 1642.971 41.29145 [8,] 1641.538 40.82854 [9,] 1639.142 40.44384 [10,] 1638.036 40.11434 [11,] 1636.867 39.79746 [12,] 1635.669 39.46465 [13,] 1634.540 39.13081 [14,] 1634.540 38.80507 [15,] 1632.452 38.48751 [16,] 1632.452 38.18814 [17,] 1630.098 37.91951 [18,] 1629.212 37.66426 [19,] 1628.577 37.43430 [20,] 1627.789 37.20883 [21,] 1626.979 36.99344 [22,] 1626.287 36.82920 [23,] 1625.582 36.65408 [24,] 1624.942 36.47904 [25,] 1624.409 36.31154 [26,] 1623.874 36.14190 [27,] 1623.436 35.97600 [28,] 1623.436 35.82978 [29,] 1622.736 35.68776 [30,] 1622.669 35.55899 [31,] 1622.600 35.41437 [32,] 1622.600 35.25478 [33,] 1622.405 35.08116 [34,] 1622.311 34.89484 [35,] 1622.368 34.71026 [36,] 1622.400 34.51561 [37,] 1622.425 34.30164 [38,] 1622.396 34.11608 [39,] 1622.358 33.91397 [40,] 1622.178 33.71431 [41,] 1621.990 33.48841 [42,] 1621.816 33.27181 [43,] 1621.663 33.03111 [44,] 1621.242 32.80157 [45,] 1620.742 32.58167 [46,] 1620.379 32.36877 [47,] 1619.903 32.15469 [48,] 1619.462 31.91760 [49,] 1618.921 31.66950 [50,] 1618.586 31.42587 [51,] 1618.586 31.18399 [52,] 1617.964 30.91512 [53,] 1617.407 30.65337 [54,] 1616.886 30.39253 [55,] 1616.325 30.10745 [56,] 1616.325 29.79605 [57,] 1615.397 29.45294 [58,] 1615.000 29.08296 [59,] 1614.623 28.65353 [60,] 1614.507 28.22077 [61,] 1614.308 27.76972 [62,] 1613.921 27.29711 > (midr <- midrange(x)) [1] 2193.5 > midm <- array(NA,dim=8) > for (j in 1:8) midm[j] <- midmean(x,j) > midm [1] 1614.777 1620.379 1620.379 1620.379 1619.903 1614.777 1620.379 1620.379 > postscript(file="/var/www/html/rcomp/tmp/1kfye1268604764.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/21xhg1268604764.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/3882e1268604764.tab") > > try(system("convert tmp/1kfye1268604764.ps tmp/1kfye1268604764.png",intern=TRUE)) character(0) > try(system("convert tmp/21xhg1268604764.ps tmp/21xhg1268604764.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 0.965 0.350 1.112