平方平均数最容易实现
a <- c(5:15)
root.mean.square <- sqrt(sum(a^2))
几何平均数实现算法,要考虑到NA或负值
geometry.mean <- exp(mean(log(x)))
geo_mean <- function(data) {
log_data <- log(data)
gm <- exp(mean(log_data[is.finite(log_data)]))
return(gm)
}
参考: http://stackoverflow.com/questions/2602583/geometric-mean-is-there-a-built-in
也可以用psych 包里的 geometric.mean
http://personality-project.org/r/html/geometric.mean.html
调和平均数 harmonic.mean, 有两个包含有这个函数 lmomco和 psych
length(a)/sum(1/a)
http://personality-project.org/r/psych/help/harmonic.mean.html
http://www.inside-r.org/packages/cran/lmomco/docs/harmonic.mean
library(ggplot2)
library(reshape2)
# Function to calculate the harmonic mean
harmonicMean <- function(array){
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 <- function(array){
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 <- function(sample){
#Passed data frame with n number of rows and 2 columns (values and obs)
arithmetic <- mean(sample$value)
harmonic <- harmonicMean(sample$value)
geometric <- geometricMean(sample$value)
results <- data.frame(arithmetic, harmonic, geometric)
return(results)
}
##### Graphs #####
# Color Scheme
ealred <- "#7D110C"
ealtan <- "#CDC4B6"
eallighttan <- "#F7F6F0"
ealdark <- "#423C30"
ealorange <- "#BB681C"
ealgreen <- "#3e4525"
ealblue <- "#25516d"
# Function that plots the three means for comparison, called below
plot.means <- function(sample) {
# First calculate the various means and then flatten to a data frame that
# can be plotted with ggplot2
results <- fetchMeans(sample)
results.melted <- melt(results, variable.name="Type", value.name="Mean")
g <- ggplot(sample, aes(x=obs, y=value)) + geom_bar(stat="identity", alpha=1, fill=ealtan) +
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 <- 25
m <- 5
sample <- data.frame("value"=rnorm(n=n, mean=m))
sample$obs <- rownames(sample)
# Next plot the three means, compared with the sample population
g <- plot.means(sample)
g <- g + ggtitle("Mean Comparison with\nNormally Distributed Sample")
g
# ggsave("test.png")
#### Comparison based on Sample with an Outlier
# Add a few outliers to distort the population
sample.outliers <- sample
sample.outliers[n-2, 1] <- m^2.5
g.outlier <- plot.means(sample.outliers)
g.outlier <- g.outlier + ggtitle("Mean Comparison using\nSample with Outliers")
g.outlier
参考:
http://economistatlarge.com/r-guide/arithmetic-harmonic-geometric-means-r
转载于:https://blog.51cto.com/matrix6ro/1790804
这篇博客介绍了如何在R语言中计算几何平均数、调和平均数和平方平均数。对于几何平均数,文章提到了处理NA和负值的算法,并推荐使用psych包中的geometric.mean函数。调和平均数可以通过lmomco和psych包中的函数实现。提供了相关的参考链接和资源。
966

被折叠的 条评论
为什么被折叠?



