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语言的几何平均数,调和平均数,平方平均数相关推荐
- python r语言培训_R语言学习第一课
最早2010年6月17日建立的这个博客, 开始学习R语言也是在4年前,读过Robert I. Kabacoff著的<R语言实战>,从中学到了不少R语言的实践技能. 最近开始关注统计学,运筹 ...
- python基因差异分析_R语言之生信②差异基因分析2
目录 R语言之生信②差异基因分析2 样品的无监督聚类 检查基因表达分析最重要的探索性策略之一是多维定标(MDS)图或类似的图.该图以无监督的方式显示了样本之间的相似性和不相似性,以便人们可以了解在进行 ...
- python 求离差_R中Chisquared离差检验的误差
我对R非常陌生,正在尝试确定包含~500k点的形状文件是否是随机分布的.不管我输入什么参数,我都会得到相同的错误信息.在 以下是我从一开始就有的:> library(spatstat) spat ...
- python依照概率抽样_R语言之随机数与抽样模拟篇
R语言生成均匀分布随机数的函数是runif() 句法是:runif(n,min=0,max=1) n表示生成的随机数数量,min表示均匀分布的下限,max表示均匀分布的上限:若省略参数min.max ...
- 利用python构建马科维茨_R语言动量和马科维茨Markowitz投资组合(Portfolio)模型实现...
动量和马科维茨投资组合模型使 均值方差优化 组合成为可行的解决方案.通过建议并测试: 增加最大权重限制 增加目标波动率约束 下面,我将查看8个资产的结果: 首先,让我们加载所有历史数据 #****** ...
- r语言 python 金融 论文_R语言实现金融数据的时间序列分析及建模
一 移动平均 移动平均能消除数据中的季节变动和不规则变动.若序列中存在周期变动,则通常以周期为移动平均项数.移动平均法可以通过数据显示出数据长期趋势的变动规律. R可用filter()函数做移动平均. ...
- python求几何平均_创建复杂条件列(几何平均值)Python
这将计算每个站点的几何平均值并检查其是否大于30:>>> df['geo_mean_acceptable'] = ( df.groupby('Site') .transform(la ...
- C#计算数组的算术平均数、几何平均数、调和平均数、平方平均数和中位数
2019独角兽企业重金招聘Python工程师标准>>> 1.函数实现 0)打印数组 /// <summary> /// 打印数组 /// </summary> ...
- python求同构数_用c语言求1到1000的同构数_后端开发
python与平台有关吗_后端开发 Python与平台无关,因为Python是跨平台的语言,Python作为脚本语言,是解释执行的,所以能跨平台,前提是必须要有一个对应的解释器. 具体请看实例: py ...
最新文章
- 禁止 Python 子类覆盖父类方法
- 【python】【h5py】TypeError: Can‘t convert element to hsize_t
- 2函数 matlab_(2)Matlab函数“fmincon”非线性优化问题
- OpenCV学习笔记资料大集锦
- 新建gradle项目时总在下载gradle.zip
- SAP HR系统如何处理员工月中调动问题
- wicket常用控件使用方法 .
- ZooKeeper动态重新配置
- jq使用教程01_最贴心教程,安装JQData全靠这篇指南
- 软件外包相关职业规划
- Linux实验室 CentOS关机大法
- plsql链接oracle64位报错,PLSQL developer 连接不上64位Oracle 的解决方法
- 【Python】Scrapy抓取多玩Gif图片
- 人大金仓助力广东融合创新智慧校园建设与网络安全交流会成功举办
- 波士顿大学计算机硕士排名,波士顿大学计算机工程研究生排名重点消息重点解析...
- Android 快速集成文档校正能力 超简单
- Python-温度转换
- 打开 CMD 的方式+常用的Dos命令
- DayDayUp:计算机技术与软件专业技术资格证书之《系统集成项目管理工程师》课程讲解之项目管理概述、项目管理基本基础知识、项目立项管理相关知识
- TCP之快重传与快恢复
热门文章
- matlab数据整周期截断,凯塞窗四谱线插值FFT的电力谐波分析方法
- 上班第一天(4)--一个程序员的成长史(13)
- 为什么这个男人爱“拈花惹草”还受阿里人追捧?
- Hybrid A*路径规划器的代码注释
- Lua ipairs、pairs
- C#编程实现阶乘的两种方法
- mysql不识别生僻字_MySQL生僻字插入失败怎样办
- 【阅读笔记】联邦学习实战——联邦学习攻防实战
- 陌上花开,可缓缓归矣
- CodeForces - 1255B Fridge Lockers 思维+建图)