获得两组分数的均值的绝对偏差 [英] Obtaining absolute deviation from mean for two sets of scores

查看:150
本文介绍了获得两组分数的均值的绝对偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要获得两组得分均值的绝对偏差,我通常需要在R中编写如下所示的长代码.

To obtain absolute deviation from the mean for two groups of scores, I usually need to write long codes in R such as the ones shown below.

我想知道是否可以在BASE R中以某种方式Vectorize mad()函数,以便在下面显示的示例中使用每组分数的平均分数的绝对偏差可以使用mad()Vectorized版本?任何其他可行的想法都受到高度赞赏?

I was wondering if it might be possible in BASE R to somehow Vectorize the mad() function so that the absolute deviation from the mean scores for each group of scores in the example I'm showing below could be obtained using that Vectorized version of mad()? Any other workable ideas are highly appreciated?

set.seed(0)
     y = as.vector(unlist(mapply(FUN = rnorm, n = c(10, 10)))) # Produces two sets of scores
groups = factor( rep(1:2, times = c(10, 10) ) )                # Grouping ID variable

G1 = y[groups == 1]              # subset y scores for group 1
G2 = y[groups == 2]              # subset y scores for group 2
G1.abs.dev = abs(G1 - mean(G1))  # absolute deviation from mean scores for group 1
G2.abs.dev = abs(G2 - mean(G2))  # absolute deviation from mean scores for group 2

推荐答案

如何

score <- lapply(split(y, groups), FUN = function (u) abs(u - mean(u)))

score <- ave(y, groups, FUN = function (u) abs(u - mean(u)))

结果以不同的方式组织.选择最适合您的一种.

The results are organized in a different way. Choose the one that is most comfortable to you.

您的措辞有误. mad返回数据的单个统计信息/值.例如,

There is something wrong with your wording. mad returns a single statistic / value for data. For example,

sapply(split(y, groups), mad)

您不是向量化mad,而是像示例代码所示那样简单地计算每个基准的偏差.

You are not vectorizing mad, but simply computing the deviation for each datum as your example code shows.

这篇关于获得两组分数的均值的绝对偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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