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?
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
總結
以上是生活随笔為你收集整理的python求几何平均_R语言的几何平均数,调和平均数,平方平均数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英文常见姓氏列表
- 下一篇: smarty模板参考手册