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(15136 + ,16733 + ,20016 + ,17708 + ,18019 + ,19227 + ,22893 + ,23739 + ,21133 + ,22591 + ,26786 + ,29740 + ,15028 + ,17977 + ,20008 + ,21354 + ,19498 + ,22125 + ,25817 + ,28779 + ,20960 + ,22254 + ,27392 + ,29945 + ,16933 + ,17892 + ,20533 + ,23569 + ,22417 + ,22084 + ,26580 + ,27454 + ,24081 + ,23451 + ,28991 + ,31386 + ,16896 + ,20045 + ,23471 + ,21747 + ,25621 + ,23859 + ,25500 + ,30998 + ,24475 + ,23145 + ,29701 + ,34365 + ,17556 + ,22077 + ,25702 + ,22214 + ,26886 + ,23191 + ,27831 + ,35406 + ,23195 + ,25110 + ,30009 + ,36242 + ,18450 + ,21845 + ,26488 + ,22394 + ,28057 + ,25451 + ,24872 + ,33424 + ,24052 + ,28449 + ,33533 + ,37351 + ,19969 + ,21701 + ,26249 + ,24493 + ,24603 + ,26485 + ,30723 + ,34569 + ,26689 + ,26157 + ,32064 + ,38870 + ,21337 + ,19419 + ,23166 + ,28286 + ,24570 + ,24001 + ,33151 + ,24878 + ,26804 + ,28967 + ,33311 + ,40226 + ,20504 + ,23060 + ,23562 + ,27562 + ,23940 + ,24584 + ,34303 + ,25517 + ,23494 + ,29095 + ,32903 + ,34379 + ,16991 + ,21109 + ,23740 + ,25552 + ,21752 + ,20294 + ,29009 + ,25500 + ,24166 + ,26960 + ,31222 + ,38641 + ,14672 + ,17543 + ,25453 + ,32683 + ,22449 + ,22316 + ,27595 + ,25451 + ,25421 + ,25288 + ,32568 + ,35110 + ,16052 + ,22146 + ,21198 + ,19543 + ,22084 + ,23816 + ,29961 + ,26773 + ,26635 + ,26972 + ,30207 + ,38687 + ,16974 + ,21697 + ,24179 + ,23757 + ,25013 + ,24019 + ,30345 + ,24488 + ,25156 + ,25650 + ,30923 + ,37240 + ,17466 + ,19463 + ,24352 + ,26805 + ,25236 + ,24735 + ,29356 + ,31234 + ,22724 + ,28496 + ,32857 + ,37198 + ,13652 + ,22784 + ,23565 + ,26323 + ,23779 + ,27549 + ,29660 + ,23356) > 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] 25392.15 > sqrtn <- sqrt(length(x)) > (armse <- sd(x) / sqrtn) [1] 402.5796 > (armose <- arm / armse) [1] 63.07361 > (geo <- geomean(x)) [1] 24839.89 > (har <- harmean(x)) [1] 24288.35 > (qua <- quamean(x)) [1] 25944.62 > (win <- winmean(x)) [,1] [,2] [1,] 25390.24 400.1000 [2,] 25392.20 399.0921 [3,] 25393.26 398.6711 [4,] 25384.76 390.3640 [5,] 25400.95 387.2533 [6,] 25405.08 386.2982 [7,] 25368.53 379.6828 [8,] 25332.39 373.4055 [9,] 25318.12 370.9859 [10,] 25314.38 363.0295 [11,] 25307.31 360.7181 [12,] 25307.24 360.4720 [13,] 25313.89 358.4446 [14,] 25267.28 348.1241 [15,] 25265.23 345.9935 [16,] 25258.78 344.1554 [17,] 25284.95 337.1779 [18,] 25339.06 324.9207 [19,] 25354.82 322.0543 [20,] 25340.05 318.9108 [21,] 25330.50 316.6725 [22,] 25273.12 307.9928 [23,] 25240.19 291.2217 [24,] 25224.78 288.1869 [25,] 25224.22 287.8663 [26,] 25195.41 283.5174 [27,] 25222.10 278.2550 [28,] 25223.69 271.1966 [29,] 25166.19 263.6129 [30,] 25215.45 253.8264 [31,] 25206.82 247.4336 [32,] 25202.45 246.0547 [33,] 25211.64 244.5757 [34,] 25198.89 237.7184 [35,] 25194.52 236.5593 [36,] 25256.29 229.2742 [37,] 25193.22 222.2708 [38,] 25146.80 215.4097 [39,] 25128.85 213.3212 [40,] 25145.90 210.9949 [41,] 25194.35 205.6533 [42,] 25151.16 200.8409 [43,] 25082.02 193.7907 [44,] 25080.52 191.7048 [45,] 25044.21 187.0746 [46,] 25002.13 179.6270 [47,] 24952.46 172.8720 [48,] 24905.01 165.3051 [49,] 24917.53 162.5155 [50,] 24920.38 161.5930 [51,] 24902.12 158.2238 [52,] 24925.76 152.8451 [53,] 24839.33 137.9109 [54,] 24854.06 135.9778 [55,] 24864.99 130.9880 [56,] 24892.36 124.1910 [57,] 24919.56 121.8564 [58,] 24920.55 120.7573 > (tri <- trimean(x)) [,1] [,2] [1,] 25374.37 392.3491 [2,] 25358.13 384.0063 [3,] 25340.49 375.5815 [4,] 25322.06 366.6487 [5,] 25305.44 359.5527 [6,] 25284.94 352.6762 [7,] 25263.19 345.4529 [8,] 25246.63 338.9424 [9,] 25234.69 333.0128 [10,] 25224.23 326.9927 [11,] 25213.93 321.6633 [12,] 25204.10 316.2167 [13,] 25194.01 310.3443 [14,] 25183.05 304.2248 [15,] 25183.05 298.8475 [16,] 25168.51 293.2504 [17,] 25161.51 287.3661 [18,] 25152.39 281.7245 [19,] 25139.16 276.8730 [20,] 25124.47 271.8646 [21,] 25110.31 266.7131 [22,] 25096.33 261.2983 [23,] 25085.45 256.2561 [24,] 25076.20 252.3698 [25,] 25067.56 248.3902 [26,] 25058.66 244.0390 [27,] 25051.07 239.6643 [28,] 25041.78 235.3248 [29,] 25032.09 231.1902 [30,] 25032.09 227.3373 [31,] 25015.28 223.9455 [32,] 25005.57 220.7550 [33,] 24995.73 217.3236 [34,] 24985.06 213.6223 [35,] 24974.62 210.1261 [36,] 24963.99 206.3145 [37,] 24949.98 202.6847 [38,] 24938.41 199.3006 [39,] 24928.56 196.1558 [40,] 24919.15 192.8021 [41,] 24908.53 189.2119 [42,] 24895.20 185.6241 [43,] 24883.28 182.0499 [44,] 24874.03 178.7326 [45,] 24864.43 175.1404 [46,] 24856.06 171.5263 [47,] 24849.24 168.1930 [48,] 24844.41 165.0982 [49,] 24844.41 162.3522 [50,] 24837.97 159.4482 [51,] 24834.05 156.1425 [52,] 24830.79 152.6693 [53,] 24826.20 149.2369 [54,] 24825.56 147.0730 [55,] 24824.15 144.7000 [56,] 24822.11 142.4728 [57,] 24818.55 140.6395 [58,] 24813.35 138.6593 > (midr <- midrange(x)) [1] 26939 > midm <- array(NA,dim=8) > for (j in 1:8) midm[j] <- midmean(x,j) > midm [1] 24812.03 24874.03 24812.03 24874.03 24874.03 24812.03 24874.03 24852.52 > postscript(file="/var/www/html/rcomp/tmp/1ykl61271770224.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/2ykl61271770224.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/315cn1271770224.tab") > > try(system("convert tmp/1ykl61271770224.ps tmp/1ykl61271770224.png",intern=TRUE)) character(0) > try(system("convert tmp/2ykl61271770224.ps tmp/2ykl61271770224.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 0.918 0.331 1.094