简体   繁体   English

如何执行模拟以在 R 中找到具有给定概率的 z-score (x)

[英]How do I perform a simulation to find a z-score (x) with a given probability in R

As the title illustrates, I would like to conduct a simulation test.正如标题所示,我想进行模拟测试。 I was given a probability P(L>x)=0.05, and L follows a normal distribution with mean=0, std=100.我得到的概率为 P(L>x)=0.05,L 服从均值=0、std=100 的正态分布。 I was asked to perform some sort of simulation, IDEALLY using a hit-or-miss approach multiple times to do so in order to find an appropriate x.我被要求执行某种模拟,最好使用多次命中或未命中的方法来执行此操作,以便找到合适的 x。 I was not allowed to use qnorm() function.我不被允许使用 qnorm() 函数。 Can you please help me out?你能帮我一下吗? Thank you谢谢

As we want P(L>x)=0.05, we can create a function that calculates P(L>x)-0.05, and find the x that turns it to 0 (its root) with uniroot :由于我们想要 P(L>x)=0.05,我们可以创建一个计算 P(L>x)-0.05 的函数,并使用uniroot找到将其变为 0(其根)的uniroot

prob = function(x){
  n = 10000
  L = rnorm(n,0,100)
  sum(L > x)/n - 0.05}

uniroot(prob, c(-400,-50))

Obs: the second argument for uniroot is the arbitrary interval where it'll try to find the root. Obs: uniroot的第二个参数是它尝试找到根的任意间隔。

This will find a different root every time you run it as L is created inside prob .每次运行它时都会发现不同的根,因为L是在prob创建的。 For better accuracy, you can increase n .为了获得更好的准确性,您可以增加n

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

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