<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1101350361280593719</id><updated>2026-02-14T04:09:30.358-08:00</updated><category term="R"/><category term="Statistics"/><category term="R-function"/><category term="Generalized regression"/><category term="LATEX"/><category term="HTML"/><category term="Database"/><category term="MATLAB"/><category term="Mixed model"/><category term="OS X"/><category term="PDF"/><category term="PHP"/><category term="PowerPoint"/><title type='text'>Apan’s Notes</title><subtitle type='html'>Mac, LATEX, HTML, Statistics, and Programing.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>34</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-8994012615836903524</id><published>2018-05-21T05:57:00.001-07:00</published><updated>2018-05-21T06:01:56.742-07:00</updated><title type='text'>An R function: OLS/Robust scaled mass index</title><content type='html'>&lt;p class=&quot;abstract&quot;&gt;&lt;/p&gt;
&lt;h4&gt;&lt;/h4&gt;
&lt;h5&gt;&lt;/h5&gt;


&lt;h4&gt;R code&lt;/h4&gt;
&lt;script src=&quot;https://gist.github.com/chenpanliao/8ef652404d024c2c4191b8f159400a37.js&quot;&gt;&lt;/script&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;

&lt;h4&gt;Example&lt;/h4&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;library(magrittr)
library(data.table)
library(ggplot2)
library(ggpubr)
set.seed(123)
dt &lt;-
  data.table(`Body length` = seq(1, 5, 0.5)) %&gt;%
  .[, `Body weight` := 2 * `Body length` ^ (3 + rnorm(length(`Body length`), sd = 0.001))] %&gt;%
  rbind(., data.table(`Body length` = 6, `Body weight` = 200)) %&gt;%
  .[order(`Body length`), ]

dt.SMI &lt;-
  scaledMassIndex(dt$`Body length`, dt$`Body weight`, x.0 = 2)
summary(dt.SMI)

g1 &lt;-
  ggplot(dt, aes(`Body length`, `Body weight`)) +
  geom_point() +
  geom_line(aes(x, y, color = Method),
            data = attr(dt.SMI, &quot;pred&quot;) %&gt;% melt(., &quot;x&quot;, c(&quot;y.ols&quot;, &quot;y.rob&quot;), &quot;Method&quot;, &quot;y&quot;)) +
  scale_colour_discrete(labels = c(&quot;OLS&quot;, &quot;Robust (M-estimation)&quot;))
g2 &lt;-
  ggplot(dt, aes(log10(`Body length`), log10(`Body weight`))) +
  geom_point() +
  geom_line(
    aes(log10(x), log10(y), color = Method),
    data = attr(dt.SMI, &quot;pred&quot;) %&gt;% melt(., &quot;x&quot;, c(&quot;y.ols&quot;, &quot;y.rob&quot;), &quot;Method&quot;, &quot;y&quot;)
  ) +
  scale_colour_discrete(labels = c(&quot;OLS&quot;, &quot;Robust (M-estimation)&quot;))
g3 &lt;-
  ggplot(melt(dt.SMI, &quot;x&quot;, c(&quot;SMI.ols&quot;, &quot;SMI.rob&quot;), &quot;Method&quot;),
         aes(x, value)) +
  geom_point(aes(color = Method)) +
  xlab(&quot;Body length&quot;) +
  ylab(&quot;SMI (at body length = 2)&quot;) +
  scale_colour_discrete(labels = c(&quot;OLS&quot;, &quot;Robust (M-estimation)&quot;))
ggarrange(g1,
          g2,
          g3,
          ncol = 1,
          nrow = 3,
          align = &quot;hv&quot;)&lt;/pre&gt;

&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEid7lqT6ime3L0o_zQCiZfrLbDnIpNiKJo09uOwZ-HppzxDUEt19n2UBxLEhvstpDKTqMzG63YLBoHmFe2gPC_2g80uHXdHFpauC-oH48bdHsT9Kz1HuzXGAjSwwzdMUHBchIFnBWFSRu4/s1600/Rplot.png&quot; /&gt;&lt;/p&gt;
</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/8994012615836903524/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2018/05/an-r-function-olsrobust-caled-mass-index.html#comment-form' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/8994012615836903524'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/8994012615836903524'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2018/05/an-r-function-olsrobust-caled-mass-index.html' title='An R function: OLS/Robust scaled mass index'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEid7lqT6ime3L0o_zQCiZfrLbDnIpNiKJo09uOwZ-HppzxDUEt19n2UBxLEhvstpDKTqMzG63YLBoHmFe2gPC_2g80uHXdHFpauC-oH48bdHsT9Kz1HuzXGAjSwwzdMUHBchIFnBWFSRu4/s72-c/Rplot.png" height="72" width="72"/><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-2951708178969517633</id><published>2015-12-19T09:39:00.000-08:00</published><updated>2015-12-21T09:40:55.015-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><title type='text'>R 繪圖：柱狀圖的低階繪圖範例</title><content type='html'>&lt;p class=&quot;abstract&quot;&gt;R 繪圖時的柱狀圖如何加上誤差線？如何使用線條填色？如何把圖示放在外部？本文提供一個例子。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmz1RdA1oqvuMeLR_IDbkuPHCCHxbvqQupgdu6CoSNjxNKfF9xPVGB1yLz7zEg-ioLy2w1J4B6zzgUAUo7t9PsZvTfzwRg7PcwBRNBatjiYt-cv3u4DM-3GFyQ4eZULqG11wqv-A9faZ4/s800/R-bar-plot-example.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;以下是產生上圖的原始碼。&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/chenpanliao/416255b9a4ae03e71ac0.js&quot;&gt;&lt;/script&gt;
</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/2951708178969517633/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2015/12/r-barplot-error-bar-line-fill.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/2951708178969517633'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/2951708178969517633'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2015/12/r-barplot-error-bar-line-fill.html' title='R 繪圖：柱狀圖的低階繪圖範例'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmz1RdA1oqvuMeLR_IDbkuPHCCHxbvqQupgdu6CoSNjxNKfF9xPVGB1yLz7zEg-ioLy2w1J4B6zzgUAUo7t9PsZvTfzwRg7PcwBRNBatjiYt-cv3u4DM-3GFyQ4eZULqG11wqv-A9faZ4/s72-c/R-bar-plot-example.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-4652856041724293611</id><published>2015-06-19T12:57:00.000-07:00</published><updated>2015-06-19T12:58:40.091-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>「在 95% 信心水準，抽樣誤差於正負 3.1% 以內」到底是什麼意思？</title><content type='html'>&lt;p&gt;如果你對統計學有點興趣，可以往下看看這篇文。不知不覺寫得有點長。&lt;/p&gt;

&lt;p&gt;故事：「某民調訪問 1000 名受訪者，有 40% 比例的人支持；在 95% 信心水準，抽樣誤差於正負 3.1% 以內。」現實中，民調常常出現相似的句子。我相信很多人看不懂這句話到底是什麼意思，或是誤解了這句話。所以現在來聊聊這到底是什麼鬼好了。&lt;/p&gt;

&lt;p&gt;我們先把上面這個故事改成另一種情境好了。&lt;/p&gt;

&lt;p&gt;例子：「你有一個大袋子。袋子裡有無數顆球。球只可能是黑色或白色。你希望知道一件事：到底黑球在大袋子中的個數比例是多少。現在，你抽出 1000 顆球，然後你發現有 400 顆是黑球（40%）。在 95% 信心水準，抽樣誤差於正負 3.1% 以內，也就是黑球比例的區間估計是 40% − 3.1% 到 40% + 3.1%。」&lt;/p&gt;

&lt;p&gt;如果你可以看出這個例子其實和民調結果的故事是同一件事，那就可以往下看了。&lt;/p&gt;

&lt;p&gt;在例子中，每一次調查就是抽出 n 顆球（例子中的 n = 1000）。袋子中黑球的比例，也就是我們希望知道的未知數，我們叫 p 好了。因為球實在太多了，我們不可能知道 p 的大小，只有抽出 n 顆球來估 p 的大小。&lt;/p&gt;

&lt;p&gt;在統計學理，我們可以在還沒抽出球之前就預知「每次調查有多準」。什麼叫「有多準」呢？統計學的辦法是決定一個機率和設計一個區間，並估計 p 有多大的機率會座落在這個設計出來的區間之內。如果你每次調查抽出的球數 n 很大很大，那這個區間會很容易計算。&lt;/p&gt;

&lt;p&gt;有點難懂？我試著換個方式說。例如，如果我可以做 100 次相同調查，我將會得到 100 個黑球比例和 100 個區間。我可以保證，這 100 次調查中，約有 95 個區間會包括了那個我們不可能知道的 p。這就是「在 95% 信心水準」的實際意義。&lt;/p&gt;

&lt;p&gt;你也許會問，可不可能算 100 個區間有 99 個區間會包括 p 的調查？可以，但每個區間大小會變寬（由 ± 3.1% 變成 ± 4.1%），但結果也變得更值得信賴（因為區間將有 99% 的機率可以包括了真實的 p）。&lt;/p&gt;

&lt;p&gt;如果你每次抽出更多顆球，那每個區間也都會變窄（因為抽出越多球就準嘛）。例如，每次抽出 5000 顆球，那這個區間大小會由 ± 3.1% 變成 ± 1.4%。&lt;/p&gt;

&lt;p&gt;回到原本的例子。如果以相同的調查方法（一樣訪問 1000 名受訪者的情況下），並且重覆很多很多次（當然，實際上只進行了一次）。我們可以保證，那個我們想估計的支持度將座落在 95% 次所算出的區間之內。在故事中，這次調查結果的區間是 40% − 3.1% 到 40% + 3.1%。我們 *永遠不可能* 知道這次得到的區間會不會包括了真實的支持度 p，但它很可能會包括，因為這個調查方法就保證了進行 100 次約有 95 次是成立的。&lt;/p&gt;

&lt;p&gt;有些人會把 40% − 3.1% 到 40% + 3.1% 理解成因為調查進行所造成的誤差，例如打錯資料、拒絕受訪、無效結果之類的。這都不是信賴區間的意義。&lt;/p&gt;

&lt;p&gt;我並不想把這中間的數學式寫出來啦。只想說個概念。&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/4652856041724293611/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2015/06/confidence-interval.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/4652856041724293611'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/4652856041724293611'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2015/06/confidence-interval.html' title='「在 95% 信心水準，抽樣誤差於正負 3.1% 以內」到底是什麼意思？'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-1193287615148610702</id><published>2015-03-16T06:41:00.000-07:00</published><updated>2015-03-16T06:41:00.093-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><title type='text'>《初學 R 語言的 60 分鐘》投影片</title><content type='html'>&lt;p class=&quot;abstract&quot;&gt;2015 年 3 月 14 日於蓮華池研究中心所舉行的台灣生態研究網年會中，我針對 R 語言的初學者所設計的課程投影片。&lt;/p&gt;

&lt;iframe src=&quot;//www.slideshare.net/slideshow/embed_code/44171070&quot; width=&quot;425&quot; height=&quot;355&quot; frameborder=&quot;0&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; scrolling=&quot;no&quot; style=&quot;border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;&quot; allowfullscreen&gt; &lt;/iframe&gt; &lt;div style=&quot;margin-bottom:5px&quot;&gt; &lt;strong&gt; &lt;a href=&quot;//www.slideshare.net/chenpanliao/slide-44171070&quot; title=&quot;初學R語言的60分鐘&quot; target=&quot;_blank&quot;&gt;初學R語言的60分鐘&lt;/a&gt; &lt;/strong&gt; from &lt;strong&gt;&lt;a href=&quot;//www.slideshare.net/chenpanliao&quot; target=&quot;_blank&quot;&gt;Chen-Pan Liao&lt;/a&gt;&lt;/strong&gt; &lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/1193287615148610702/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2015/03/rSlideForBeginner.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/1193287615148610702'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/1193287615148610702'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2015/03/rSlideForBeginner.html' title='《初學 R 語言的 60 分鐘》投影片'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total><georss:featurename>Taiwan, 南投縣魚池鄉五城村</georss:featurename><georss:point>23.918712892212021 120.88516715967648</georss:point><georss:box>23.918259392212022 120.88453665967648 23.91916639221202 120.88579765967648</georss:box></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-5465120411430519954</id><published>2014-12-06T12:54:00.002-08:00</published><updated>2014-12-06T16:13:19.260-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>統計檢驗簡介：一個二項式分配的簡單例子</title><content type='html'>&lt;p&gt;最近回應了一篇在 &lt;a href=&quot;https://www.ptt.cc/bbs/Statistics/M.1417877983.A.554.html&quot;&gt;PTT 統計板的問題：「[問題] 顯著水準的意思」&lt;/a&gt;。&lt;/p&gt;
&lt;blockquote&gt;
&lt;pre&gt;
※ 引述《mrlee112233 (小史)》之銘言：
: 不好意思
: 我想問一下顯著水準
: 有沒有簡單一點的解釋
: 我上網查過資料
: 但因爲我不是相關科系
: 所以我跟本看不懂在寫什麼
: 虛無假設.type l error .type ll error..等
: 跟本不懂=   =
: 我文獻報告裏
: 他假設alpha=0.05 然後用二相分佈去做計算
: 所以想問有沒有比較淺顯易懂的解釋
: 謝謝
&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;以下則是我的回應，也歡迎從 &lt;a href=&quot;https://www.ptt.cc/bbs/Statistics/M.1417897430.A.89F.html&quot;&gt;PTT 上我原本的回應&lt;/a&gt;觀看相同的內容。&lt;/p&gt;


&lt;blockquote&gt;
&lt;p&gt;我會建議你從教科書或聽課來學習這一連串的概念，但我也可以體會非相關科系的朋友不容易了解這些概念，所以寫一個例子給你參考，並省略一些太艱澀的話語。&lt;/p&gt;

&lt;p&gt;假如有一個袋子，裡有無數顆球，其中球不是黑的就是白的。你可以獨立抽出 10 顆球，並記錄每顆球是什麼顏色。在抽出球之前，你可以設立一個虛無假說：「袋中的黑球和白球比例是 1:1」。當然，你並不知道這是不是對的，但就先這麼假設吧。&lt;/p&gt;

&lt;p&gt;假如這個虛無假說是真實的，又因為你會抽出 10 顆球，所以你可以預先算出你抽出 0 顆黑球到 10 個顆黑球的機率。例如，抽出 0 顆黑球的機率是 0.0009766，抽出 1 顆黑球的機率是 0.009766，抽出 5 顆黑球的機率是 0.2461，…… 記得，這在還沒有抽球之前，就可以算得的。&lt;/p&gt;

&lt;p&gt;接下來，你可以設立一個和虛無假說相對的假說，叫對立假說。我們就說這個對立假說是「袋中的黑球和白球比例不是 1:1」。這裡會跳出單尾和雙尾檢驗的概念，但我就不多說了，反正我們就做雙尾檢驗吧。另外，我們也要設定顯著水準，常見為 0.05。&lt;/p&gt;

&lt;p&gt;這時候，你可以抽出 10 顆球了。如果你抽出 5 顆黑球和 5 顆白球，那你可能會相信虛無假說是對的。但如果你抽出 0 顆黑球和 10 顆白球，或是 10 顆黑球和 0 顆白球，那你可能會大大地懷疑虛無假說，而相信對立假說才是對的。問題來了：到底要多麼地違背虛無假說，你才相信對立假說？這就是顯著水準 = 0.05 的作用。&lt;/p&gt;

&lt;p&gt;例如，如果你真的抽出 1 顆黑球和 9 顆白球好了。發生這種情況的機率是 0.009766（假如虛無假說是對的），另外，還有三種情況和 1 黑 9 白一樣程度或更違反虛無假說，分別是 0 黑 10 白（機率是 0.0009766）、9 黑 1 白（機率是 0.009766）、10 黑 0 白（機率是 0.0009766）。這四種情況的機率總共是 0.02148，稱為 p-value。&lt;/p&gt;

&lt;p&gt;這時候，因為這個 p-value = 0.02148 比你設定的顯著水準小，所以你可以下一個結論：在 0.05 的顯著水準下，虛無假說不被接受。&lt;/p&gt;

&lt;p&gt;當然，你也可能猜錯了，因為即使虛無假說是真的，你還是有個很小的機率抽到 1 黑 9 白或更偏激的結果。這種猜錯的情況就稱為型一錯誤。不過因為通常我們會設定一個滿小的顯著水準，所以型一錯誤不甚容易發生。&lt;/p&gt;

&lt;p&gt;換一個情況，假如你抽出的是 2 黑 8 白呢？這時候，下列的所有情況的機率和就是 p-value：0 黑 10 白（機率是 0.0009766）、1 黑 9 白（機率是 0.009766）、2 黑 8 白（機率是 0.0439）、8 黑 2 白（機率是 0.0439）、9 黑 1 白（機率是 0.009766）、10 黑 0 白（機率是 0.0009766）。加起來的機率是 p-value = 0.1094，比顯著水準大了。這時候，你可以下一個結論：沒有證據指出虛無假說錯了，在 0.05 的顯著水準下。&lt;/p&gt;

&lt;p&gt;再一次地，你也可能猜錯了，因為，說不定虛無假說並不正確（例如真實情況是 30% 黑球 70% 白球）。這種猜錯的情形就叫型二錯誤。要儘量避免型二錯誤是可能的（關鍵字：檢定力），但就不多說了。&lt;/p&gt;

&lt;p&gt;整個故事其實不複雜，好啦，我寫得讓它變得有點複雜了。在虛無假說為真的條件下，取得目前及更偏離虛無假說的結果之機率叫 p-value，並拿它與顯著水準相比較。&lt;/p&gt;

&lt;p&gt; 因此，顯著水準被當做一個門檻、一個標準，來決定我們拒絕或不拒絕虛無假說。&lt;/p&gt;

&lt;p&gt;我原本以為可以用最簡單的例子說明這一連串的概念，結果還是用了這麼多字。算是騙 p 幣好了。&lt;/p&gt;
&lt;/blockquote&gt;
</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/5465120411430519954/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2014/12/introduction-of-statistical-hypothesis.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/5465120411430519954'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/5465120411430519954'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2014/12/introduction-of-statistical-hypothesis.html' title='統計檢驗簡介：一個二項式分配的簡單例子'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-7223943328538126333</id><published>2014-01-02T18:11:00.000-08:00</published><updated>2014-09-14T01:18:08.876-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="R-function"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>An R function: Exact one/paired sample test for mean(s)</title><content type='html'>&lt;p class=&quot;abstract&quot;&gt;Here I provide a &lt;em&gt;exact&lt;/em&gt; one sample or paired sample test for mean(s), which is the &lt;em&gt;exact&lt;/em&gt; version of permutation test to compare a single sample against a mean or to compare paired samples’ differences against a mean.&lt;/p&gt;

&lt;p&gt;
The algorithm in this function is based on Bryan F.&amp;nbsp;J.&amp;nbsp;Manly. 1997. 
&lt;em&gt;Randomization, bootstrap and Monte Carlo methods in biology&lt;/em&gt;. 2nd edition. pp. 91-97.
That is, all possible permutations (i.e.&amp;nbsp;all possible cases of exchange between $x_i$ and $\mu$ in one-sample test or all possible cases of exchange between $x_{1i}$ and $x_{2i}$) in paired-sample test are processed to calculate a &lt;em&gt;exact&lt;/em&gt; p-value.
&lt;/p&gt;


&lt;h4&gt;R code&lt;/h4&gt;

&lt;script src=&quot;https://gist.github.com/chenpanliao/75ecb122e3eb814176c2.js&quot;&gt;&lt;/script&gt;


&lt;h4&gt;Arguments&lt;/h4&gt;

&lt;p&gt;The usage of this function &lt;code&gt;exactOneOrPairedSampleTest()&lt;/code&gt; is very similar to the R built-in function &lt;code&gt;t.test()&lt;/code&gt;. There are four arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;x1&lt;/code&gt;: a numeric vector specifying $x_{1i}$&lt;/li&gt;
&lt;li&gt;&lt;code&gt;x2 = NULL&lt;/code&gt;: a numeric vector specifying $x_{2i}$ (in paired-sample case)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mu = 0&lt;/code&gt;: a number specifying true mean or true difference $\mu$&lt;/li&gt;
&lt;li&gt;&lt;code&gt;alternative = c(&quot;t&quot;,&quot;g&quot;,&quot;l&quot;)&lt;/code&gt;a single character specifying $H_0$: true mean of $x_1$ or mean of $x_{1i} - x_{2i}$ is equal, greater or less than $\mu$, respectively.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Example 1: one-sample test&lt;/h4&gt;

&lt;p&gt;Let $x_i = \{43,67,64,64,51,53,53,26,36,48,34,48,6 \}$, $i=1 \ldots 13$:&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; x &amp;lt;- c(43,67,64,64,51,53,53,26,36,48,34,48,6)&lt;/pre&gt;

&lt;p&gt;Now we can call the above function &lt;code&gt;exactOneOrPairedSampleTest()&lt;/code&gt; to test $H_0$: true mean of $x_i = 56$ against $H_A$: true mean of $x_i \neq 56$:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; test1 &amp;lt;- exactOneOrPairedSampleTest(x, alternative=&quot;t&quot;, mu=56)
&amp;gt; test1

 Exact one sample test

Alternative hypothesis: true mean is not equal to 56 

mean(x) - mu = -10.3846153846154
Number of total permutation = 8192 
Number of rejected permutation = 364 
P-value = 0.04443359&lt;/pre&gt;

&lt;p&gt;The results show that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$\sum_{i=1}^{13} x_i / 13 - \mu = 45.61538 - 56 = -10.38462$;&lt;/li&gt;
&lt;li&gt;Totally $8192 = 2^{13}$ permutations are processed;&lt;/li&gt;
&lt;li&gt;364 permutations do not support $H_0$;&lt;/li&gt;
&lt;li&gt;$P = 364/2^{13} = 0.0444$.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can also call the function &lt;code&gt;hist.exactOneOrPairedSampleTest()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; hist(test1)&lt;/pre&gt;


&lt;!--
&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3s_zKlxO_KPhK8qkuibo-JHDepQBPkf5XG1eyXTGRLHC7EZN8bZwWWypNf7PStL7oVvgJFUWtQQ5ZpQELCZXeJkwxTGJwdtUVr6FET3gFp6N5f-uRSu8CDvEny44D6BlWEiSN8rWBUBo/s1600/Rplot1.png&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3s_zKlxO_KPhK8qkuibo-JHDepQBPkf5XG1eyXTGRLHC7EZN8bZwWWypNf7PStL7oVvgJFUWtQQ5ZpQELCZXeJkwxTGJwdtUVr6FET3gFp6N5f-uRSu8CDvEny44D6BlWEiSN8rWBUBo/s400/Rplot1.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
--&gt;

&lt;p&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3s_zKlxO_KPhK8qkuibo-JHDepQBPkf5XG1eyXTGRLHC7EZN8bZwWWypNf7PStL7oVvgJFUWtQQ5ZpQELCZXeJkwxTGJwdtUVr6FET3gFp6N5f-uRSu8CDvEny44D6BlWEiSN8rWBUBo/s1600/Rplot1.png&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3s_zKlxO_KPhK8qkuibo-JHDepQBPkf5XG1eyXTGRLHC7EZN8bZwWWypNf7PStL7oVvgJFUWtQQ5ZpQELCZXeJkwxTGJwdtUVr6FET3gFp6N5f-uRSu8CDvEny44D6BlWEiSN8rWBUBo/s1600/Rplot1.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;


&lt;p&gt;As you can see, the vertical lines $x=\pm 10.38462$ shows the critical boundary of rejecting $H_0$. Note that it is a two-tail test.&lt;/p&gt;

&lt;p&gt;Finally, you may call the returned object:&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; str(test1)
List of 14
 $ x1          : num [1:13] 43 67 64 64 51 53 53 26 36 48 ...
 $ x2          : NULL
 $ x1.name     : chr &quot;x&quot;
 $ x2.name     : chr &quot;NULL&quot;
 $ n           : int 13
 $ mu          : num 56
 $ test.0      : num -10.4
 $ is.onesample: logi TRUE
 $ alternative : chr &quot;t&quot;
 $ test.perm   : num [1:8192] -10.38 -8.38 -12.08 -10.08 -11.62 ...
 $ DF          :&#39;data.frame&#39;: 13 obs. of  3 variables:
  ..$ x1  : num [1:13] 43 67 64 64 51 53 53 26 36 48 ...
  ..$ mu  : num [1:13] 56 56 56 56 56 56 56 56 56 56 ...
  ..$ diff: num [1:13] -13 11 8 8 -5 -3 -3 -30 -20 -8 ...
 $ N           : num 8192
 $ p.value     : num 0.0444
 $ rejected.N  : int 364
 - attr(*, &quot;class&quot;)= chr &quot;exactOneOrPairedSampleTest&quot;&lt;/pre&gt;
&lt;p&gt;to get more details of the results if you need them.&lt;/p&gt;

&lt;h4&gt;Example 2: paired-sample test&lt;/h4&gt;

&lt;p&gt;Let $x_{1i} = \{92, 0,72,80,57,76,81,67,50,77,90\}$ and $x_{2i} = \{43,67,64,64,51,53,53,26,36,48,34\}$, where each $i$ is paired and $i=1 \ldots 11$:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; x1 &amp;lt;- c(92, 0,72,80,57,76,81,67,50,77,90)
&amp;gt; x2 &amp;lt;- c(43,67,64,64,51,53,53,26,36,48,34)&lt;/pre&gt;

&lt;p&gt;Now we can call the function &lt;code&gt;exactOneOrPairedSampleTest()&lt;/code&gt; to test $H_0$: true mean of $x_{1i} - x_{2i} \leq 10$ against $H_A$: true mean of $x_{1i} - x_{2i} &gt; 10$:&lt;/p&gt;


&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; test2 &amp;lt;- exactOneOrPairedSampleTest(x1, x2, alternative=&quot;g&quot;, mu=10)
&amp;gt; # equivalent to test2 &amp;lt;- exactOneOrPairedSampleTest(x1 - x2, alternative=&quot;g&quot;, mu=10)
&amp;gt; test2
 Exact paired sample test

Alternative hypothesis: means of x1 - x2 is greater than 10
mean((x1)-(x2)) - mu = 8.45454545454546
Number of total permutation = 2048 
Number of rejected permutation = 445 
P-value = 0.2172852 &lt;/pre&gt;

&lt;p&gt;The results show that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$\sum_{i=1}^{11} (x_{1i}-x_{2i}) / 11 - 10 = 8.45455$;&lt;/li&gt;
&lt;li&gt;Totally $2048 = 2^{11}$ permutations are processed;&lt;/li&gt;
&lt;li&gt;445 permutations do not support $H_0$;&lt;/li&gt;
&lt;li&gt;$P = 445/2^{11} = 0.2172852$.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can also call the function &lt;code&gt;hist.exactOneOrPairedSampleTest()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; hist(test2)&lt;/pre&gt;




&lt;p&gt;
&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj16YvC8NPQ2xi6Mm8VW1S1Jxzl9dSrsVaovSI9tuGF-An0RYVkQ_80qiR9ES89HSVZWqNZjUDLNVU1oDcJWfelKNsVdJZkf6vuFajGNKXzAy_M94k8QpWcJ-WZrMYJsC5iNnxbmGSAD-k/s1600/Rplot2.png&quot; /&gt;
&lt;/p&gt;

&lt;p&gt;As you can see, the vertical lines $x=8.45455$ shows the critical boundary of rejecting $H_0$. Note that this is a right-tail test.&lt;/p&gt;

&lt;p&gt;Again, you may call the returned object:&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; str(test2)
List of 14
 $ x1          : num [1:11] 92 0 72 80 57 76 81 67 50 77 ...
 $ x2          : num [1:11] 43 67 64 64 51 53 53 26 36 48 ...
 $ x1.name     : chr &quot;x1&quot;
 $ x2.name     : chr &quot;x2&quot;
 $ n           : int 11
 $ mu          : num 10
 $ test.0      : num 8.45
 $ is.onesample: logi FALSE
 $ alternative : chr &quot;g&quot;
 $ test.perm   : num [1:2048] 8.45 1.36 22.45 15.36 8.82 ...
 $ DF          :&#39;data.frame&#39;: 11 obs. of  3 variables:
  ..$ x1  : num [1:11] 92 0 72 80 57 76 81 67 50 77 ...
  ..$ x2  : num [1:11] 43 67 64 64 51 53 53 26 36 48 ...
  ..$ diff: num [1:11] 39 -77 -2 6 -4 13 18 31 4 19 ...
 $ N           : num 2048
 $ p.value     : num 0.217
 $ rejected.N  : int 445
 - attr(*, &quot;class&quot;)= chr &quot;exactOneOrPairedSampleTest&quot;&lt;/pre&gt;
&lt;p&gt;to get more details of the results if you need them.&lt;/p&gt;


</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/7223943328538126333/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2014/01/a-r-function-exact-onepaired-sample.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/7223943328538126333'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/7223943328538126333'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2014/01/a-r-function-exact-onepaired-sample.html' title='An R function: Exact one/paired sample test for mean(s)'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg3s_zKlxO_KPhK8qkuibo-JHDepQBPkf5XG1eyXTGRLHC7EZN8bZwWWypNf7PStL7oVvgJFUWtQQ5ZpQELCZXeJkwxTGJwdtUVr6FET3gFp6N5f-uRSu8CDvEny44D6BlWEiSN8rWBUBo/s72-c/Rplot1.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-6828708258736629700</id><published>2013-12-31T14:44:00.001-08:00</published><updated>2014-09-07T12:36:22.896-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="R-function"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>An R function for bootstrap confidence intervals</title><content type='html'>&lt;p class=&quot;abstract&quot;&gt;Here I introduce a R function made by myself to calculate several confidence intervals of mean/median including exact CI, basic bootstrap CI, percentile bootstrap CI &amp; studentized bootstrap CI.&lt;/p&gt;
&lt;h4&gt;R code&lt;/h4&gt;

&lt;script src=&quot;https://gist.github.com/chenpanliao/d93fcbaf5117dd53aaa8.js&quot;&gt;&lt;/script&gt;

&lt;!--
&lt;pre class=&quot;prettyprint&quot;&gt;#    Copyright 2013 Chen-Pan Liao &lt;andrew.43@gmail.com&gt;
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    any later version.
#    
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see &amp;lt;http://www.gnu.org/licenses/&amp;gt;.

## main function
bootCI &amp;lt;- function(
  x,
  alpha=0.05,
  alternative=c(&quot;t&quot;, &quot;l&quot;, &quot;g&quot;),   # see arg &quot;alternative&quot; in help(t.test)
  B=1999,                         # number of bootstrap samples
  quantileAlgorithm = 7           # passed to quantile(type)
){
  #validation
  if(mode(x) != &quot;numeric&quot;){
    stop(&quot;mode(x) must be \&quot;numeric\&quot;&quot;)
  }
  if(class(x) != &quot;numeric&quot; &amp;amp;&amp;amp; class(x) != &quot;integer&quot;){
    stop(&quot;class(x) must be \&quot;numeric\&quot; or \&quot;integer\&quot;&quot;)
  }
  if(length(x) &amp;lt; 2){
    stop(&quot;length(x) must be larger than 1&quot;)
  }
  
  # initiation
  alternative &amp;lt;- alternative[1]
  stat.length &amp;lt;- length(x)
  stat.mean &amp;lt;- mean(x)
  stat.sem &amp;lt;- sqrt(var(x) / stat.length)
    
  # alternative
  probs &amp;lt;- c(alpha/2, 1-alpha/2)
  if(alternative == &quot;l&quot;) probs &amp;lt;- c(0, 1-alpha)
  if(alternative == &quot;g&quot;) probs &amp;lt;- c(alpha, 1)
 
  # bootstrap
  boot.mat &amp;lt;- matrix(0.0, B+1, stat.length)
  boot.mat[1,] &amp;lt;- x
  for (i in 2:(B+1)) {
    boot.mat[i,] &amp;lt;- sample(x, replace=T)
  }

  # exact (traditional) CI based on t distribution
  CI.exact &amp;lt;- stat.mean + qt(probs, stat.length - 1) * stat.sem
  names(CI.exact) &amp;lt;- paste(probs*100, &quot;%&quot;, sep=&quot;&quot;)
  
  # basic bootstrap CI
  CI.basic &amp;lt;- 2 * stat.mean - quantile(
    apply(boot.mat, 1, mean), probs=1-probs, type=quantileAlgorithm
  )
  names(CI.basic) &amp;lt;- rev(names(CI.basic))

  # percentile bootstrap CI
  CI.percentile &amp;lt;- quantile(
    apply(boot.mat, 1, mean), probs=probs, type=quantileAlgorithm
  )

  # studentized bootstrap CI
  tmp.mean &amp;lt;- apply(boot.mat, 1, mean)
  tmp.sem &amp;lt;- apply(
    boot.mat, 1, function(.){sqrt(var(.) / length(.))}
  )
  tmp.t &amp;lt;- (tmp.mean - stat.mean) / tmp.sem
  expr &amp;lt;- expression(
    CI.studentized &amp;lt;- 
      stat.mean - quantile(
        tmp.t, probs=1-probs, type=quantileAlgorithm
      ) * stat.sem
  )
  a.try &amp;lt;- try(eval(expr), T)
  if(&quot;try-error&quot; %in% class(a.try)) {
    warning(&quot;studentized bootstrap CI cannot 
      be done well due to boostrap sem = 0&quot;)
  }
  names(CI.studentized) &amp;lt;- rev(names(CI.studentized))
  
  # exceptions of one-tail
  if(alternative == &quot;g&quot;) {
    CI.exact[2] = Inf
    CI.basic[2] = Inf
    CI.percentile[2] = Inf
    CI.studentized[2] = Inf
  } else if(alternative == &quot;l&quot;) {
    CI.exact[1] = -Inf
    CI.basic[1] = -Inf
    CI.percentile[1] = -Inf
    CI.studentized[1] = -Inf
  }
  
  output &amp;lt;- list(
    x = x,
    alpha = alpha,
    alternative = alternative,
    B = B,
    CI.exact = CI.exact,
    CI.basic = CI.basic, 
    CI.percentile = CI.percentile, 
    CI.studentized = CI.studentized
  )
  class(output) &amp;lt;- &quot;bootCI&quot;
  return(output)
}

## print function
print.bootCI &amp;lt;- function(w){
  cat(&quot;Summary of x\n&quot;)
  print(summary(w$x))
  cat(&quot;CIs of mu\n&quot;)
  mat &amp;lt;- 
    rbind(w$CI.exact, w$CI.basic, w$CI.percentile, w$CI.studentized)
  rownames(mat) &amp;lt;- 
    c(&quot;$CI.exact&quot;, &quot;$CI.basic&quot;, &quot;$CI.percentile&quot;, &quot;$CI.studentized&quot;)
  print(mat)
}&lt;/pre&gt;

--&gt;


&lt;h4&gt;Arguments&lt;/h4&gt;

&lt;p&gt;There are five arguments in this R function &lt;code&gt;bootCI()&lt;/code&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;x&lt;/code&gt; — a numeric vector specifying $x_i$&lt;/li&gt;
&lt;li&gt;&lt;code&gt;alpha = 0.05&lt;/code&gt; — a numeric specifying $\alpha$ value&lt;/li&gt;
&lt;li&gt;&lt;code&gt;alternative = c(&quot;t&quot;, &quot;l&quot;, &quot;g&quot;)&lt;/code&gt; — a single character string specifying two-sided, less or greater tail&lt;/li&gt;
&lt;li&gt;&lt;code&gt;B = 1999&lt;/code&gt; — a integer specifying number of bootstrap samples&lt;/li&gt;
&lt;li&gt;&lt;code&gt;quantileAlgorithm = 7&lt;/code&gt; — a integer specifying the argument &lt;code&gt;type&lt;/code&gt; passed to &lt;code&gt;quantile()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Examples&lt;/h4&gt;

&lt;p&gt;We can define a numeric vector $x = [5\, 2\, 3\, 6\, 8\, 19\, 1.5]$ including $N=7$ items as &lt;code&gt;num&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&amp;gt; num &amp;lt;- c(5, 2, 3, 6, 8, 19, 1.5)&lt;/pre&gt;

&lt;p&gt;After loading the above function, we can call the function to calculate CIs:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; bootCI(num)
Summary of x
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.500   2.500   5.000   6.357   7.000  19.000 
CIs of mu
                     2.5%     97.5%
$CI.exact       0.7778728 11.936413
$CI.basic       1.7125000  9.714286
$CI.percentile  3.0000000 11.001786
$CI.studentized 2.6785623 17.669412&lt;/pre&gt;

&lt;p&gt;The above results show that&lt;/p&gt;

\begin{aligned}
\text{exact CI} &amp; = ( \hat{\mu} + t_{\frac{\alpha}{2}} \cdot \hat{se}_{\hat{\mu}} \;,\; \hat{\mu} + t_{1-\frac{\alpha}{2}} \cdot \hat{se}_{\hat{\mu}} ) \\
\text{basic CI} &amp; = ( 2\hat{\mu} - \hat{\mu}_{1-\frac{\alpha}{2}}^{*}  \;,\; 2\hat{\mu} - \hat{\mu}_{\frac{\alpha}{2}}^{*} ) \\
\text{percentile CI} &amp; = ( \hat{\mu}_{\frac{\alpha}{2}}^* \;,\; \hat{\mu}_{1-\frac{\alpha}{2}}^* ) \\
\text{studentized CI} &amp; = ( \hat{\mu} + t^*_{\frac{\alpha}{2}} \cdot \hat{se}_{\hat{\mu}} \;,\; \hat{\mu} + t^*_{1-\frac{\alpha}{2}} \cdot \hat{se}_{\hat{\mu}} ) \\
\end{aligned}

&lt;p&gt;where $\hat{\mu}$ is mean of $x$, $t_{\frac{\alpha}{2}}$ is lower $\alpha/2$ critical value for the $t$ distribution given $\text{df} = N-1$, $\hat{se}_{\hat{\mu}}$ is the standard error of the mean of $x$, $\hat{\mu}_{\frac{\alpha}{2}}^*$ is $\alpha/2$ percentile of the mean of bootstrapped $x$, and $t^*_{\frac{\alpha}{2}} = \frac{\hat{\mu^*}-\hat{\mu}}{\hat{se}^*_{\hat{\mu^*}}}$ is t-value based on bootstrapped $x$. See&lt;/p&gt;

&lt;ul&gt;

&lt;li&gt;J. Carpenter and J. Bithell (2000). Bootstrap confidence intervals: when, which, what? A practical guide for medical statisticians. Statist. Med. 19:1141-1164&lt;/li&gt;

&lt;li&gt;Wikipedia contributors, &#39;Bootstrapping (statistics)&#39;,  &lt;i&gt;Wikipedia, The Free Encyclopedia,&lt;/i&gt; 17 December 2013, 21:28 UTC, &amp;lt;&lt;a class=&quot;external free&quot; href=&quot;//en.wikipedia.org/w/index.php?title=Bootstrapping_(statistics)&amp;amp;oldid=586549893&quot;&gt;http://en.wikipedia.org/w/index.php?title=Bootstrapping_(statistics)&amp;amp;oldid=586549893&lt;/a&gt;&amp;gt; [accessed 31 December 2013]&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;for more details.&lt;/p&gt;

&lt;p&gt;We can assign a different $\alpha$, direction or number of bootstrap samples:&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; bootCI(num, alpha=0.01)
Summary of x
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.500   2.500   5.000   6.357   7.000  19.000 
CIs of mu
                      0.5%    99.5%
$CI.exact       -2.0962642 14.81055
$CI.basic       -0.1428571 10.28571
$CI.percentile   2.4285714 12.85714
$CI.studentized  1.1394952 25.22030

&amp;gt; bootCI(num, alternative=&quot;g&quot;)
Summary of x
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.500   2.500   5.000   6.357   7.000  19.000 
CIs of mu
                      5% 100%
$CI.exact       1.926445  Inf
$CI.basic       2.714286  Inf
$CI.percentile  3.285714  Inf
$CI.studentized 3.332740  Inf

&amp;gt; bootCI(num, B=99)
Summary of x
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  1.500   2.500   5.000   6.357   7.000  19.000 
CIs of mu
                     2.5%     97.5%
$CI.exact       0.7778728 11.936413
$CI.basic       1.6053571  9.330357
$CI.percentile  3.3839286 11.108929
$CI.studentized 2.5532298 15.532786&lt;/pre&gt;

&lt;p&gt;The returned list may be helpful for someone:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; myCI &amp;lt;- bootCI(num)
&amp;gt; myCI$CI.percentile
    2.5%    97.5% 
 3.00000 10.85714 
&amp;gt; str(myCI)
List of 8
 $ x             : num [1:7] 5 2 3 6 8 19 1.5
 $ alpha         : num 0.05
 $ alternative   : chr &quot;t&quot;
 $ B             : num 1999
 $ CI.exact      : Named num [1:2] 0.778 11.936
  ..- attr(*, &quot;names&quot;)= chr [1:2] &quot;2.5%&quot; &quot;97.5%&quot;
 $ CI.basic      : Named num [1:2] 1.86 9.71
  ..- attr(*, &quot;names&quot;)= chr [1:2] &quot;2.5%&quot; &quot;97.5%&quot;
 $ CI.percentile : Named num [1:2] 3 10.9
  ..- attr(*, &quot;names&quot;)= chr [1:2] &quot;2.5%&quot; &quot;97.5%&quot;
 $ CI.studentized: Named num [1:2] 2.65 17.73
  ..- attr(*, &quot;names&quot;)= chr [1:2] &quot;2.5%&quot; &quot;97.5%&quot;
 - attr(*, &quot;class&quot;)= chr &quot;bootCI&quot;&lt;/pre&gt;

</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/6828708258736629700/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2013/12/an-r-function-for-bootstrap-confidence.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/6828708258736629700'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/6828708258736629700'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2013/12/an-r-function-for-bootstrap-confidence.html' title='An R function for bootstrap confidence intervals'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-6043535324868221270</id><published>2013-12-14T19:34:00.001-08:00</published><updated>2014-01-02T09:24:30.022-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Mixed model"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><title type='text'>Paired t-test vs general mixed model with R</title><content type='html'>&lt;h4&gt;Dataset&lt;/h4&gt;

&lt;p&gt;Consider the following dataset:&lt;p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; x1 &amp;lt;- c(3,4,3,5,6,6,6,7,7,6,8,9,11)
&amp;gt; x2 &amp;lt;- c(3,4,5,5,8,6,8,7,9,8,8,9,14)
&amp;gt; cbind(x1,x2)
      x1 x2
 [1,]  3  3
 [2,]  4  4
 [3,]  3  5
 [4,]  5  5
 [5,]  6  8
 [6,]  6  6
 [7,]  6  8
 [8,]  7  7
 [9,]  7  9
[10,]  6  8
[11,]  8  8
[12,]  9  9
[13,] 11 14&lt;/pre&gt;

&lt;p&gt;where &lt;code&gt;x1&lt;/code&gt; and &lt;code&gt;x2&lt;/code&gt; are paired sample.
&lt;/p&gt;

&lt;h4&gt;Paired &lt;em&gt;t&lt;/em&gt;-test &lt;/h4&gt;

&lt;p&gt;The process of a typical paired &lt;em&gt;t&lt;/em&gt;-test with R is following:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; t.test(x1, x2, paired=T)

 Paired t-test

data:  x1 and x2
t = -3.1225, df = 12, p-value = 0.008814
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -1.6977786 -0.3022214
sample estimates:
mean of the differences 
                     -1&lt;/pre&gt;

&lt;p&gt;The results show that 1) average of $x_1 - x_2$ equals to -1; 2) $t_{12} = -3.1225$, $p = 0.008814$.&lt;/p&gt;


&lt;h4&gt;Mixed model for paired sample&lt;/h4&gt;

&lt;p&gt;We can build a mixed model for this paired-sample example. Consider the following dataset:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; y &amp;lt;- c(x1,x2)
&amp;gt; trt &amp;lt;- gl(2,13,26)
&amp;gt; subject &amp;lt;- gl(13,1,26)
&amp;gt; cbind(y, trt, subject)
       y trt subject
 [1,]  3   1       1
 [2,]  4   1       2
 [3,]  3   1       3
 [4,]  5   1       4
 [5,]  6   1       5
 [6,]  6   1       6
 [7,]  6   1       7
 [8,]  7   1       8
 [9,]  7   1       9
[10,]  6   1      10
[11,]  8   1      11
[12,]  9   1      12
[13,] 11   1      13
[14,]  3   2       1
[15,]  4   2       2
[16,]  5   2       3
[17,]  5   2       4
[18,]  8   2       5
[19,]  6   2       6
[20,]  8   2       7
[21,]  7   2       8
[22,]  9   2       9
[23,]  8   2      10
[24,]  8   2      11
[25,]  9   2      12
[26,] 14   2      13&lt;/pre&gt;

&lt;p&gt;
where &lt;code&gt;y&lt;/code&gt; is dependent variable, &lt;code&gt;trt&lt;/code&gt; is a two-level fixed factor, and &lt;code&gt;subject&lt;/code&gt; is experimental units considered as blocks (random factor). A mixed model can fit this dataset:&lt;/p&gt;

\[
y_{ij} = \tau_i + b_{j} + \varepsilon_{ij}
\]

&lt;p&gt;
where $i=\{1,2\}$ showing two-level in fixed factor $\tau$, $j=\{1,2,\ldots , 13\}$ showing 13 subjects as a random factor $b$.
&lt;/p&gt;

&lt;p&gt;We can find the solution of this mixed model by using &lt;code&gt;lme4:lmer()&lt;/code&gt;:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; require(lme4)
&amp;gt; mod &amp;lt;- lmer(y ~ trt + (1|subject) )
&amp;gt; summary(mod)
Linear mixed model fit by REML [&#39;lmerMod&#39;]
Formula: y ~ trt + (1 | subject) 

REML criterion at convergence: 98.5708 

Random effects:
 Groups   Name        Variance Std.Dev.
 subject  (Intercept) 5.8590   2.4205  
 Residual             0.6667   0.8165  
Number of obs: 26, groups: subject, 13

Fixed effects:
            Estimate Std. Error t value
(Intercept)   6.2308     0.7085   8.794
trt2          1.0000     0.3203   3.123

Correlation of Fixed Effects:
     (Intr)
trt2 -0.226&lt;/pre&gt;

&lt;p&gt;
As you can see, the estimate of &lt;code&gt;trt2&lt;/code&gt; equals 1 showing the average of $x_2 - x_1$; the statistic $t=3.123$ is equivalent to the &lt;em&gt;t&lt;/em&gt;-value I get in paired &lt;em&gt;t&lt;/em&gt;-test.&lt;/p&gt;

&lt;p&gt;We can find the &lt;em&gt;p&lt;/em&gt;-value by using &lt;code&gt;car:Anova()&lt;/code&gt;:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; require(car)
&amp;gt; Anova(mod, type=3, test.statistic=&quot;F&quot;)
Analysis of Deviance Table (Type III Wald F tests with Kenward-Roger df)

Response: y
                F Df Df.res    Pr(&amp;gt;F)    
(Intercept) 77.34  1 13.288 6.636e-07 ***
trt          9.75  1 12.000  0.008814 ** 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1&lt;/pre&gt;

&lt;p&gt;and then we can find that $F_{1,12} = 9.75 = 3.123^2$ and $p=0.008814$. These outcomes are all equivalent to what I got in paired &lt;em&gt;t&lt;/em&gt;-test.&lt;/p&gt;

</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/6043535324868221270/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2013/12/paired-t-test-vs-general-mixed-model.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/6043535324868221270'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/6043535324868221270'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2013/12/paired-t-test-vs-general-mixed-model.html' title='Paired t-test vs general mixed model with R'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-347114260243037188</id><published>2013-11-01T06:40:00.001-07:00</published><updated>2014-01-02T09:36:10.389-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Generalized regression"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>Multiple comparisons for generalized linear model in R</title><content type='html'>&lt;h4&gt;An example&lt;/h4&gt;

&lt;H5&gt;Defining y and x&lt;/h5&gt;


&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; y &amp;lt;- c (rpois(10,1), rpois(10,2), rpois(10,3), rpois(10,5))
&amp;gt; y
 [1] 0 1 0 2 1 2 0 1 1 0 3 2 2 4 2 3 3 1 1 1 3 0 4 4 2 3 1 4 3 6 3 8 6 3 2 6 5 5 2 8
&amp;gt; x &amp;lt;- gl(4,10)
&amp;gt; x
 [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4
Levels: 1 2 3 4&lt;/pre&gt;

&lt;h5&gt;Fitting a Poisson regression&lt;/h5&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; f &amp;lt;- glm(y~x, family=poisson)
&amp;gt; summary(f)

Call:
glm(formula = y ~ x, family = poisson)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-2.44949  -0.90724   0.04533   0.52699   1.52242  

Coefficients:
            Estimate Std. Error z value Pr(&amp;gt;|z|)    
(Intercept)  -0.2231     0.3536  -0.631 0.527945    
x2            1.0116     0.4129   2.450 0.014277 *  
x3            1.3218     0.3979   3.322 0.000895 ***
x4            1.7918     0.3819   4.692 2.71e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 67.967  on 39  degrees of freedom
Residual deviance: 34.884  on 36  degrees of freedom
AIC: 142.6

Number of Fisher Scoring iterations: 5&lt;/pre&gt;


&lt;h5&gt;Multiple comparisons&lt;/h5&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; # install.packages(&quot;multcomp&quot;)
&amp;gt; require(multcomp)
&amp;gt; f.mc &amp;lt;- glht(f, linfct = mcp(x = &quot;Tukey&quot;))
&amp;gt; summary(f.mc)

  Simultaneous Tests for General Linear Hypotheses

Multiple Comparisons of Means: Tukey Contrasts


Fit: glm(formula = y ~ x, family = poisson)

Linear Hypotheses:
           Estimate Std. Error z value Pr(&amp;gt;|z|)    
2 - 1 == 0   1.0116     0.4129   2.450  0.06433 .  
3 - 1 == 0   1.3218     0.3979   3.322  0.00481 ** 
4 - 1 == 0   1.7918     0.3819   4.692  &amp;lt; 0.001 ***
3 - 2 == 0   0.3102     0.2807   1.105  0.67729    
4 - 2 == 0   0.7802     0.2575   3.030  0.01198 *  
4 - 3 == 0   0.4700     0.2327   2.019  0.17303    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Adjusted p values reported -- single-step method)

&amp;gt; confint(f.mc)

  Simultaneous Confidence Intervals

Multiple Comparisons of Means: Tukey Contrasts


Fit: glm(formula = y ~ x, family = poisson)

Quantile = 2.5471
95% family-wise confidence level
 

Linear Hypotheses:
           Estimate lwr      upr     
2 - 1 == 0  1.01160 -0.04002  2.06322
3 - 1 == 0  1.32176  0.30822  2.33529
4 - 1 == 0  1.79176  0.81905  2.76447
3 - 2 == 0  0.31015 -0.40481  1.02512
4 - 2 == 0  0.78016  0.12436  1.43596
4 - 3 == 0  0.47000 -0.12281  1.06282

&amp;gt; coef(f.mc)
    2 - 1     3 - 1     4 - 1     3 - 2     4 - 2     4 - 3 
1.0116009 1.3217558 1.7917595 0.3101549 0.7801586 0.4700036 
&amp;gt; vcov(f.mc)
              2 - 1         3 - 1         4 - 1         3 - 2         4 - 2         4 - 3
2 - 1  1.704542e-01  1.249996e-01  1.249996e-01 -4.545455e-02 -4.545455e-02  9.714451e-17
3 - 1  1.249996e-01  1.583330e-01  1.249996e-01  3.333333e-02  6.938894e-17 -3.333333e-02
4 - 1  1.249996e-01  1.249996e-01  1.458330e-01 -2.775558e-17  2.083333e-02  2.083333e-02
3 - 2 -4.545455e-02  3.333333e-02 -2.775558e-17  7.878788e-02  4.545455e-02 -3.333333e-02
4 - 2 -4.545455e-02  6.938894e-17  2.083333e-02  4.545455e-02  6.628788e-02  2.083333e-02
4 - 3  9.714451e-17 -3.333333e-02  2.083333e-02 -3.333333e-02  2.083333e-02  5.416667e-02&lt;/pre&gt;

&lt;h4&gt;Read more&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.stat.wmich.edu/wang/664/egs/Rmice.html&quot;&gt;&lt;em&gt;Simultaneous Inferences in R — Using package:multcomp&lt;/em&gt; by J.&amp;nbsp;C.&amp;nbsp;Wang&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://cran.r-project.org/web/packages/multcomp/vignettes/generalsiminf.pdf&quot;&gt;&lt;em&gt;Simultaneous Inference in General Parametric Models&lt;/em&gt; by T.&amp;nbsp;Hothorn et al.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/347114260243037188/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2013/11/multiple-comparisons-for-generalized.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/347114260243037188'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/347114260243037188'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2013/11/multiple-comparisons-for-generalized.html' title='Multiple comparisons for generalized linear model in R'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-2811073230125360212</id><published>2013-08-07T10:39:00.000-07:00</published><updated>2014-01-13T19:10:29.438-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Generalized regression"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>在 R 中配置 Generalized Logit Model</title><content type='html'>&lt;div lang=&quot;zh-Hant&quot;&gt;

&lt;p&gt;已知資料 &lt;code&gt;data.csv&lt;/code&gt; 如下: &lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;treat, r.a, r.b, r.c
   t1,  10,  20,   5
   t2,  11,  22,   5
   t3,  21,  40,  11
   t4,  21,   5,  10&lt;/pre&gt;

&lt;p&gt;其中 &lt;code&gt;treat&lt;/code&gt; 為具有 4 個水準的固定因子; &lt;code&gt;r.a&lt;/code&gt;, &lt;code&gt;r.b&lt;/code&gt; 及 &lt;code&gt;r.c&lt;/code&gt; 分別為 3 個類型的反應類別, 其下為頻率.&lt;/p&gt;

&lt;p&gt;以下為 R code:&lt;/p&gt;


&lt;pre class=&quot;prettyprint&quot;&gt;# 系統第一次運作請先安裝 package nnet
install.packages(&quot;nnet&quot;)

# 引入 package nnet
require(nnet)

# 讀取資料
dat &amp;lt;- read.csv(&quot;data.csv&quot;)

# 建立 generalized logit model
m &amp;lt;- multinom(
  cbind(r.a, r.b, r.c) ~ treat,          # 其中 r.a 寫在最前面即可作為比較基準
  data=dat,
  contrasts=list(treat=contr.treatment), # 其中採用 treatment contrast 方便解釋
  Hess=T                                 # 其中採用 Hessian Matrix 求標準誤
)

# 係數與標準誤
(s.m &amp;lt;- summary(m))

# odds ratio
exp(s.m$coefficients)

# chi-square
(chisq &amp;lt;- (s.m$coefficients / s.m$standard.error)^2)

# p-value
pchisq(chisq, df=1, lower.tail=F)&lt;/pre&gt;

&lt;p&gt;可得到以下結果: &lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; (s.m &amp;lt;- summary(m))
Call:
multinom(formula = cbind(r.a, r.b, r.c) ~ treat, data = dat, 
    contrasts = list(treat = contr.treatment), Hess = T)

Coefficients:
    (Intercept)        treat2      treat3      treat4
r.b   0.6931308  2.158365e-05 -0.04877868 &amp;lt;strong&amp;gt;-2.12818025&amp;lt;/strong&amp;gt;
r.c  -0.6931750 -9.525513e-02  0.04656602 -0.04876199

Std. Errors:
    (Intercept)    treat2    treat3    treat4
r.b   0.3872968 0.5351291 0.4718241 0.6305649
r.c   0.5477240 0.7687048 0.6622145 0.6690451

Residual Deviance: 346.6049 
AIC: 362.6049 


&amp;gt; # odds ratio
&amp;gt; exp(s.m$coefficients)
    (Intercept)   treat2    treat3    treat4
r.b   1.9999671 1.000022 0.9523919 &amp;lt;strong&amp;gt;0.1190537&amp;lt;/strong&amp;gt;
r.c   0.4999861 0.909141 1.0476672 0.9524078


&amp;gt; # chi-square
&amp;gt; (chisq &amp;lt;- (s.m$coefficients / s.m$standard.error)^2)
    (Intercept)       treat2      treat3       treat4
r.b    3.202894 1.626795e-09 0.010688081 &amp;lt;strong&amp;gt;11.390878935&amp;lt;/strong&amp;gt;
r.c    1.601630 1.535527e-02 0.004944712  0.005311928


&amp;gt; # p-value
&amp;gt; pchisq(chisq, df=1, lower.tail=F)
    (Intercept)    treat2    treat3      treat4
r.b  0.07350811 0.9999678 0.9176589 &amp;lt;strong&amp;gt;0.000738056&amp;lt;/strong&amp;gt;
r.c  0.20567234 0.9013815 0.9439400 0.941899243&lt;/pre&gt;

&lt;p&gt;可以發現, 唯一達顯著之係數為 &lt;code&gt;r.b&lt;/code&gt; 對比 &lt;code&gt;r.a&lt;/code&gt; 時在 &lt;code&gt;treat4&lt;/code&gt; 對比 &lt;code&gt;treat1&lt;/code&gt; 的情況, 其係數 $\beta = -2.12818025$, 勝算比 $= \exp(-2.12818025) = 0.1190537$, 檢定量 $\chi^2_1 = 11.390878935$, $p = 0.000738056$, 顯示 &lt;code&gt;treat4&lt;/code&gt; 比 &lt;code&gt;treat1&lt;/code&gt; 的 &lt;code&gt;r.b / r.a&lt;/code&gt; 達顯著差異. 由實際資料 $\frac{5/21}{20/10} = 0.1190476$ 也和模型所預測的勝算比 0.1190537 相當接近.&lt;/p&gt;

&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/2811073230125360212/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2013/08/r-generalized-logit-model.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/2811073230125360212'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/2811073230125360212'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2013/08/r-generalized-logit-model.html' title='在 R 中配置 Generalized Logit Model'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-7621837832951775406</id><published>2013-06-05T20:41:00.002-07:00</published><updated>2014-10-17T12:48:00.438-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="R-function"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>G-test for 2-way contingency data in R</title><content type='html'>&lt;p class=&quot;abstract&quot;&gt;A R function applying &lt;em&gt;G&lt;/em&gt;-test for 2-way contingency data in R is demonstrated. Yates’ correction and Williams’ correction are also included in the output.&lt;/p&gt;

&lt;h4&gt;R Source&lt;/h4&gt;


&lt;script src=&quot;https://gist.github.com/chenpanliao/90a3062b5e54347768d3.js&quot;&gt;&lt;/script&gt;

&lt;!-- 
&lt;pre class=&quot;prettyprint&quot;&gt;#    Copyright 2013 Chen-Pan Liao &lt;andrew.43@gmail.com&gt;
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    any later version.
#    
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see &amp;lt;http://www.gnu.org/licenses/&amp;gt;.

g.test.2way &lt;- function(
  x # must be a 2-way xtabs/matrix
)
{
  ## initiating
  data.o &amp;lt;- x
  sum.total &amp;lt;- sum(x)
  sum.row &amp;lt;- rowSums(x)
  sum.col &amp;lt;- colSums(x)
  n.row &amp;lt;- dim(x)[1]
  n.col &amp;lt;- dim(x)[2]
  chisq.df &amp;lt;- (n.col - 1) * (n.row - 1)
  
  ## check
  if(n.row == 1) {stop(&quot;Numbers of row must be larger than 1&quot;)}
  if(n.col == 1) {stop(&quot;Numbers of column must be larger than 1&quot;)}
  if(mode(data.o) != &quot;numeric&quot;) {stop(&quot;Mode of x must be numeric&quot;)}
  if(
    sum(class(data.o) != c(&quot;matrix&quot;, &quot;xtabs&quot;)) == 0
  ) {
    stop(&quot;Class of x must be matrix or xtabs&quot;)
  }
  
  ## matrix of expected value
  data.e &amp;lt;- matrix(NA, ncol = n.col, nrow = n.row)
  for (m in 1:n.row){
    for (n in 1:n.col){
      data.e[m,n] &amp;lt;- sum.col[n] * sum.row[m] / sum.total
    }
  }
  
  ## matrix of ovserved value with Yates&#39; correction
  data.o.y &amp;lt;- matrix(NA, ncol = n.col, nrow = n.row)
  for (m in 1:n.row){
    for (n in 1:n.col){
      if (data.e[m,n] - data.o[m,n] &amp;gt; 0){
        data.o.y[m,n] &amp;lt;- data.o[m,n] + 0.5
      } else if (data.e[m,n] - data.o[m,n] &amp;lt; 0){
        data.o.y[m,n] &amp;lt;- data.o[m,n] - 0.5
      } else {
        data.o.y[m,n] &amp;lt;- data.o[m,n]
      }
    }
  }

  ## q_min for Williams&#39; correction
  q.min &amp;lt;- 1 + 
           (sum.total * sum(1 / sum.col) - 1) *
           (sum.total * sum(1 / sum.row) - 1) /
           (6 * (chisq.df) * sum.total)


  ## G-value
  isnot.na.index &amp;lt;- !is.na(data.o * log(data.o / data.e))
  g &amp;lt;- 2 * sum((data.o * log(data.o / data.e))[isnot.na.index])
  isnot.na.index.y &amp;lt;- !is.na(data.o.y * log(data.o.y / data.e))
  g.y &amp;lt;- 2 * sum((data.o.y * log(data.o.y / data.e))[isnot.na.index])
  g.w &amp;lt;- g / q.min
  
  ## P-value
  p.g &amp;lt;- 1 - pchisq(g, chisq.df)
  p.g.y &amp;lt;- 1 - pchisq(g.y, chisq.df)
  p.g.w &amp;lt;- 1 - pchisq(g.w, chisq.df)


  ## return
  z &amp;lt;- list(
    data.observed = data.o,
    data.expected = data.e,
    data.observed.Yates = data.o.y,
    df = chisq.df,
    q.min = q.min,
    g = g,
    g.Yates = g.y,
    g.Williams = g.w,
    p = p.g,
    p.Yates = p.g.y,
    p.Williams = p.g.w
  )
  class(z) &amp;lt;- &quot;g.test.2way&quot;
  return(z)
}

## print function
print.g.test.2way &amp;lt;- function(x){
  cat(&quot;Observed value:\n&quot;)
  print(x$data.observed)
  cat(&quot;Expected value:\n&quot;)
  print(x$data.expected)
  cat(&quot;Observed value with Yates&#39; correction:\n&quot;)
  print(x$data.observed.Yates)
  cat(&quot;Degree of freedom =&quot;, x$df, &quot;\n&quot;)
  cat(sprintf(&quot;G-test:\n\tG = %g,\tp = %g\n&quot;, x$g, x$p))
  cat(sprintf(
    &quot;G-test with Yates&#39; correction:\n\tG = %g,\tp = %g\n&quot;,
    x$g.Yates, x$p.Yates
  ))
  cat(sprintf(
    &quot;G-test with Williams&#39; correction:\n\tG = %g,\tq_min = %g,\tp = %g\n&quot;,
    x$g.Williams, x$q.min, x$p.Williams
  ))
}&lt;/pre&gt;
--&gt;


&lt;h4&gt;Example&lt;/h4&gt;
&lt;h5&gt;R code&lt;/h5&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;my.2way.contingency.table &amp;lt;- matrix(
  c(
    5,0,3,12,6,
    4,2,7,23,11
  ),
  nrow = 2, ncol=5, byrow=T
)

g.test.result &amp;lt;- g.test.2way(my.2way.contingency.table)
g.test.result
str(g.test.result)&lt;/pre&gt;

&lt;h5&gt;R output&lt;/h5&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; my.2way.contingency.table &amp;lt;- matrix(
+   c(
+     5,0,3,12,6,
+     4,2,7,23,11
+   ),
+   nrow = 2, ncol=5, byrow=T
+ )
&amp;gt; 
&amp;gt; g.test.result &amp;lt;- g.test.2way(my.2way.contingency.table)
&amp;gt; g.test.result
Observed value:
     [,1] [,2] [,3] [,4] [,5]
[1,]    5    0    3   12    6
[2,]    4    2    7   23   11
Expected value:
         [,1]      [,2]     [,3]     [,4]      [,5]
[1,] 3.205479 0.7123288 3.561644 12.46575  6.054795
[2,] 5.794521 1.2876712 6.438356 22.53425 10.945205
Observed value with Yates&#39; correction:
     [,1] [,2] [,3] [,4] [,5]
[1,]  4.5  0.5  3.5 12.5  6.5
[2,]  4.5  1.5  6.5 22.5 10.5
Degree of freedom = 4 
G-test:
 G = 3.41127, p = 0.491497
G-test with Yates&#39; correction:
 G = 1.28744, p = 0.863503
G-test with Williams&#39; correction:
 G = 3.07349, q_min = 1.1099, p = 0.545603
&amp;gt; str(g.test.result)
List of 11
 $ data.observed      : num [1:2, 1:5] 5 4 0 2 3 7 12 23 6 11
 $ data.expected      : num [1:2, 1:5] 3.205 5.795 0.712 1.288 3.562 ...
 $ data.observed.Yates: num [1:2, 1:5] 4.5 4.5 0.5 1.5 3.5 6.5 12.5 22.5 6.5 10.5
 $ df                 : num 4
 $ q.min              : num 1.11
 $ g                  : num 3.41
 $ g.Yates            : num 1.29
 $ g.Williams         : num 3.07
 $ p                  : num 0.491
 $ p.Yates            : num 0.864
 $ p.Williams         : num 0.546
 - attr(*, &quot;class&quot;)= chr &quot;g.test.2way&quot;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/7621837832951775406/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2013/06/g-test-for-2-way-contingency-data-in-r.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/7621837832951775406'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/7621837832951775406'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2013/06/g-test-for-2-way-contingency-data-in-r.html' title='G-test for 2-way contingency data in R'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-2699794724458502021</id><published>2013-03-12T10:37:00.000-07:00</published><updated>2014-10-17T12:56:43.833-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="R-function"/><title type='text'>Convert taxa matrix to guild matrix with R</title><content type='html'>&lt;p class=&quot;abstract&quot;&gt;在 R 中如何把分類群資料小計成為功能群資料? 可利用以下介紹的 R function taxaToGuild() 將一分類群資料檔及一分類群對應功能群資料檔轉化出一個功能群資料框. 一般來說, 該 function 也提供一個資料框資料以系統化的方式進行欄小計.&lt;/p&gt;







&lt;h4&gt;taxaToGuild.R 原始檔&lt;/h4&gt;


&lt;script src=&quot;https://gist.github.com/chenpanliao/db0aa37e2b24863eef85.js&quot;&gt;&lt;/script&gt;

&lt;!--
&lt;pre class=&quot;prettyprint&quot;&gt;#    Copyright 2013 Chen-Pan Liao &lt;andrew.43@gmail.com&gt;
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    any later version.
#    
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see &amp;lt;http://www.gnu.org/licenses/&amp;gt;.


############################################
## taxaToGuild.r
## Author: Chen-Pan Liao (2013)
## License: Public domain
## Environment: R (ver. 2.15+)
## Usage:
##  new.dataframe &amp;lt;- taxaToGuild(
##    taxa.file.csv,
##    taxa.file.csv.row.num,
##    guild.file.csv,
##    guild.file.csv.row.num,
##    guild.system.row.num
##  )
############################################



taxaToGuild &amp;lt;- function (

  taxa.file.csv,
  taxa.file.csv.row.num,
  guild.file.csv,
  guild.file.csv.row.num,
  guild.system.row.num


#  taxa.file.csv = &quot;taxa.csv&quot;,
#  taxa.file.csv.row.num = 1,
#  guild.file.csv = &quot;guild.csv&quot;,
#  guild.file.csv.row.num = 1,
#  guild.system.row.num = 1


){

  guild.data &amp;lt;- read.csv(guild.file.csv, row.names = guild.file.csv.row.num)
  taxa.data &amp;lt;- read.csv(&quot;taxa.csv&quot;, row.names = taxa.file.csv.row.num)
  
  guild.list.taxa &amp;lt;- row.names(guild.data)
  guild.list.guild &amp;lt;- guild.data[, guild.system.row.num]
  taxa.list &amp;lt;- names(taxa.data)
  
  taxa.list.ind &amp;lt;- numeric(length(taxa.list))
  taxa.unknown &amp;lt;- 0
  for(n in 1:length(taxa.list)){
    if (sum(taxa.list[n] == guild.list.taxa) &amp;gt; 0) {
      taxa.list.ind[n] &amp;lt;- which(taxa.list[n] == guild.list.taxa)
    } else {
      taxa.list.ind[n] &amp;lt;- NA
      taxa.unknown[length(taxa.unknown) + 1] &amp;lt;- n
    }
  }
  
  new.guild.list &amp;lt;- unique(guild.list.guild[taxa.list.ind])
  taxa.unknown &amp;lt;- taxa.unknown[-1]
  
  #new.taxa.data &amp;lt;- taxa.data
  #names(new.taxa.data) &amp;lt;- guild.list.guild[taxa.list.ind]
  
  
  ## new taxa matrix initiation 
  newer.taxa.data &amp;lt;- data.frame (
    matrix(
      0,
      nrow = nrow(taxa.data),
      ncol = length(new.guild.list)
    ),
    row.names = row.names(taxa.data)
  )
  names(newer.taxa.data) &amp;lt;- new.guild.list
  
  
  ## subsum of new taxa matrix
  for(n in 1:length(new.guild.list)){
    if (!is.na(new.guild.list[n])) {
      newer.taxa.data[,n] &amp;lt;- rowSums(
        data.frame(
          taxa.data[
            ,
            which(new.guild.list[n] == guild.list.guild[taxa.list.ind])
          ]
        )
      )
    } else {
      newer.taxa.data[,n] &amp;lt;- rowSums(
        data.frame(
          taxa.data[ , taxa.unknown]
        )
      )
    }
  }
  cat(
    sprintf(
      &quot;The guild system is %s.\n&quot;,
      names(guild.data)[guild.system.row.num]
    )
  )
  cat(
    sprintf(
      &quot;Warning: the following taxa is merged into guild NA due to no reference: %s.\n&quot;,
      taxa.list[taxa.unknown]
    )
  )
  
  return(newer.taxa.data)

}&lt;/pre&gt;
--&gt;



&lt;h4&gt;例子&lt;/h4&gt;
&lt;p&gt;假設有一蜘蛛以科為分類群的物種數資料，以 CSV 檔儲存後如下 (taxa.csv):&lt;/p&gt;

&lt;pre&gt;&quot;Plot&quot;,&quot;Leptonetidae&quot;,&quot;Clubionidae&quot;,&quot;Others&quot;,&quot;Araneidae&quot;,&quot;Oonopidae&quot;,&quot;Ctenizidae&quot;
1,2,0,0,1,1,2
2,1,2,3,1,0,0
3,1,0,1,1,1,1
4,2,3,2,1,0,1
5,0,1,1,2,1,4
6,2,0,0,0,1,3
7,1,0,1,2,2,1
8,0,0,1,2,3,1&lt;/pre&gt;

&lt;p&gt;其中第一列皆為變數名且第一欄為可辦視的樣點名。另有一科名對照功能群的資料檔以 CSV 檔儲存後如下 (guild.csv):&lt;/p&gt;

&lt;pre&gt;&quot;taxa&quot;,&quot;Guild1&quot;,&quot;Guild2&quot;,&quot;Guild3&quot;
&quot;Oonopidae&quot;,&quot;Ground runner&quot;,&quot;runner&quot;,&quot;Ground&quot;
&quot;Araneidae&quot;,&quot;Orb weaver&quot;,&quot;weaver&quot;,&quot;Space&quot;
&quot;Clubionidae&quot;,&quot;Foliage runner&quot;,&quot;runner&quot;,&quot;Foliage&quot;
&quot;Ctenidae&quot;,&quot;Ground runner&quot;,&quot;runner&quot;,&quot;Ground&quot;
&quot;Ctenizidae&quot;,&quot;Burrow dweller&quot;,&quot;dweller&quot;,&quot;Burrow&quot;
&quot;Gnaphosidae&quot;,&quot;Ground runner&quot;,&quot;runner&quot;,&quot;Ground&quot;
&quot;Leptonetidae&quot;,&quot;Ground weaver&quot;,&quot;weaver&quot;,&quot;Ground&quot;
&quot;Linyphiidae&quot;,&quot;Space weaver&quot;,&quot;weaver&quot;,&quot;Space&quot;&lt;/pre&gt;

&lt;p&gt;其中第一列皆為變數名且第一欄為可辦視的科名, 第 2 至 4 欄為三套不同的功能群系統. 在將 taxa.csv, guild.csv, taxaToGuild.r 三檔案置於相同某路徑後，於 R 環境中以 &lt;code&gt;setwd(&quot;某路徑&quot;)&lt;/code&gt; 後進行以下動作.&lt;/p&gt;


&lt;pre class=&quot;prettyprint&quot;&gt;source(&quot;taxaToGuild.r&quot;)
new.dataframe &amp;lt;- taxaToGuild(
  taxa.file.csv = &quot;taxa.csv&quot;,
  taxa.file.csv.row.num = 1,
  guild.file.csv = &quot;guild.csv&quot;,
  guild.file.csv.row.num = 1,
  guild.system.row.num = 1
)
new.dataframe&lt;/pre&gt;

&lt;p&gt;其中 &lt;code&gt;taxa.file.csv = &quot;taxa.csv&quot;&lt;/code&gt; 表示引入 taxa.csv 為分類群資料, &lt;code&gt;taxa.file.csv.row.num = 1&lt;/code&gt; 表示 taxa.csv 的第一欄為列名, &lt;code&gt;guild.file.csv = &quot;guild.csv&quot;&lt;/code&gt; 表示引入 guild.csv 為功能群對照檔, &lt;code&gt;guild.file.csv.row.num = 1&lt;/code&gt; 表示 guild.csv 的第一欄為列名, &lt;code&gt;guild.system.row.num = 1&lt;/code&gt; 表示以 guild.csv 的第 1 組對照表 (實際上是此例的第 2 欄). 新產生的功能群資料框賦予變數 &lt;code&gt;new.dataframe&lt;/code&gt;. 操作可得以下結果.&lt;/p&gt;



&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; source(&quot;taxaToGuild.r&quot;)
&amp;gt; new.dataframe &amp;lt;- taxaToGuild(
+   taxa.file.csv = &quot;taxa.csv&quot;,
+   taxa.file.csv.row.num = 1,
+   guild.file.csv = &quot;guild.csv&quot;,
+   guild.file.csv.row.num = 1,
+   guild.system.row.num = 1
+ )
The guild system is Guild1.
Warning: the following taxa is merged into guild NA due to no reference: Others.
&amp;gt; new.dataframe
  Ground weaver Foliage runner NA Orb weaver Ground runner Burrow dweller
1             2              0  0          1             1              2
2             1              2  3          1             0              0
3             1              0  1          1             1              1
4             2              3  2          1             0              1
5             0              1  1          2             1              4
6             2              0  0          0             1              3
7             1              0  1          2             2              1
8             0              0  1          2             3              1&lt;/pre&gt;

&lt;p&gt;值得注意的是, 因為 &quot;Others&quot; 無法藉由 &quot;Guild1&quot; 這項對應表成功對應至某個功能群, 所以被丟棄至 new.dataframe 中的 &quot;NA&quot; 欄. 在本例中若欲刪去 new.dataframe 中的 &quot;NA&quot; 欄, 可利用 &lt;code&gt;new.dataframe[,-3]&lt;/code&gt;.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/2699794724458502021/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2013/03/convert-taxa-matrix-to-guild-matrix.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/2699794724458502021'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/2699794724458502021'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2013/03/convert-taxa-matrix-to-guild-matrix.html' title='Convert taxa matrix to guild matrix with R'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-3144901992655818748</id><published>2012-12-12T00:51:00.000-08:00</published><updated>2014-01-02T09:47:49.675-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><title type='text'>One-dimensional interpolation with R</title><content type='html'>&lt;p&gt;The function &lt;code&gt;approx()&lt;/code&gt; and &lt;code&gt;spline()&lt;/code&gt; provide one-dimensional linear and non-linear interpolation with R, respectively. Syntax of them are similar, for example,&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;x.old &amp;lt;- 1:10
y.old &amp;lt;- rnorm(10)
x.new &amp;lt;- seq(1, 10, 0.001)

# Linear interpolation
y.new.linear &amp;lt;- approx(x.old, y.old, xout = x.new)$y

# Forsythe, Malcolm and Moler&#39;s spline (default of spline)
y.new.fmm &amp;lt;- spline(x.old, y.old, xout = x.new, method = &quot;fmm&quot;)$y

# Natural splines
y.new.natural &amp;lt;- 
  spline(x.old, y.old, xout = x.new, method = &quot;natural&quot;)$y

# Periodic splines 
y.new.periodic &amp;lt;-
  spline(x.old, y.old, xout = x.new, method = &quot;periodic&quot;)$y

# Plot
plot(
  y.new.fmm ~ x.new,
  type=&quot;l&quot;, col=2, xlab=&quot;x&quot;, ylab=&quot;y&quot;,
  main=&quot;One-dimensional Interpolation with R&quot;
)
lines(y.new.periodic ~ x.new, col=3)
lines(y.new.natural ~ x.new, col=4)
lines(y.new.linear ~ x.new, col=5)
points(y.old ~ x.old)
legend(8, 2, c(&quot;fmm&quot;, &quot;periodic&quot;, &quot;natural&quot;, &quot;linear&quot;, &quot;old&quot;),
  col = c(2:5,1), lty = c(1,1,1,1,0), pch = c(NA,NA,NA,NA,1), merge = T
)&lt;/pre&gt;


&lt;p&gt;
&lt;a href=&quot;https://picasaweb.google.com/lh/photo/9pQ0rK6V2w_L0C58venZCJGnzz1qy_nbzwK82mACLgY?feat=directlink&quot;&gt;&lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr-ZHffz_Tc6EqZmd5V29IvH4qc5f49lZ_puMzbjCGmlWtVRKxPE5AXb3kis_wVOcw3eDpD3f_bwkeqPgr0Re_agz0Pghl-KYWSRBo96v7YVQuBy9qT55DmU0uXaZ65u_i8FoFqDMRR_8/s800/one-dimensional-interpolation-with-R.png&quot; alt=&quot;One-dimensional Interpolation with R&quot; style=&quot;max-width:680px&quot; /&gt;&lt;/a&gt;
&lt;/p&gt;

</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/3144901992655818748/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2012/12/one-dimensional-interpolation-with-r.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/3144901992655818748'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/3144901992655818748'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2012/12/one-dimensional-interpolation-with-r.html' title='One-dimensional interpolation with R'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjr-ZHffz_Tc6EqZmd5V29IvH4qc5f49lZ_puMzbjCGmlWtVRKxPE5AXb3kis_wVOcw3eDpD3f_bwkeqPgr0Re_agz0Pghl-KYWSRBo96v7YVQuBy9qT55DmU0uXaZ65u_i8FoFqDMRR_8/s72-c/one-dimensional-interpolation-with-R.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-6539564114316152337</id><published>2012-05-27T11:13:00.001-07:00</published><updated>2014-10-17T13:20:53.850-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="R-function"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>Nested ANOVA permutation test in R</title><content type='html'>&lt;p class=&quot;abstract&quot;&gt;A R function for the permutation test for onw-way nested design ANOVA.&lt;/p&gt;

&lt;p&gt;
This work is inspired by Dr.&amp;nbsp;David C. Howell, University of Vermont.
In Dr.&amp;nbsp;Howell’s webpage “&lt;a href=&quot;http://www.uvm.edu/~dhowell/StatPages/More_Stuff/Permutation%20Anova/PermTestsAnova.html&quot;&gt;Permutation Tests for Factorial ANOVA Designs&lt;/a&gt;” (http://www.uvm.edu/~dhowell/StatPages/More_Stuff/Permutation%20Anova/PermTestsAnova.html [access on May 28, 2012]), he described the algorithm of permutation test for a one-way nested design ANOVA by using R language.
I followed this algorithm and made a R function to do this test.
&lt;/p&gt;


&lt;script src=&quot;https://gist.github.com/chenpanliao/42d8ade2461e67f17037.js&quot;&gt;&lt;/script&gt;

&lt;!--
&lt;pre class=&quot;prettyprint&quot;&gt;#    Copyright 2012 Chen-Pan Liao &lt;andrew.43@gmail.com&gt;
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    any later version.
#    
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see &amp;lt;http://www.gnu.org/licenses/&amp;gt;.

# Author: Chen-Pan Liao
# Date: May 28, 2012
#
# This work is inspirated by Dr. David C. Howell, University of Vermont.
# See Dr. Howell&#39;s webpage to learn more:
# http://www.uvm.edu/~dhowell/StatPages/More_Stuff/
# Permutation%20Anova/PermTestsAnova.html

nestedPermutationAnova &amp;lt;- function(
  dv,           # Dependent variable
  treatment,    # Treatment
  subject,      # Experimental unit
  nreps = 1999  # Number of permutation
) {
  # initiation
  dv &amp;lt;- as.numeric(dv)
  treatment &amp;lt;- as.factor(treatment)
  subject &amp;lt;- as.factor(subject)
  nreps &amp;lt;- as.integer(nreps)
  subject &amp;lt;- as.factor(paste(subject,treatment,sep=&quot;_in_&quot;))
    # rename subject as &quot;subject_in_treatment&quot;
  treatment.name &amp;lt;- levels(treatment)
  subject.name &amp;lt;- levels(subject)
  treatment.n &amp;lt;- nlevels(treatment)
  subject.n &amp;lt;- nlevels(subject)
  
  f.treatment &amp;lt;- numeric(nreps)
  f.subject &amp;lt;- numeric(nreps);
  
  # original model
  model.0 &amp;lt;- summary(aov(dv ~ treatment / subject))
  f.subject.0 &amp;lt;- model.0[[1]][1,4]
  f.treatment.0 &amp;lt;- model.0[[1]][1,3] / model.0[[1]][2,3]
  
  # permutation
  for (i in 1:nreps) {

    # permutation for effect of subject
    dv.subject &amp;lt;- NULL
    for (j in 1:treatment.n){
      dv.subject &amp;lt;- c(
        dv.subject,
        sample(dv[ treatment == treatment.name[j] ])
      )
    }
    model &amp;lt;- summary(aov(dv.subject ~ treatment / subject))
    f.subject[i] &amp;lt;- model[[1]][1,4]
    
    # permutation for effect of treatment
    dv.treatment &amp;lt;- NULL
    subject.name.reorder &amp;lt;- sample(subject.name)
    for (j in 1:subject.n){
      dv.treatment &amp;lt;- c(
        dv.treatment,
        dv[ subject == subject.name.reorder[j] ]
      )
    }
    model &amp;lt;- summary(aov(dv.treatment ~ treatment / subject))
    f.treatment[i] &amp;lt;- model[[1]][1,3] / model[[1]][2,3]
  }
  
  # p-value
  p.treatment &amp;lt;- (sum(f.treatment &amp;gt;= f.treatment.0)+1) / (nreps+1)
  p.subject &amp;lt;- (sum(f.subject &amp;gt;= f.subject.0)+1) / (nreps+1)
  
  # plot
  par(mfrow = c(1,2))
  hist(f.treatment, breaks=40, xlab=&quot;Sudo f-value for treatment&quot;, main=&quot;&quot;)
  abline(v=f.treatment.0, lty=2)
  hist(f.subject, breaks=40, xlab=&quot;Sudo f-value for subject&quot;, main=&quot;&quot;)
  abline(v=f.subject.0, lty=2)
  
  # return
  return (
    list(
      p.treatment = p.treatment,
      p.subject = p.subject,
      nreps = nreps
    )
  )
}&lt;/pre&gt;
--&gt;


&lt;h2&gt;Example&lt;/h2&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;dat &lt;- read.csv(textConnection(&quot;
 trt , unit , obs
 1 , 1 , 11
 1 , 1 ,  9
 1 , 1 ,  9
 1 , 2 ,  8
 1 , 2 ,  7
 1 , 2 ,  6
 1 , 3 ,  8
 1 , 3 , 10
 1 , 3 , 11
 2 , 4 , 11
 2 , 4 ,  8
 2 , 4 ,  7
 2 , 5 , 10
 2 , 5 , 14
 2 , 5 , 12
 2 , 6 ,  9
 2 , 6 , 10
 2 , 6 ,  8
&quot;))

## traditional nested ANOVA
mod.2 &lt;- aov(
 obs ~ factor(trt) + Error(factor(unit)),
 data = dat
)
summary(mod.2) # alternative

## permutation nested ANOVA
nestedPermutationAnova(dat$obs, dat$trt, dat$unit, 499)&lt;/pre&gt;

</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/6539564114316152337/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2012/05/nested-anova-permutation-test-in-r.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/6539564114316152337'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/6539564114316152337'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2012/05/nested-anova-permutation-test-in-r.html' title='Nested ANOVA permutation test in R'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-2051227752407071831</id><published>2012-05-10T03:44:00.002-07:00</published><updated>2013-03-17T11:15:39.666-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Generalized regression"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>Logistic regression with package rms in R</title><content type='html'>&lt;p class=&quot;abstract&quot;&gt;&lt;/p&gt;


&lt;p&gt;The &lt;a href=&quot;http://cran.r-project.org/web/packages/rms/index.html&quot;&gt;&lt;code&gt;rms&lt;/code&gt;&lt;/a&gt; package by Frank E Harrell Jr provides a useful function &lt;code&gt;lrm&lt;/code&gt; to apply logistic regression. Moreover, this function also provides many useful statistics, and a gooeness-of-fit test (le Cessie and Houwelingen test) could be applied by using the function &lt;code&gt;residuals(a lrm model, &quot;gof&quot;)&lt;/code&gt;. Read the &lt;a href=&quot;http://cran.r-project.org/web/packages/rms/rms.pdf&quot;&gt;official document of package &lt;code&gt;rms&lt;/code&gt;&lt;/a&gt; for more details.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/2051227752407071831/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2012/05/logistic-regression-with-package-rms-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/2051227752407071831'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/2051227752407071831'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2012/05/logistic-regression-with-package-rms-in.html' title='Logistic regression with package rms in R'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-5037045527086038496</id><published>2012-03-18T12:02:00.001-07:00</published><updated>2014-01-13T19:10:43.210-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>淺談貝氏定理</title><content type='html'>&lt;div lang=&quot;zh-Hant&quot;&gt;

&lt;p class=&quot;abstract&quot;&gt;本文將介紹貝氏定理 (Bayes’ theorem) 的推導與實例。&lt;/p&gt;

&lt;h4&gt;推導&lt;/h4&gt;

&lt;p&gt;
條件機率的定義是,
\[ \Pr(A|B)= \frac{\Pr(A \cap B)}{\Pr(B)}, \]
其中 $\Pr(A|B)$ 是指在事件 $B$ 發生的前題下, 事件 $A$ 發生的機率; $\Pr(A \cap B)$ 是指事件 $A$ 發生且事件 $B$ 發生的機率. 當然, $A \cap B$ 與 $B \cap A$ 是完全同義的, 因此, 我們也可以得到
\[ \Pr(B|A)=\frac{\Pr(B \cap A)}{\Pr(A)}=\frac{\Pr(A \cap B)}{\Pr(A)}. \]
&lt;p&gt;

&lt;p&gt;
有趣的事發生了. 若將上述二式中的 $\Pr(A \cap B)$ 單離出來, 可以發現
\[ \Pr(A \cap B) = \Pr(A|B) \times \Pr(B) = \Pr(B|A) \times \Pr(A), \]
可得
\[ \Pr(A|B) = \frac{\Pr(B|A) \times \Pr(A)}{\Pr(B)}, \]
被稱為貝氏定理. 不過在實用上來說, 常利用全機率定理改寫分母, 成為定理的另一個形式
\[ \Pr(A|B) = \frac{\Pr(B|A) \times \Pr(A)}{\Pr(B|A)\times \Pr(A) + \Pr(B|\lnot A)\times \Pr(\lnot A) }, \]
其中 $\lnot A$ 表示 “非 $A$”. 
&lt;/p&gt;


&lt;h4&gt;例題&lt;/h4&gt;
&lt;p&gt;
&lt;a href=&quot;http://en.wikipedia.org/w/index.php?title=Bayes%27_theorem&amp;oldid=482244832&quot;&gt;Wikipedia&lt;/a&gt; 中描述了一個貝氏定理中著名的 “吸毒測試” 例題, 簡單翻譯與修改後如下. 某種檢測吸毒的方法具有 99% 的敏感度和 97% 的可靠度, 也就是有吸毒者受測呈陽性的機率為 99% 而非吸毒者受測呈陰性的機率是 97%. 調查人員已經知道某群體的總體吸毒率為 0.1%. 已知, 在該群體中某一位人員受檢後呈陽性, 則該人員吸毒的機率是多少?
&lt;/p&gt;

&lt;p&gt;
令吸毒事件為 $D$, 測試結果呈陽性為 $P$. 由命題可知, $\Pr(P|D)=0.99$, $\Pr(\lnot P|\lnot D)=0.97$, $\Pr(D)=0.001$. 由貝氏定義可知, 
\[
\begin{aligned}
\Pr(D|P) =&amp; \frac{\Pr(P|D) \times \Pr(D)}{\Pr(P)} \\
         =&amp; \frac{\Pr(P|D) \times \Pr(D)}{\Pr(P|D) \times \Pr(D) + \Pr(P|\lnot D) \times \Pr(\lnot D)} \\
         =&amp; \frac{\Pr(P|D) \times \Pr(D)}{\Pr(P|D) \times \Pr(D) + [1-\Pr(\lnot P|\lnot D)] \times [1-\Pr(\lnot D)]} \\
         =&amp; \frac{0.99 \times 0.001}{0.99 \times 0.001 + (1-0.97) \times (1-0.001)} \\
    \simeq&amp; 0.032
\end{aligned}
\]
為所求, 也就是該人員受測呈陽性但有吸毒的機率只有 3.2%, 即使該檢測有 99% 的敏感度. 這種結果是不是與你預期的差距很大?
&lt;/p&gt;

&lt;p&gt;
利用貝氏定理, 統計學家已經將之推廣出一派新的統計學門, 稱為貝氏推論. 貝氏推論的核心概念是, 在已知的資料中, 如何從過去的知識推論母體. 在例題中, 過去的知識 $\Pr(D)$ 被稱為事前機率 (prior), $\Pr(D|P)$ 被稱為事後機率 (posterior), 而 $\Pr(P|D) / \Pr(P)$ 被稱為標準概似度 (standardised likelihood). 因此, 貝氏定理亦常被表示成
\[ \text{posterior} = \text{standardised likelihood} \times \text{prior}. \]
假如 posterior 不易取得, 可藉由對 prior 更多的了解, 使我們可以更有信心地推論 posterior, 是貝氏推論的重要武器.
&lt;/p&gt;

&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/5037045527086038496/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2012/03/basic-of-bayes-theorem.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/5037045527086038496'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/5037045527086038496'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2012/03/basic-of-bayes-theorem.html' title='淺談貝氏定理'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-3288657899730702586</id><published>2011-12-10T01:13:00.001-08:00</published><updated>2013-03-30T19:56:06.016-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>Create a NA matrix in R</title><content type='html'>&lt;p&gt;


&lt;p&gt;&lt;code&gt;matrix(data=NA, nrow=3, ncol=5)&lt;/code&gt; can create/initiate a 3 × 5 NA matrix on the fly. Of course,  &lt;code&gt;matrix(data=0, nrow=3, ncol=5)&lt;/code&gt; may be useful in case.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/3288657899730702586/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2011/12/create-na-matrix-in-r.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/3288657899730702586'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/3288657899730702586'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2011/12/create-na-matrix-in-r.html' title='Create a NA matrix in R'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-6994529394099475678</id><published>2011-10-30T16:56:00.000-07:00</published><updated>2014-01-07T04:21:08.128-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>Statistical computing by using R</title><content type='html'>
&lt;p&gt;
最近把東海大學生命科學系大學部的生物統計學實驗講義中的 SAS code 全改寫成 R code。內容是如何使用 R 計算常見的統計檢定，包括例題及 R code。該文件以 &lt;a rel=&quot;license&quot; href=&quot;http://creativecommons.org/licenses/by-sa/3.0/&quot;&gt;Creative Commons Attribution-ShareAlike 3.0 Unported License&lt;/a&gt; 授權。&lt;/p&gt;



&lt;p&gt;
&lt;a href=&quot;http://www.scribd.com/doc/70943527/Statistical-computing-by-using-R&quot; title=&quot;View Statistical computing by using R on Scribd&quot;&gt;View Statistical computing by using R on Scribd&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
&lt;iframe src=&quot;https://docs.google.com/file/d/0B0E2FRIvjDDoOU1VUkFVb1BXRVU/preview&quot; width=&quot;640&quot; height=&quot;480&quot;&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;!-- 
&lt;iframe class=&quot;scribd_iframe_embed&quot; src=&quot;//www.scribd.com/embeds/70943527/content?start_page=1&amp;view_mode=scroll&amp;access_key=key-2lmdoe4endtkj6rg0wuz&amp;show_recommendations=false&quot; data-auto-height=&quot;false&quot; data-aspect-ratio=&quot;0.708006279434851&quot; scrolling=&quot;no&quot; id=&quot;doc_98036&quot; width=&quot;400&quot; height=&quot;600&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;
&lt;iframe class=&quot;scribd_iframe_embed&quot; data-aspect-ratio=&quot;0.6&quot; data-auto-height=&quot;true&quot; id=&quot;doc_49569&quot; scrolling=&quot;no&quot; src=&quot;http://www.scribd.com/embeds/70943527/content?start_page=1&amp;amp;view_mode=list&amp;amp;access_key=key-2lmdoe4endtkj6rg0wuz&quot; width=&quot;100%&quot;&gt;&lt;/iframe&gt;&lt;/p&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
(function() { var scribd = document.createElement(&quot;script&quot;); scribd.type = &quot;text/javascript&quot;; scribd.async = true; scribd.src = &quot;http://www.scribd.com/javascripts/embed_code/inject.js&quot;; var s = document.getElementsByTagName(&quot;script&quot;)[0]; s.parentNode.insertBefore(scribd, s); })();
&lt;/script&gt;
--&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/6994529394099475678/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2011/10/statistical-computing-by-using-r.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/6994529394099475678'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/6994529394099475678'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2011/10/statistical-computing-by-using-r.html' title='Statistical computing by using R'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total><georss:featurename>Taichung City, Taiwan</georss:featurename><georss:point>24.2332076 120.9417368</georss:point><georss:box>23.7698726 120.3100228 24.6965426 121.5734508</georss:box></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-5458167648297774596</id><published>2011-08-04T05:57:00.000-07:00</published><updated>2014-01-29T00:59:33.310-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Generalized regression"/><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="R-function"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>A function of Pearson/Deviance goodness-of-fit for (generalized) linear regression</title><content type='html'>&lt;!-- &lt;p&gt;&lt;a href=&quot;http://zh.wikipedia.org/wiki/File:Rlogo.png&quot;&gt;&lt;img src=&quot;http://upload.wikimedia.org/wikipedia/commons/c/c1/Rlogo.png&quot; alt=&quot;GNU R logo&quot; class=&quot;rightImage&quot; /&gt;&lt;/a&gt;&lt;/p&gt; --&gt;

&lt;p class=&quot;abstract&quot;&gt;After an analysis of (generalized) linear regression, a Pearson/Deviance goodness-of-fit test is useful to test if the model fitted reasonably. I wrote a simple function in R to do it.&lt;/p&gt;


&lt;h4&gt;Source Code&lt;/h4&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;#    Copyright 2011 Chen-Pan Liao &lt;andrew.43@gmail.com&gt;
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    any later version.
#    
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see &amp;lt;http://www.gnu.org/licenses/&amp;gt;.

# Usage example:
#   &lt;em&gt;model.name&lt;/em&gt; &lt;- glm (...)
#   lm.fit.test(&lt;em&gt;model.name&lt;/em&gt;)

lm.fit.test &amp;lt;- function (my.model) {
  df &amp;lt;- my.model$df.residual;
  chisq.pearson &amp;lt;- sum(resid(my.model , type=&quot;pearson&quot;)^2);
  chisq.deviance &amp;lt;- sum(resid(my.model , type=&quot;deviance&quot;)^2);
  p.pearson &amp;lt;- pchisq( chisq.pearson , df , lower.tail=F );
  p.deviance &amp;lt;- pchisq( chisq.deviance , df , lower.tail=F );
  ratio.pearson &amp;lt;- chisq.pearson / df;
  ratio.deviance &amp;lt;- chisq.deviance / df;
  cat(
    &quot;Pearson chisq = &quot; , chisq.pearson ,
    &quot;, df = &quot; , df ,
    &quot;, p = &quot; , p.pearson ,
    &quot;, chisq / df = &quot; , chisq.pearson/df ,
    &quot;.\n&quot; ,
    &quot;Deviance chisq = &quot; , chisq.deviance ,
    &quot;, df = &quot; , df ,
    &quot;, p = &quot; , p.deviance ,
    &quot;, chisq / df = &quot; , chisq.deviance/df ,
    &quot;.\n&quot;
  );
}&lt;/pre&gt;


&lt;h4&gt;Usage and example&lt;/h4&gt;
&lt;p&gt;After defining a glm() model as a custom name, copy the source code into R interpreter and then input &lt;code&gt;lm.fit.test(your.model.name)&lt;/code&gt; where &lt;code&gt;your.model.name&lt;/code&gt; is the custom glm model name. See the fallowing example&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;y &amp;lt;- c(rpois(20,1) , rpois(20,2));
x &amp;lt;- gl(2 , 20);
my.model &amp;lt;- glm(y~x , family=poisson);
summary(my.model);
lm.fit.test(my.model);&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/5458167648297774596/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2011/08/function-of-pearsondeviance-goodness-of.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/5458167648297774596'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/5458167648297774596'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2011/08/function-of-pearsondeviance-goodness-of.html' title='A function of Pearson/Deviance goodness-of-fit for (generalized) linear regression'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-8158895373325398683</id><published>2011-07-07T08:56:00.001-07:00</published><updated>2013-03-30T19:58:12.027-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>Games-Howell post-hoc in R</title><content type='html'>

&lt;p&gt;
Mr.&amp;nbsp;&lt;a href=&quot;http://aoki2.si.gunma-u.ac.jp/Aoki/Aoki.html&quot;&gt;Shigenobu AOKI&lt;/a&gt; displayed a quick R function for both of Tukey and Games-Howell post-hoc comparisons after an ANOVA. The Games-Howell post-hoc is famous for non-equal variances between treatments. Read more on &lt;a href=&quot;http://aoki2.si.gunma-u.ac.jp/R/tukey.html&quot;&gt;the webpage about Games-Howell post-hoc in R&lt;/a&gt;.
&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/8158895373325398683/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2011/07/games-howell-post-hoc-in-r.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/8158895373325398683'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/8158895373325398683'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2011/07/games-howell-post-hoc-in-r.html' title='Games-Howell post-hoc in R'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-3205168939967751207</id><published>2011-06-22T23:21:00.000-07:00</published><updated>2014-01-13T19:11:02.415-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Generalized regression"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>邏輯斯迴歸、勝算與勝算比的關係 (logistic regression, odds and odds ratio)</title><content type='html'>&lt;div lang=&quot;zh-Hant&quot;&gt;

&lt;h4&gt;公式一覽&lt;/h4&gt;
&lt;p&gt;
&lt;!-- &lt;img src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgH2OqYQSfTBuhTcv7atGovU3mLxEDv0N_1nQC6GCk8BG0VFYpDUTBIXNFmS-xbpepHYIRRpnCRhmiRIH-fibQqXH7n4VQm0kghI0nTd7K4u6STiAvhW6dkyQqK3L8BA8DzjWIgixIuU44/s570/logistic-model.png&quot; alt=&quot;Logistic regression, odds and odds ratio&quot; class=&quot;centerImage&quot; /&gt; --&gt;

\[
\log ( \text{odd} ) = \text{logit} (p) = \log ( \frac{\Pr (y=1) }{1 - \Pr (y=1)} ) = \beta_0 + \sum_{i=1}^{n} \beta_i x_i
\]

\[
\text{odd} = \frac{\Pr (y=1) }{1 - \Pr (y=1)} = \exp ( \beta_0 + \sum_{i=1}^{n} \beta_i x_i )
\]

\[
\Pr (y=1) = \frac{\text{odd}}{1+\text{odd}} = \frac{1}{1+\text{odd}^{-1}} = \frac{1}{1+\exp (-( \beta_0 + \sum_{i=1}^{n}\beta_i x_i)) }
\]

\[
\log (\text{odds ratio}) = \log \left( \frac{\text{odd}_1}{\text{odd}_2} \right) = \log(\text{odd}_1) - \log(\text{odd}_2)
\]
&lt;/p&gt;


&lt;h4&gt;寫在前面&lt;/h4&gt;
&lt;p class=&quot;abstract&quot;&gt;
如何解讀邏輯斯迴歸 (logistic regression) 的迴歸式? 因為我常搞錯邏輯斯迴歸結果與勝算或勝算比的關係, 特書此筆記. 本文最主要的內容是唸過 “&lt;a href=&quot;http://www.ats.ucla.edu/stat/mult_pkg/faq/general/odds_ratio.htm&quot;&gt;FAQ: How do I interpret odds ratios in logistic regression?&lt;/a&gt;” 一文後的筆記。&lt;/p&gt;


&lt;h4&gt;名詞翻譯&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;logistic regression: 邏級式迴歸;&lt;/li&gt;
&lt;li&gt;odds: 勝算;&lt;/li&gt;
&lt;li&gt;odds ratio: 勝算比.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;請注意 “勝算” 與 “勝算比” 是兩回事.&lt;/p&gt;

&lt;h4&gt;僅包含常數項的迴歸式&lt;/h4&gt;
&lt;p&gt;在模型
\[ \text{logit}(p) = \log(\frac{1}{1-p}) = \beta_0,\]
其中 $p$ 為依變數 $y = 1$ 之機率, 而 $y$ 為布林資料 (0 或 1) 以表示肯定或否定. 此時, $\beta_0$ 即為
\[ \log(\frac{\Pr (y = 1)}{\Pr (y = 0)}).\]
說成白話文, $y = 1$ 的機率除以 $y = 0$ 的機率為 $\exp(\beta_0)$.&lt;/p&gt;

&lt;p&gt;我以下列資料舉例. 抽樣的 10 名學生中, 有 6 人有手機而 4 人沒有. 計算 “有手機” 對 “無手機” 的勝算為 $\frac{6/10}{4/10} = 1.5$. 在迴歸式中可得 $\text{logit}(p) = \log( \frac{1}{1-p}) = 0.4055$, 可驗證 $\exp(0.4055) = 1.500052$ (因為 $y = 1$ 為有手機而 $y = 0$ 為無手機). 以下為驗證之 R code:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; &lt;code&gt;isPhone&amp;lt;-c(rep(1,6),rep(0,4))&lt;/code&gt;
&amp;gt; &lt;code&gt;m&amp;lt;-glm(isPhone~1,family=binomial);summary(m)&lt;/code&gt;

Call:
glm(formula = isPhone ~ 1, family = binomial)

Deviance Residuals: 
   Min      1Q  Median      3Q     Max  
-1.354  -1.354   1.011   1.011   1.011  

Coefficients:
            Estimate Std. Error z value Pr(&amp;gt;|z|)
(Intercept)   0.4055     0.6455   0.628     0.53

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 13.46  on 9  degrees of freedom
Residual deviance: 13.46  on 9  degrees of freedom
AIC: 15.46

Number of Fisher Scoring iterations: 4

&amp;gt; exp(0.4055)
[1] 1.500052&lt;/pre&gt;

&lt;h4&gt;包含一個二元類別自變數的迴歸式&lt;/h4&gt;

&lt;p&gt;在模型 
\[ \text{logit}(p) = \beta_0 + \beta_1 x_1, \]
其中 $p$ 為依變數 $y = 1$ 之機率而 $y$ 為布林資料 (0 或 1) 以表示肯定或否定, $x_1$ 為二元類別資料並轉換為布林資料 (0 或 1). 此時, $\beta_0$ 即為 $x = 0$ 時 $\log ( \frac{\Pr (y = 1)}{\Pr (y = 0)})$, 而 $\beta_1$ 為 $x_1 = 1$ 對 $x_1 = 0$ 在 $y$ 是否為 1 的勝算比. 說成白話文, $x_1 = 0$ 時 $y = 1$ 的機率除以 $y = 0$ 的機率為 $\exp( \beta_0)$; $x_1 = 1$ 對 $x_1 = 0$ 的勝算比為 $\beta_1$.&lt;/p&gt;

&lt;p&gt;我以下列資料舉例. 抽樣的 10 名學生中, 有 6 名男生 4 名女生, 且每人分別擁有手機或沒有, 資料如下:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;有無手機&lt;/th&gt;&lt;th&gt;男生 (x&lt;sub&gt;1&lt;/sub&gt; = 1)&lt;/th&gt;&lt;th&gt;女生 (x&lt;sub&gt;1&lt;/sub&gt; = 0)&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;有 (y = 1)&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;無 (y = 0)&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;小計&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;“男生有手機” 對 “男生無手機” 的勝算為 $\frac{5/6}{1/6} = 5$; “女生有手機” 對 “女生無手機” 的勝算為 $\frac{1/4}{3/4} = 0.3333$; 二者的勝算比為 $5 / 0.3333 = 15$.&lt;/p&gt;

&lt;p&gt;在男生 = 1, 女生 = 0, 有手機 = 1, 無手機 = 0 的設定下, 迴歸式中可得 $\text{logit}(p) = -1.099 + 2.708(\text{性別})$, 可驗證 $\exp (-1.099) = 0.3332041$ 為 “女生有手機” 對 “女生無手機” 的勝算 (因為女生的編碼為 $y = 0$); $\exp (2.708) = 14.99925$ 為性別與有無手機的勝算比, 可解釋作 “男生有手機之勝算為女生的 15 倍”. 以下為驗證之 R code:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; isMale&amp;lt;-c(1,1,1,1,1,1,0,0,0,0)
&amp;gt; isPhone&amp;lt;-as.factor(c(1,1,1,1,1,0,0,0,0,1))
&amp;gt; m&amp;lt;-glm(isPhone~isMale,family=binomial);summary(m)

Call:
glm(formula = isMale ~ isPhone, family = binomial)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-1.8930  -0.7585   0.6039   0.6039   1.6651  

Coefficients:
            Estimate Std. Error z value Pr(&amp;gt;|z|)  
(Intercept)   -1.099      1.155  -0.951   0.3414  
isMale         2.708      1.592   1.701   0.0889 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 13.4602  on 9  degrees of freedom
Residual deviance:  9.9054  on 8  degrees of freedom
AIC: 13.905

Number of Fisher Scoring iterations: 4

&amp;gt; exp(-1.099)
[1] 0.3332041
&amp;gt; exp(2.708)
[1] 14.99925&lt;/pre&gt;

&lt;h4&gt;包含一連續變數作為自變數的迴歸式&lt;/h4&gt;
&lt;p&gt;在模型
\[\text{logit}(p) = \beta_0 + \beta_0 x_1,\]
其中 $p$ 為依變數 $y = 1$ 之機率而 $y$ 為布林資料 (0 或 1) 以表示肯定或否定, $x_1$ 為連續型變數. 此時, $\beta_0$ 即為 $x = 0$ 時 $\log (y = 1 \text{對} y = 0 \text{的勝算})$, 而 $\beta_1$ 為 $x_1$ 對勝算的貢獻量. 說成白話文, $x_1=0$ 時 $y = 1$ 的機率除以 $y = 0$ 的機率為 $\exp(\beta_0)$; 若 $x_1 \neq 0$ 時 $y = 1$ 的機率除以 $y = 0$ 的機率為 $\exp (\beta_0 + \beta_1 x_1)$, 而 $\beta_1$ 為正數或負數則影響了勝算的正向或負向影響。&lt;/p&gt;


&lt;p&gt;我以下列資料舉例. 抽樣的 10 名學生中, 每人有不同每日零用錢及是否擁有手機, 資料如下:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;&lt;th&gt;有無手機 (y, 1 為有, 0 為無)&lt;/th&gt;&lt;th&gt;每日零用錢 (x&lt;sub&gt;1&lt;/sub&gt;)&lt;/th&gt;&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;100&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;120&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;90&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;110&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;110&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;80&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;100&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;90&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;70&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;0&lt;/td&gt;&lt;td&gt;110&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;上述資料可得回歸式 $\text{logit}(p) = -9.71278 + 0.09840(\text{每日零用錢})$. 由迴歸式可觀察出, 每日零用錢每增加 1 單位則對有手機之勝算增加 $\text{exp}(0.09840) = 1.103404$, 亦表示有無手機在多拿 1 單位零用錢與沒有者的勝算比是 $\exp(\beta_1 \times 1) = 1.103404$，多拿 2 單位則為 $\exp(\beta_1 \times 2)$, 以此類推.&lt;/p&gt;

&lt;p&gt;我們可以估計每日零用錢為 70 時有手機對無手機的勝算: $\exp (-9.71278 + 0.09840 \times 70) = 0.05932171$, 可解釋為每日零用錢為 70 的學生中有手機的機率為沒有手機的 0.05932171 倍. 要估算每日零用錢為 75 的勝算亦如法炮製: $\exp( -9.71278 + 0.09840 \times 75) = 0.09702564$. 因此, 二者的勝算比為 $0.09702564 / 0.05932171 = 1.635584$, 也就是 $\exp (0.09840 \times 5)$. &lt;/p&gt;

&lt;p&gt;由於$\text{勝算} = p / (1 - p)$, 故可由勝算回推 $p$, 公式為 $p = \text{勝算} / (1 + \text{勝算})$. 前面有談到零用錢為 70 時有手機對無手機的勝算為 0.05932171, 可推得零用錢為 70 時有手機的機率為 $0.05932171 / (1 + 0.05932171) = 0.05599971$. 同理, 每日零用錢為 75 時有手機的機率為 $0.09702564 / (1 + 0.09702564) = 0.08844428$.&lt;/p&gt;


&lt;pre class=&quot;prettyprint&quot;&gt;&amp;gt; isPhone&amp;lt;-c(1,1,1,1,1,0,0,0,0,0)
&amp;gt; money&amp;lt;-c(100, 120, 90, 110, 110, 80, 100, 90, 70, 110)
&amp;gt; m&amp;lt;-glm(isPhone~money,family=binomial);summary(m)
Call:
glm(formula = isPhone ~ money, family = binomial)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-1.67078  -0.76661   0.07113   0.75438   1.55604  

Coefficients:
            Estimate Std. Error z value Pr(&gt;|z|)
(Intercept) -9.71278    6.59477  -1.473    0.141
money        0.09840    0.06574   1.497    0.134

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 13.863  on 9  degrees of freedom
Residual deviance: 10.481  on 8  degrees of freedom
AIC: 14.481

Number of Fisher Scoring iterations: 4&lt;/pre&gt;

&lt;h4&gt;結論&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;邏輯斯迴歸的迴歸式 $\log ( \text{odd} ) = \text{logit} (p) = \log \left( \frac{\Pr (y=1) }{1 - \Pr (y=1)} \right) = \beta_0 + \sum_{i=1}^{n} \beta_i x_i$ 所加總得到的數值是 &lt;strong&gt;log(勝算)&lt;/strong&gt;, 故 exp(logit(p)) = 勝算;&lt;/li&gt;
&lt;li&gt;將二個勝算相除可以得到&lt;strong&gt;勝算比&lt;/strong&gt;;&lt;/li&gt;
&lt;li&gt;可由勝算回推機率, 公式為機率 = 勝算 / (1 + 勝算);&lt;/li&gt;
&lt;li&gt;因為指數與對數在運算上的特性, log(勝算比) 常成為二個 logit(p) 相加減之結果.&lt;/li&gt;
&lt;li&gt;若不解, 一切回歸到 “log(勝算) = log (p/(1 - p)) = 迴歸式” 及 “勝算比 = 勝算&lt;sub&gt;1&lt;/sub&gt; / 勝算&lt;sub&gt;2&lt;/sub&gt;” 的基本概念, 再往下推導.
&lt;/ul&gt;

&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/3205168939967751207/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2011/06/explaining-logistic-regression.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/3205168939967751207'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/3205168939967751207'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2011/06/explaining-logistic-regression.html' title='邏輯斯迴歸、勝算與勝算比的關係 (logistic regression, odds and odds ratio)'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-6860039588422104329</id><published>2011-06-20T02:48:00.000-07:00</published><updated>2014-01-02T09:57:29.828-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>Least-significant difference (LSD) in R</title><content type='html'>

&lt;p class=&quot;abstract&quot;&gt;How to apply LSD (as a kind of multiple comparisons after ANOVA) in R?&lt;/p&gt;

&lt;p&gt;A dataset for typical one-way ANOVA is given:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;y &amp;lt;- rnorm(40)
x &amp;lt;- gl(4,10)
m &amp;lt;- aov(y~x)&lt;/pre&gt;

&lt;p&gt;Applying LSD by using function &lt;code&gt;pairwise.t.test&lt;/code&gt;:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;pairwise.t.test(y,x,p.adj=&quot;none&quot;)&lt;/pre&gt;

&lt;p&gt;Applying LSD by using function &lt;code&gt;LSD.test&lt;/code&gt; from package &lt;code&gt;agricolae&lt;/code&gt;:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;library(agricolae)
LSD.test(m,&quot;x&quot;,group=T) # with grouping
LSD.test(m,&quot;x&quot;,group=F) # without grouping&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/6860039588422104329/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2011/06/least-significant-difference-lsd-in-r.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/6860039588422104329'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/6860039588422104329'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2011/06/least-significant-difference-lsd-in-r.html' title='Least-significant difference (LSD) in R'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-1612621324389218537</id><published>2011-06-15T04:12:00.000-07:00</published><updated>2014-01-02T09:59:38.528-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="R"/><category scheme="http://www.blogger.com/atom/ns#" term="Statistics"/><title type='text'>Specifying a reference level in R: function relevel</title><content type='html'>

&lt;p class=&quot;abstract&quot;&gt;How to specify a specific level as a reference level in GNU R while applying (generalized) linear regression?&lt;/p&gt;

&lt;p&gt;A dataframe &lt;code&gt;dt&lt;/code&gt; is given:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;y&amp;lt;-rpois(40,1)
x1&amp;lt;-gl(4,10)
x2&amp;lt;-gl(4,1,40)
dt &amp;lt;- data.frame(cbind(y,x1,x2))&lt;/pre&gt;

&lt;p&gt;Applying poisson regression:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;m1&amp;lt;-glm(y~factor(x1)+factor(x2),data=dt)
summary(m1)&lt;/pre&gt;

&lt;p&gt;The output shows x1=1 and x2=1 are both reference level defined by R automatically. To switch the reference level from x1=1 to x1=2, the function &lt;code&gt;relevel&lt;/code&gt; could be used:&lt;/p&gt;


&lt;pre class=&quot;prettyprint&quot;&gt;m2&amp;lt;-glm(y~&lt;strong&gt;relevel(factor(dt$x1),&quot;2&quot;)&lt;/strong&gt;+factor(x2),data=dt)
summary(m2)&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/1612621324389218537/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2011/06/specifying-reference-level-in-r.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/1612621324389218537'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/1612621324389218537'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2011/06/specifying-reference-level-in-r.html' title='Specifying a reference level in R: function relevel'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-5651085198595131177</id><published>2011-05-20T01:23:00.000-07:00</published><updated>2015-03-26T06:56:35.859-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="LATEX"/><title type='text'>阿盤的 Beamer 中英字型配方</title><content type='html'>&lt;div lang=&quot;zh-Hant&quot;&gt;

&lt;p class=&quot;abstract&quot;&gt;本文介紹我自己的 Beamer 中英文字型配置。&lt;/p&gt;


&lt;iframe src=&quot;//www.slideshare.net/slideshow/embed_code/29758346&quot; width=&quot;425&quot; height=&quot;355&quot; frameborder=&quot;0&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot; scrolling=&quot;no&quot; style=&quot;border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;&quot; allowfullscreen&gt; &lt;/iframe&gt; &lt;div style=&quot;margin-bottom:5px&quot;&gt; &lt;strong&gt; &lt;a href=&quot;//www.slideshare.net/chenpanliao/apan-beamertemplate&quot; title=&quot;阿盤的 Beamer 中英字型配方&quot; target=&quot;_blank&quot;&gt;阿盤的 Beamer 中英字型配方&lt;/a&gt; &lt;/strong&gt; from &lt;strong&gt;&lt;a href=&quot;//www.slideshare.net/chenpanliao&quot; target=&quot;_blank&quot;&gt;Chen-Pan Liao&lt;/a&gt;&lt;/strong&gt; &lt;/div&gt;

&lt;p&gt;&lt;!--詳文請閱&lt;a href=&quot;https://docs.google.com/viewer?a=v&amp;amp;pid=explorer&amp;chrome=true&amp;amp;srcid=0B0E2FRIvjDDoNGI5MzFiNjYtN2I4Ny00MzYwLTkyOTctYzg4Zjc4ZTQ2MDBj&amp;amp;hl=zh_TW&quot;&gt;阿盤的 Beamer 中英字型配方&lt;/a&gt;。--&gt;另歡迎至&lt;a href=&quot;http://hyperrate.com/thread.php?tid=23496&quot;&gt;討論串&lt;/a&gt;伺教。&lt;/p&gt;

&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/5651085198595131177'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/5651085198595131177'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2011/05/beamer.html' title='阿盤的 Beamer 中英字型配方'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-1101350361280593719.post-6388879260440375901</id><published>2010-06-25T15:38:00.000-07:00</published><updated>2014-01-13T19:34:15.307-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Database"/><category scheme="http://www.blogger.com/atom/ns#" term="HTML"/><category scheme="http://www.blogger.com/atom/ns#" term="PHP"/><title type='text'>學習 HTML, PHP 及 Database</title><content type='html'>&lt;div lang=&quot;zh-Hant&quot;&gt;

&lt;p class=&quot;abstract&quot;&gt;
在 &lt;a href=&quot;telnet://ptt.cc&quot;&gt;&lt;code&gt;telnet://ptt.cc&lt;/code&gt;&lt;/a&gt; 的 Web_Design 板有板友剛入門寫網頁. 他有為自己訂立了學習目標, 包括學習架站, 寫討論區等等, 但又不知道如何入門, 也不知道學會了這些技術有什麼幫助, 於是發表標題為 “[問題] 新手上路，卻不知路在何處...” 的文章. 有些板友給予他回應, 大多認為他想學的東西太基礎, 早已經有許多現成的技術可以取代, 例如 “手寫討論區? 何不用 &lt;a href=&quot;http://www.xoops.org/&quot;&gt;XOOPS&lt;/a&gt;?” 等等. 我看了還蠻多感觸的, 於是寫了篇文章回應, 內容如下.&lt;/p&gt;


&lt;blockquote&gt;
&lt;p&gt;
我願意分享一些自己的心得給原 PO.&lt;/p&gt;

&lt;p&gt;
我不是本科生, 也完全沒有相關背景, 頂多小時候學過 BASIC 語言罷了. 上大學之後, 為了當時流行每個人有自己的個人網頁, 就開始入門. 一開始, 當然也只是用 WYSIWYG 的軟體來製作網頁. 不知不覺, 就對 HTML 有了興趣. 畢竟這才是做網頁的基礎.&lt;/p&gt;

&lt;p&gt;
HTML 不只是做網頁罷了, 其內涵是讓你使用正確的 tag 來展現文稿結構. 當時看了一些書, 主要是教導大家屏棄舊式瀏覽器支援的 HTML 語法 (如 &amp;lt;font&amp;gt;), 而採用 HTML 4 + CSS. 學這些沒為了什麼, 就是讓自己的網頁在各種瀏覽器都可以正確解析罷了. (於是加入了 “凡是網頁設計師必痛恨 IE 6” 的族群.)&lt;/p&gt;

&lt;p&gt;
我就利用自己學會的 “標準 HTML + CSS” 開始寫自己的日記. 做到這裡, 就很有成就感了: 每天寫寫內容, 畫畫 CSS.&lt;/p&gt;

&lt;p&gt;
好日子過沒多久, 發現我的日記越來越多. 由於我是以 “一篇日記就是一個 HTML file” 的方式進行.
要讓每篇日記都共享相同的網頁結構變得非常困難. 看來, 是該把我的日記做得像 blog 才行. 於是我就開始學習 PHP.&lt;/p&gt;

&lt;p&gt;
當然, PHP 必竟是一種程式語言, 學習上會吃力一些. 於是我就從最簡單的 PHP 教本開始唸. 只唸我有需要的部份. 有時候簡單的教本還沒唸完, 就又買了進階級的教本來唸. 就這些唸了又練, 練了又唸, 花了不少時間,
終於把網頁以 PHP 改寫完.&lt;/p&gt;

&lt;p&gt;
沒多久, 我才認知, 原來我還需要學會 database 才能幫助我的日記網站有更好的維護方式. 那時我連 database 是幹嘛的都不懂, 只覺得大概就像個 excel 檔吧! 好在 PHP 的教本都是連帶著 MySQL 一起教, 也就學了 database 了.&lt;/p&gt;

&lt;p&gt;
從學 HTML 到 PHP + MySQL, 也花課餘時間二三年了呢. 雖然程式不到專業級, 但給自己的成就感是很豐富的了. 有了一些些實做的經驗, javascript, python, perl, 正規表示式也都學了點皮毛. 我幫老師寫過實驗室網站, 也利用這些知識幫助我目前的工作. 但我明白我是不可能用這些基本功養家的.&lt;/p&gt;

&lt;p&gt;
也許原 PO 會覺得, 花了這些多時間學會這些不能拿來養家的技術, 何苦? 我倒完全不這麼覺得啦.&lt;/p&gt;

&lt;p&gt;
我學會了 HTML, 也就學會了 “文稿結構” 這件事. 這對寫任何文章都有幫助, 也使我學習 LATEX 非常快.&lt;/p&gt;

&lt;p&gt;
為了學 CSS, 就探究了平面設計與繪圖軟體操作. 這在許多工作也都用得著.&lt;/p&gt;

&lt;p&gt;
學會了 PHP, 就等於你是個 “會寫程式的人” (即使只是半調子.) 會寫點小程式對做很多事都很有幫助, 例如別人要用手按一星期的工作, 而你只要半天就完成. 此外, 只要學會第一種程式語言, 第二種就容易許多了.&lt;/p&gt;

&lt;p&gt;
學了 database, 我連計帳本都做正規化了! 有時候老闆交代幫他整理資料或實驗數據, 懂 database 概念的人真的得心應手許多.&lt;/p&gt;

&lt;p&gt;
好吧, 也許我是在催眠自己. 但我沒後悔過花這麼多時間學過這些技術. 它們雖然沒直接幫助我賺什麼錢, 但附加價值是無窮的, 也大大地豐富我的精神生活. 不過, 假如現在我再重新寫一個中型網站專案, 我沒空也懶惰就是了!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://apansharing.blogspot.com/feeds/6388879260440375901/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://apansharing.blogspot.com/2011/06/learning-html-php-and-database.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/6388879260440375901'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1101350361280593719/posts/default/6388879260440375901'/><link rel='alternate' type='text/html' href='http://apansharing.blogspot.com/2011/06/learning-html-php-and-database.html' title='學習 HTML, PHP 及 Database'/><author><name>apan</name><uri>http://www.blogger.com/profile/11846279873979046420</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>