平方平均数最容易实现a
root.mean.square
几何平均数实现算法,要考虑到NA或负值geometry.mean
geo_mean
log_data
gm
return(gm)
}
也可以用psych 包里的 geometric.mean
调和平均数harmonic.mean, 有两个包含有这个函数 lmomco和 psychlength(a)/sum(1/a)library(ggplot2)
library(reshape2)
# Function to calculate the harmonic mean
harmonicMean
if(!is.numeric(array)){
stop("Passed argument must be an array. Consider using sapply for data frames.")
}
if(any(array<0)){
stop("All values must be greater than zero.")
}
length (array) / sum(1 / array)
}
# Function to calculate the geometric mean
geometricMean
if(!is.numeric(array)){
stop("Passed argument must be an array. Consider using sapply for data frames.")
}
if(any(array<0)){
stop("All values must be greater than zero. If you are attempting to
apply this function to rates, convert to +1 format. For example,
5% becomes 1.05 and -20% becomes .8.")
}
prod(array)^(1/length(array))
}
# Function to capture the three means based on the sample
fetchMeans
#Passed data frame with n number of rows and 2 columns (values and obs)
arithmetic
harmonic
geometric
results
return(results)
}
##### Graphs #####
# Color Scheme
ealred
ealtan
eallighttan
ealdark
ealorange
ealgreen
ealblue
# Function that plots the three means for comparison, called below
plot.means
# First calculate the various means and then flatten to a data frame that
# can be plotted with ggplot2
results
results.melted
g
geom_hline(data=results.melted, aes(yintercept=Mean, color=Type), show_guide=TRUE, size=1) +
scale_color_manual(name="Type of Mean",
values=c(ealred, ealorange, ealblue),
breaks=c("arithmetic", "harmonic", "geometric"),
labels=c(paste("Arithmetic: ", round(results$arithmetic, digits=2)),
paste("Harmonic: ", round(results$harmonic, digits=2)),
paste("Geometric: ", round(results$geometric, digits=2)))) +
scale_x_discrete(breaks=NULL) +
labs(x="Observations", y=NULL) +
theme(panel.background=element_rect(fill=eallighttan))
return(g)
}
#### Comparison with Normally Distributed Sample ####
# First generate 'random' set of n numbers with mean of m. These will be normally
# distributed so you expect arithmetic mean, harmonic mean, and geometric
# mean to be fairly consistent.
n
m
sample
sample$obs
# Next plot the three means, compared with the sample population
g
g
g
# ggsave("test.png")
#### Comparison based on Sample with an Outlier
# Add a few outliers to distort the population
sample.outliers
sample.outliers[n-2, 1]
g.outlier
g.outlier
g.outlier
参考:
http://economistatlarge.com/r-guide/arithmetic-harmonic-geometric-means-r