简体   繁体   English

如何使用if函数在r中创建随机损失样本

[英]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.

相关问题 如何从 r 中的.csv 创建随机样本 - How to create a random sample from .csv in r 如何在 R 中创建与向量成比例的随机样本? - How to create random sample proportional to vector in R? 如何修复 R 中样本 function 的随机种子 - How to fix the random seed of sample function in R R使用样本创建具有随机数的矩阵列 - R Using Sample to Create Column of Matrix with Random numbers 如何从 R 中的随机多变量样本创建多个模拟数据集 - How to create multiple simulated datasets from a random multivariate sample in R 有没有办法在不使用 R 中的 sample() 函数的情况下创建向量的排列? - Is there a way to create a permutation of a vector without using the sample() function in R? 如何循环遍历 R 中的虚拟数据以使用示例函数随机替换随机数量的值 NA? - How do I loop through dummy data in R to randomly replace a random amount of values with NA using the sample function? 如何使用 R 中的示例函数将年龄范围 (18-29) 的列重新编码为该年龄范围内的随机数? - How do you recode a column with an age range (18-29) to a random number in this age range using the sample function in R? 有没有办法在 R 中创建一个循环函数来采样 35 组 2 位随机数 - Is there a way to create a recurring function in R to sample 35 sets of 2-digit random numbers R使用随机样本转换二进制数据 - R convert binary data using random sample
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM