R語言 二項分布

二項分布模型處理在一系列實驗中僅發(fā)現(xiàn)兩個可能結(jié)果的事件的成功概率。 例如咬像,擲硬幣總是給出頭或尾明未。 在二項分布期間估計在10次重復(fù)拋擲硬幣中精確找到3個頭的概率轩触。

R語言有四個內(nèi)置函數(shù)來生成二項分布。 它們描述如下匠襟。

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">dbinom(x, size, prob)
pbinom(x, size, prob)
qbinom(p, size, prob)
rbinom(n, size, prob)
</pre>

以下是所使用的參數(shù)的描述 -

  • x是數(shù)字的向量钝侠。

  • p是概率向量。

  • n是觀察的數(shù)量酸舍。

  • size是試驗的數(shù)量帅韧。

  • prob是每個試驗成功的概率。

dbinom()

該函數(shù)給出每個點的概率密度分布啃勉。

<pre class="prettyprint notranslate tryit" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># Create a sample of 50 numbers which are incremented by 1.
x <- seq(0,50,by = 1)

Create the binomial distribution.

y <- dbinom(x,50,0.5)

Give the chart file a name.

png(file = "dbinom.png")

Plot the graph for this sample.

plot(x,y)

Save the file.

dev.off()
</pre>

當(dāng)我們執(zhí)行上面的代碼忽舟,它產(chǎn)生以下結(jié)果 -

dbinom()圖

pbinom()

此函數(shù)給出事件的累積概率。 它是表示概率的單個值。

<pre class="prettyprint notranslate tryit" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># Probability of getting 26 or less heads from a 51 tosses of a coin.
x <- pbinom(26,51,0.5)

print(x)
</pre>

當(dāng)我們執(zhí)行上面的代碼叮阅,它產(chǎn)生以下結(jié)果 -

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[1] 0.610116
</pre>

qbinom()

該函數(shù)采用概率值刁品,并給出累積值與概率值匹配的數(shù)字。

<pre class="prettyprint notranslate tryit" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># How many heads will have a probability of 0.25 will come out when a coin is tossed 51 times.
x <- qbinom(0.25,51,1/2)

print(x)
</pre>

當(dāng)我們執(zhí)行上面的代碼浩姥,它產(chǎn)生以下結(jié)果 -

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[1] 23
</pre>

rbinom()

該函數(shù)從給定樣本產(chǎn)生給定概率的所需數(shù)量的隨機值挑随。

<pre class="prettyprint notranslate tryit" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># Find 8 random values from a sample of 150 with probability of 0.4.
x <- rbinom(8,150,.4)

print(x)
</pre>

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -

<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">[1] 58 61 59 66 55 60 61 67</pre>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末勒叠,一起剝皮案震驚了整個濱河市兜挨,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌缴饭,老刑警劉巖暑劝,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異颗搂,居然都是意外死亡担猛,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門丢氢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來傅联,“玉大人,你說我怎么就攤上這事疚察≌糇撸” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵貌嫡,是天一觀的道長比驻。 經(jīng)常有香客問我,道長岛抄,這世上最難降的妖魔是什么别惦? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮夫椭,結(jié)果婚禮上掸掸,老公的妹妹穿的比我還像新娘。我一直安慰自己蹭秋,他們只是感情好扰付,可當(dāng)我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著仁讨,像睡著了一般羽莺。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上洞豁,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天禽翼,我揣著相機與錄音屠橄,去河邊找鬼。 笑死闰挡,一個胖子當(dāng)著我的面吹牛锐墙,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播长酗,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼溪北,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了夺脾?” 一聲冷哼從身側(cè)響起之拨,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎咧叭,沒想到半個月后蚀乔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡菲茬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年吉挣,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片婉弹。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡睬魂,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出镀赌,到底是詐尸還是另有隱情氯哮,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布商佛,位于F島的核電站喉钢,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏良姆。R本人自食惡果不足惜肠虽,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望歇盼。 院中可真熱鬧舔痕,春花似錦评抚、人聲如沸豹缀。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽邢笙。三九已至,卻和暖如春侍匙,著一層夾襖步出監(jiān)牢的瞬間氮惯,已是汗流浹背叮雳。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留妇汗,地道東北人帘不。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像杨箭,于是被迫代替她去往敵國和親寞焙。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容

  • 2017-6-20【能量世界1021天】 桂林陽朔 夢境體驗者:陳艷霞 夢·頻率 夢中互婿,和妹妹的 姐夫的捣郊, 媽媽的...
    陳艷霞小樹媽閱讀 328評論 2 3
  • 得不到的永遠(yuǎn)在騷動呛牲,被偏愛的都有恃無恐。也許正是這個道理驮配,才讓我們明白娘扩,任何給予都要有一個度,多一點不行僧凤,少一點也...
    一個人的春夏秋冬閱讀 1,053評論 15 6