“圆形"R中的意思 [英] "circular" mean in R

查看:9
本文介绍了“圆形"R中的意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个包含月份的数据集,考虑到月份是循环的,我如何计算平均"月份?

Given a dataset of months, how do I calculate the "average" month, taking into account that months are circular?

months = c(1,1,1,2,3,5,7,9,11,12,12,12)
mean(months)
## [1] 6.333333

在这个虚拟示例中,平均值应为 1 月或 12 月.我看到有循环统计的包,但我不确定它们是否适合我的需求.

In this dummy example, the mean should be in January or December. I see that there are packages for circular statistics, but I'm not sure whether they suit my needs here.

推荐答案

我认为

months <- c(1,1,1,2,3,5,7,9,11,12,12,12)
library("CircStats")
conv <- 2*pi/12 ## months -> radians

现在将月份转换为弧度,计算圆形平均值,然后转换回月份.我在这里减去 1 假设 1 月在0 弧度"/12 点钟...

Now convert from months to radians, compute the circular mean, and convert back to months. I'm subtracting 1 here assuming that January is at "0 radians"/12 o'clock ...

(res1 <- circ.mean(conv*(months-1))/conv)

结果是 -0.3457.你可能想要:

The result is -0.3457. You might want:

(res1 + 12) %% 12

给出 11.65,即 12 月的中途(因为我们仍处于 0=1 月,11=12 月)

which gives 11.65, i.e. partway through December (since we are still on the 0=January, 11=December scale)

认为这是对的,但没有仔细检查.

I think this is right but haven't checked it too carefully.

对于它的价值,CircStats::circ.mean 函数非常简单——如果这就是你所需要的,那么加载包的开销可能不值得:

For what it's worth, the CircStats::circ.mean function is very simple -- it might not be worth the overhead of loading the package if this is all you need:

function (x) 
{
    sinr <- sum(sin(x))
    cosr <- sum(cos(x))
    circmean <- atan2(sinr, cosr)
    circmean
}

从评论中结合@A.Webb 的巧妙替代方案:

Incorporating @A.Webb's clever alternative from the comments:

 m <- mean(exp(conv*(months-1)*1i))
 12+Arg(m)/conv%%12  ## 'direction', i.e. average month
 Mod(m)              ## 'intensity'

这篇关于“圆形"R中的意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆