[英]how to create a random loss sample in r using if function
I am working currently on generating some random data for a school project. 我目前正在为学校项目生成一些随机数据。
I have created a variable in R using a binomial distribution to determine if an observation had a loss yes=1 or not=0. 我使用二项式分布在R中创建了一个变量,以确定观察值的损失是否为yes = 1或not = 0。 Afterwards I am trying to generate the loss amount using a random distribution for all observations which already had a loss (=1).
之后,我尝试使用随机分布为所有已经损失(= 1)的观测值生成损失量。
As my loss amount is a percentage it can be anywhere between 0 因为我的损失金额是一个百分比,所以它可以介于0之间
What Is The Intuition Behind Beta Distribution @ stats.stackexchange Beta发行版背后的直觉是什么@ stats.stackexchange
In a third step I am looking for an if statement, which combines my two variables. 在第三步中,我正在寻找一个if语句,它将两个变量组合在一起。
Please find below my code (which is only working for the Loss_Y_N variable): 请在下面找到我的代码(仅适用于Loss_Y_N变量):
Loss_Y_N = rbinom(1000000,1,0.01)
Loss_Amount = dbeta(x, 10, 990, ncp = 0, log = FALSE)
ideally I can combine the two into something like 理想情况下,我可以将两者合并成类似
if(Loss_Y_N=1 then Loss_Amount=dbeta(...) #... is meant to be a random variable with mean=0.15 and should be 0<x=<1
else Loss_Amount=0)
Any input highly appreciated! 任何输入高度赞赏!
Create a vector for your loss proportion. 为您的损失比例创建一个向量。 Fill up the elements corresponding to losses with draws from the beta.
从Beta中抽取与亏损相对应的元素。 Tweak the parameters for the beta until you get the desired result.
调整Beta的参数,直到获得所需的结果。
N <- 100000
loss_indicator <- rbinom(N, 1, 0.1)
loss_prop <- numeric(N)
loss_prop[loss_indicator > 0] <- rbeta(sum(loss_indicator), 10, 990)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.