用Rstudio模拟出生——绘制pmf和cdf

Simulate birth with Rstudio - draw pmf and cdf

我有一个小练习要用 Rstudio 来解决我的统计考试。 我试着把它翻译成英文,所以如果有什么不明白的地方请问我解释。

"模拟 100,000 次出生并使用以下概率:男性 51.3%,女性 48.7%,使用 样本 函数。

我得到了51356个男性和48644个女性,相差56个。

但是现在,如何绘制概率函数的PMF和CDF?

我把模拟出生的代码放在这里:

mysample <- data.frame(sample(c("M","F"),100000,replace=T,prob=c(0.513,0.487)))
names(mysample)<-c("Gender")
males <- subset(mysample, Gender=="M")
females <- subset(mysample,Gender=="F")

theoricM <- 100000*0.513
theoricF <- 100000*0.487
realM <- as.integer(nrow(maschi))
realF <- as.integer(nrow(femmine))

#create a data frame to show differences
result <-data.frame(realM,theoricM,realF,theoricF)
names(result)<- c("Males","Theoric Males","Females","Theoric Females")

结果:

希望有人能帮助我,我知道这对于有 R 经验的人来说是一个非常简单的问题,但我才刚刚开始使用这种语言。

谢谢大家的回复。

编辑:

我试过这段代码:

x <- 1:50
plot(x,dbinom(x ,size = 50,prob = 0.513),type="l", ylab="PMF", main="Binomial Distribution PMF")

结果是:

我想我的理解是,由于概率非常接近 1/2,所以在一组 50 次出生中,男性的数量将非常接近 25。显示的是什么情节?而且,这是正确的方法吗?

你的代码(和结论)对我来说是正确的。

使用 type="h" 绘制 "high-density" 图可能在图形上更好;这更清楚地表明 x.

的 non-integer 值的概率为零
x <- 1:50
par(las=1,bty="l") ## cosmetic
plot(x,dbinom(x ,size = 50,prob = 0.513),type="h", ylab="PMF", 
     main="Binomial Distribution PMF")

(绘制 CDF/CMF 时,您可能需要使用 type="s"type="S";参见 ?plot