简体   繁体   English

R获得概率分布

[英]R obtaining a probability distribution

I have a relationship: y = a + b + c 我有一个关系:y = a + b + c

I have the average and standard deviation of a, b and c and I would like to obtain the probability distribution of y from this by Monte Carlo simulation. 我有a,b和c的平均值和标准偏差,我想通过蒙特卡洛模拟从中获得y的概率分布。

Is there a function or package or easy way that I can use to do this? 有没有可以用来执行此操作的功能或包或简单方法?

I assume that your are assuming your inputs a,b and c are normally distributed because you say you can define them with mean and standard deviation. 我假设您假设输入a,b和c呈正态分布,因为您说可以定义均值和标准差。 If that is the case, you can do this pretty fast without any special package. 如果是这样,您可以在没有任何特殊软件包的情况下快速完成此操作。

 mu.a=33
 mu.b=32
 mu.c=13
 sigma.a=22
 sigma.b=22
 sigma.c=222

n= a.large.number=10^5
a=rnorm(n,mu.a,sigma.a)
b=rnorm(n,mu.b,sigma.b)
c=rnorm(n,mu.c,sigma.c)
y=a+b+c
plot(density(y))
mean(y)
sd(y)

Make sure to be aware of all the assumptions we are making about y , a , b and c . 确保了解我们对yabc所做的所有假设。 If you want to do something more complex like figure out the sampling variance of the mean of y. 如果您想做更复杂的事情,例如找出y均值的抽样方差。 Then do this procedure many times collecting the mean and plot it. 然后,多次执行此过程以收集均值并将其绘制出来。

mysimfun=function(n,mu,sigma,stat.you.want='mean') 
   #  mu is length 3 and sigma is too.

{
n= a.large.number=10^5
    a=rnorm(n,mu[1],sigma[1])
    b=rnorm(n,mu[2],sigma[2])
    c=rnorm(n,mu[3],sigma[3])
    y=a+b+c
    plot(density(y))


return(ifelse(stat.you.want=='mean',mean(y),sd(y))
}


mu=c(mu.a,my.b,mu.c)
sigma=c(sigma.a,sigma.b,sigma.c)
mi=rep(NA,100)

Then run it in a loop of some sort. 然后以某种循环运行它。

for(i in 1:100) {mi[i]=mysimfun(10,mu,sigma,stat.you.want='mean') }

par(mfrow=c(2,1)
hist(mi)
plot(density(mi))

mean(mi)
sd(mi)

There would be two approaches: bootstrapping which I think is what you might mean by MonteCarlo or if you are more interested in the theory than constructing estimates from empiric distributions, the 'distr' package and its friends 'distrSim" and "distrTEst". 有两种方法:自举(我认为这是MonteCarlo的意思),或者如果您对理论更感兴趣,而不是根据经验分布来构建估计,则使用“ distr”软件包及其朋友“ distrSim”和“ distrTEst”。

require(boot)
ax <- rnorm(100); bx<-runif(100); cx<- rexp(100)
dat <- data.frame(ax=ax,bx=bx,cx=cx)

boot(dat, function(d){ with(d, mean(ax+bx+cx) )}, R=1000,  sim="parametric")
boot(dat, function(d){ with(d, sd(ax+bx+cx) )}, R=1000,  sim="parametric")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM