setting upper and lower limits in rnorm

前端 未结 5 1337
自闭症患者
自闭症患者 2020-12-03 14:04

I am simulating data using rnorm, but I need to set an upper and lower limit, does anyone know how to do this?

code:

rnorm(n = 10, mean = 39.74, sd =         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 14:42

    You can make your own truncated normal sampler that doesn't require you to throw out observations quite simply

    rtnorm <- function(n, mean, sd, a = -Inf, b = Inf){
        qnorm(runif(n, pnorm(a, mean, sd), pnorm(b, mean, sd)), mean, sd)
    }
    

提交回复
热议问题