1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > R语言的几何平均数 调和平均数 平方平均数

R语言的几何平均数 调和平均数 平方平均数

时间:2019-07-21 17:14:56

相关推荐

R语言的几何平均数 调和平均数 平方平均数

平方平均数最容易实现

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)}

参考: /questions/2602583/geometric-mean-is-there-a-built-in

也可以用psych 包里的 geometric.mean

http://personality-/r/html/geometric.mean.html

调和平均数harmonic.mean, 有两个包含有这个函数 lmomco和 psych

length(a)/sum(1/a)

http://personality-/r/psych/help/harmonic.mean.html

http://www.inside-/packages/cran/lmomco/docs/harmonic.mean

library(ggplot2)library(reshape2)#FunctiontocalculatetheharmonicmeanharmonicMean<-function(array){if(!is.numeric(array)){stop("Passedargumentmustbeanarray.Considerusingsapplyfordataframes.")}if(any(array<0)){stop("Allvaluesmustbegreaterthanzero.")}length(array)/sum(1/array)}#FunctiontocalculatethegeometricmeangeometricMean<-function(array){if(!is.numeric(array)){stop("Passedargumentmustbeanarray.Considerusingsapplyfordataframes.")}if(any(array<0)){stop("Allvaluesmustbegreaterthanzero.Ifyouareattemptingtoapplythisfunctiontorates,convertto+1format.Forexample,5%becomes1.05and-20%becomes.8.")}prod(array)^(1/length(array))}#FunctiontocapturethethreemeansbasedonthesamplefetchMeans<-function(sample){#Passeddataframewithnnumberofrowsand2columns(valuesandobs)arithmetic<-mean(sample$value)harmonic<-harmonicMean(sample$value)geometric<-geometricMean(sample$value)results<-data.frame(arithmetic,harmonic,geometric)return(results)}#####Graphs######ColorSchemeealred<-"#7D110C"ealtan<-"#CDC4B6"eallighttan<-"#F7F6F0"ealdark<-"#423C30"ealorange<-"#BB681C"ealgreen<-"#3e4525"ealblue<-"#25516d"#Functionthatplotsthethreemeansforcomparison,calledbelowplot.means<-function(sample){#Firstcalculatethevariousmeansandthenflattentoadataframethat#canbeplottedwithggplot2results<-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="TypeofMean",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)}####ComparisonwithNormallyDistributedSample#####Firstgenerate'random'setofnnumberswithmeanofm.Thesewillbenormally#distributedsoyouexpectarithmeticmean,harmonicmean,andgeometric#meantobefairlyconsistent.n<-25m<-5sample<-data.frame("value"=rnorm(n=n,mean=m))sample$obs<-rownames(sample)#Nextplotthethreemeans,comparedwiththesamplepopulationg<-plot.means(sample)g<-g+ggtitle("MeanComparisonwith\nNormallyDistributedSample")g#ggsave("test.png")####ComparisonbasedonSamplewithanOutlier#Addafewoutlierstodistortthepopulationsample.outliers<-samplesample.outliers[n-2,1]<-m^2.5g.outlier<-plot.means(sample.outliers)g.outlier<-g.outlier+ggtitle("MeanComparisonusing\nSamplewithOutliers")g.outlier

参考:

/r-guide/arithmetic-harmonic-geometric-means-r

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。