python求几何平均_R语言的几何平均数,调和平均数,平方平均数

平方平均数最容易实现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 

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.

sample 

sample$obs 

# Next plot the three means, compared with the sample population

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值