R:如何求两个分布的总和? [英] R: How to get a sum of two distributions?

查看:130
本文介绍了R:如何求两个分布的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题。
我想将两个非参数分布求和。

I have a simple question. I would like to sum of two non-parametric distributions.

这里是一个示例。
有两个城市有10栋房屋。我们知道每所房屋的能源消耗。 (已编辑)我想获取从每个城市中选择的随机房屋总和的概率分布。

Here is an example. There are two cities which have 10 houses. we know energy consumption for each house. (edited) I want to get the probability distribution of the sum of a random house chosen from each city.

A1 <- c(1,2,3,3,3,4,4,5,6,7) #10 houses' energy consumption for city A
B1 <- c(11,13,15,17,17,18,18,19,20,22) #10 houses' energy consumption for city B

I分别具有A1和B1的概率分布,如何获得A1 + B1的概率分布?
如果我仅在R中使用 A1 + B1 ,它会给出 12 15 18 20 20 22 22 24 26 29 。但是,我认为这是不对的。因为房子没有顺序。

I have a probability distribution of A1 and B1, how can I get the probability distribution of A1+B1? If I just use A1+B1 in R, it gives 12 15 18 20 20 22 22 24 26 29. However, I don't think this is right. Becuase there is not order in houses.

当我改变房子的顺序时,会给出另一个结果。

When I change the order of houses, it gives another results.

# Original
A1 <- c(1,2,3,3,3,4,4,5,6,7)
B1 <- c(11,13,15,17,17,18,18,19,20,22)
#change order 1
A2 <- c(7,6,5,4,4,3,3,3,2,1) 
B2 <- c(22,20,19,18,18,17,17,15,13,11)
#change order 2
A3 <- c(3,3,3,4,4,5,6,7,1,2) 
B3 <- c(17,17,18,18,19,13,20,11,22,15)
sum1 <- A1+B1; sum1
sum2 <- A1+B2; sum2
sum3 <- A3+B3; sum3

红线是sum1,sum2和sum3。我不确定如何获得两个分布之和的分布。谢谢。

Red lines are sum1, sum2, and sum3. I am not sure how can I get the distribution of the sum of two distributions.Please give me any ideas.Thanks!

(如果这些分布是正态分布或均匀分布,我可以很容易地得到分配的总和,但这不是正常现象,没有顺序)

(If those distributions are normal or uniform distributions, I could get the sum of distribution easily, but these are not a normal and there is no order)

推荐答案

您可能想要一些东西像这样:

You probably want something like:

rowSums(expand.grid(A1, B1))

使用 expand.grid 将为您提供A1和B1所有组合以及 rowSums 会添加它们。

Using expand.grid will get you a dataframe of all combinations of A1 and B1, and rowSums will add them.

这篇关于R:如何求两个分布的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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