日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/14 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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语言的几何平均数,调和平均数,平方平均数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。