簡體   English   中英

模擬從R中的截斷邏輯分布生成隨機數

[英]Simulation to generate random numbers from a truncated logistic distribution in R

我編寫了一個代碼,以在R中生成邏輯隨機變量,但截斷值在0.2到0.5之間。 那里的任何人都可以幫助正確編寫它,尤其是在if條件下。 謝謝。

<##The Logistic distribution##
ns=10
for(i in 1:100){
rtruncl=function(u,a,b,alpha1,alpha2)
#error: {
qalpha1 = log(alpha1/(1-alpha1))
qalpha2 = log((1-alpha2)/alpha2)
X=a+b*log(u/(1-u))
u=runif(ns)
if((X >= qalpha1) && (X <= qalpha2)){
# error: }
a=0; b=2 ##a is a location parameter and b is a scale parameter##
t1=rtruncl(u,a,b, 0.2,0.5)
t1
}
}  #closed the for loop

您可以使用R中內置的邏輯分布函數(例如rlogis(...)以及truncdistrlogis(...)來執行此操作。

library(truncdist)
location <- 0
scale    <- 2
X <- rlogis(1000, location=location, scale=scale)
Y <- rtrunc(1000, spec="logis", a=-5,b=5, location=location, scale=scale)
par(mfrow=c(1,2))
hist(X, xlim=c(-10,10), breaks=30, freq=F, col="lightgreen")
hist(Y, xlim=c(-10,10), breaks=30, freq=F, col="lightblue")

如果這是某種家庭作業,以顯示如何計算截斷的分布,那么我建議您看一下rtrunc(...)函數的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM