R:如何获得两个分布的总和? [英] R: How to get a sum of two distributions?
问题描述
我有一个简单的问题.我想对两个非参数分布求和.
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
我有一个 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屋!