diff --git a/08-Intro-to-Bayes.Rmd b/08-Intro-to-Bayes.Rmd index 3e2fa7a3d..78736521a 100644 --- a/08-Intro-to-Bayes.Rmd +++ b/08-Intro-to-Bayes.Rmd @@ -1,4 +1,4 @@ -# (PART) 貝葉斯統計 {-} +# (PART) 貝葉斯統計 Introduction to Bayesian Statistics {-} # 貝葉斯統計入門 {#intro-Bayes} @@ -507,6 +507,12 @@ pi <- Vectorize(function(theta) dbeta(theta, 2,8)) curve(pi, xlab=~ theta, ylab="Density", main="Beta prior: a=2, b=8",frame=FALSE, lwd=2) ``` + +你會發現,beta分佈的圖形特徵由它的兩個超參數 (hyperparameter) `a, b` 決定。當相對地 `a` 比較大的時候,beta分佈的概率多傾向於較靠近橫軸右邊,也就是1的位置(較高概率),相對地 `b` 比較大的時候,beta分佈的曲線多傾向於靠近橫軸左邊,也就是0的位置(較低概率)。如果 `a` 和 `b`同時都在變大的話,beta分佈的曲線就變得比較“瘦”。這樣決定概率分佈形狀的參數,又被叫做**形狀參數 (shape parameters)** + + + + 我們定義 $a>0$ 時[伽馬方程](https://zh.wikipedia.org/wiki/%CE%93%E5%87%BD%E6%95%B0)爲 $$\Gamma(a)=\int_0^\infty x^{a-1}e^{-ax}\text{ d}x$$ diff --git a/12-Bayesian-stats.Rmd b/12-Bayesian-stats.Rmd index 23a5ef494..8efedd4b0 100644 --- a/12-Bayesian-stats.Rmd +++ b/12-Bayesian-stats.Rmd @@ -2685,13 +2685,13 @@ Dat <- list( # fit use rjags package -inits <- function (){ - list (beta0=rnorm(1), beta1=runif(1) ) -} +# inits <- function (){ +# list (beta0=rnorm(1), beta1=runif(1) ) +# } post.jags <- jags( data = Dat, - inits = inits, + # inits = inits, parameters.to.save = c("beta0", "beta1", "theta[6]"), n.iter = 5000, model.file = paste(bugpath, @@ -2866,7 +2866,7 @@ jagsModel <- jags.model( sep = ""), data = Dat, n.chains = 2, - inits = inits, + # inits = inits, quiet = TRUE) # Step 2 update 10000 iterations @@ -2904,7 +2904,7 @@ Dat <- list( ) post.jags <- jags( data = Dat, - inits = inits, + # inits = inits, parameters.to.save = c("beta0", "beta1", "theta[6]"), n.iter = 20000, model.file = paste(bugpath, @@ -2960,7 +2960,8 @@ model{ ``` -```{r OpenBUGSPractical0424, message=TRUE, warning=FALSE, echo=TRUE} +```{r OpenBUGSPractical0424, message=TRUE, warning=FALSE, echo=TRUE, eval=TRUE, cache=TRUE} + # R2JAGS codes: Dat <- list( y = c(1, 3, 6, 8, 11, 15, 17, 19), @@ -2968,9 +2969,10 @@ Dat <- list( x = c(30, 32, 34, 36, 38, 40, 42, 44), N = 8 ) + post.jags <- jags( data = Dat, - inits = inits, + # inits = inits, parameters.to.save = c("ED95", "OR", "P35", "beta0", "beta1"), n.iter = 20000, model.file = paste(bugpath, @@ -2986,6 +2988,8 @@ post.jags <- jags( print(post.jags) ``` + + 所以,藥物每增加劑量1mg,有療效的比值比是OR = 1.46 (95%CrI: 1.31, 1.63)。能夠達到95%患者都有療效的劑量是 45.02 mg (95% CrI: 42.7, 48.2 mg)。如果給予患者藥物劑量爲 35 mg,患者的疼痛能夠得到緩解(有療效)的概率是 32.3% (95% CrI: 22.7%, 42.3%)。跟着看到這裏的你是不是也覺得貝葉斯的結果和過程能夠更加豐富地回答我們想知道的問題呢? ```{r R-OpenBUGSPractical0425, cache=FALSE, fig.width=3.5, fig.height=5, fig.cap='Posterior density plots of ED95, OR, and P35.', fig.align='center', out.width='80%', message=TRUE, warning=FALSE} @@ -3216,7 +3220,31 @@ list(alpha = 10, beta = 0, gamma = -5, logsigma2 = 5) 一開始我們先採集1000個事後概率分布樣本。模型中的四個未知參數$\alpha, \beta, \gamma, \sigma^2$的1000次事後MCMC採樣的歷史痕跡圖繪制如圖 \@ref(fig:BayesianChapter0505)。我們可以看到模型收斂的速度很快。刨除前50次採樣的圖 (Fig. \@ref(fig:BayesianChapter0506)),可以對採樣過程看得更加清楚。像圖 \@ref(fig:BayesianChapter0506) 這樣粗粗的有點像毛毛蟲一樣的歷史痕跡圖通常象徵已經達到理想的收斂。 -```{r BayesianChapter0504, cache=TRUE, message=TRUE, warning=FALSE, echo=TRUE} + + + + + + + + + + + + + + + + + + + + + + + + +```{r BayesianChapter0505, cache=FALSE, fig.width=7, fig.height=8, fig.cap='History plots for iterations 1-1000 for the Gambia example.', fig.align='center', out.width='80%', message=FALSE, warning=FALSE, echo=FALSE} library(BRugs) # Step 1 check model modelCheck(paste(bugpath, "/backupfiles/gambia-model.txt", sep = "")) @@ -3237,10 +3265,6 @@ samplesSet(parameters) # Generate 1000 iterations modelUpdate(1000) -``` - - -```{r BayesianChapter0505, cache=FALSE, fig.width=7, fig.height=8, fig.cap='History plots for iterations 1-1000 for the Gambia example.', fig.align='center', out.width='80%', message=FALSE, warning=FALSE, echo=FALSE} #### PUT THE SAMPLED VALUES IN ONE R DATA FRAME: chain <- data.frame(alpha = samplesSample("alpha"), beta = samplesSample("beta"), diff --git a/backupfiles/logistic-reg-model-centred-stat.txt b/backupfiles/logistic-reg-model-centred-stat.txt index 7a6c2fa92..d63ebdf85 100644 --- a/backupfiles/logistic-reg-model-centred-stat.txt +++ b/backupfiles/logistic-reg-model-centred-stat.txt @@ -7,8 +7,10 @@ model{ } # priors beta0 ~ dunif(-100, 100) - beta1 ~ dunif(-100, 100) + beta1 ~ dnorm(1, 0.000000001) + + # generated values OR <- exp(beta1) # odds ratio of positive response per 1 mg increase in dose - ED95 <- (logit(0.95) - beta0)/beta1 + mean(x[]) # dose that gives 95% of maximal response + ED95 <- mean(x[]) + ((logit(0.95) - beta0) / (beta1)) # dose that gives 95% of maximal response logit(P35) <- beta0 + beta1 * (35 - mean(x[])) -} \ No newline at end of file +} diff --git a/docs/ANOVA.html b/docs/ANOVA.html index da51124bb..004bdcb66 100644 --- a/docs/ANOVA.html +++ b/docs/ANOVA.html @@ -6,7 +6,7 @@
lrt
的估計回歸參數發生了怎樣的變化?lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。lrt
也加入進隨機效應部分。schgend
改成 mean_girl
會給出怎樣的結果呢?gcse
,解釋變量是 lrt
。Fixed_reml2
模型中去,結果有發生顯著的變化嗎?school
level)的殘差。lrt
的估計回歸參數發生了怎樣的變化?lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。lrt
也加入進隨機效應部分。schgend
改成 mean_girl
會給出怎樣的結果呢?gcse
,解釋變量是 lrt
。Fixed_reml2
模型中去,結果有發生顯著的變化嗎?school
level)的殘差。再一次回到數據 (26.1) 的線性迴歸來看:
-growgam1 <- read_dta("backupfiles/growgam1.dta")
-Model <- lm(wt~age, data=growgam1)
-print(summary(Model), digit=6)
##
## Call:
## lm(formula = wt ~ age, data = growgam1)
@@ -1474,8 +1497,8 @@ 28.2.4 方差分析表格 the ANO
表格中最右邊一列是平均平方和 (mean sum of squares)。它的定義是將平方和除以各自的自由度。其中殘差的平均平方和 \(MS_{RES}=\frac{SS_{RES}}{(n-2)}\) 是替代模型下殘差方差的無偏估計。總體平均平方和 (total mean sum of squares),則是零假設模型時的殘差方差估計。在 R 裏面也已經演示過多次 anova(model)
是調取方差分析表格的代碼:
-Model <- lm(wt~age, data=growgam1)
-print(anova(Model), digit=8)
+
## Analysis of Variance Table
##
## Response: wt
@@ -1637,8 +1660,8 @@ 28.3.3 分組變量的平方和
\tag{28.10}
\]
其實這就是組間平方和 (between group sum of squares)
-Mdl0 <- aov(Age ~ Group, data = Walk) # fit a one-way ANOVA
-print(summary(Mdl0), digits = 6)
+
## Df Sum Sq Mean Sq F value Pr(>F)
## Group 1 13.5017 13.50170 8.57629 0.016797 *
## Residuals 9 14.1687 1.57431
@@ -1646,7 +1669,7 @@ 28.3.3 分組變量的平方和
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 1 observation deleted due to missingness
其實這跟之前的 anova(Model)
給出的結果完全一致。
-bartlett.test(Age ~ Group, data=Walk)
+
##
## Bartlett test of homogeneity of variances
##
@@ -1794,7 +1817,7 @@ 28.3.4 簡單模型的分組變
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/Bayes-Definition.html b/docs/Bayes-Definition.html
index 0d39e00fc..39bb49159 100644
--- a/docs/Bayes-Definition.html
+++ b/docs/Bayes-Definition.html
@@ -6,7 +6,7 @@
第 2 章 Bayes 貝葉斯理論的概念 | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
38.1 定義
lrt
的估計回歸參數發生了怎樣的變化?lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。lrt
也加入進隨機效應部分。schgend
改成 mean_girl
會給出怎樣的結果呢?gcse
,解釋變量是 lrt
。Fixed_reml2
模型中去,結果有發生顯著的變化嗎?school
level)的殘差。lrt
的估計回歸參數發生了怎樣的變化?lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。lrt
也加入進隨機效應部分。schgend
改成 mean_girl
會給出怎樣的結果呢?gcse
,解釋變量是 lrt
。Fixed_reml2
模型中去,結果有發生顯著的變化嗎?school
level)的殘差。許多時候,我們需要將概率中的條件相互對調。 例如: 在已知該人羣中有20%的人有吸菸習慣(\(P(S)\)),吸菸的人有9%的概率有哮喘(\(P(A|S)\)),不吸菸的人有7%的概率有哮喘(\(P(A|\bar{S})\))的前提下,有個人前來門診,發現是哮喘患者,那麼這個人有多大的概率是一個菸民?也就是要求 \(P(S|A)\)
+許多時候,我們需要將概率中的條件相互對調。 +例如: +在已知該人羣中有20%的人有吸菸習慣(\(P(S)\)),吸菸的人有9%的概率有哮喘(\(P(A|S)\)),不吸菸的人有7%的概率有哮喘(\(P(A|\bar{S})\))的前提下,有個人前來門診,發現是哮喘患者,那麼這個人有多大的概率是一個菸民?也就是要求 \(P(S|A)\)
這裏先引入貝葉斯的概念:
-我們可以將 \(P(A\cap S)\) 寫成: \[P(A\cap S)=P(A|S)P(S)\\or\\ -P(A\cap S)=P(S|A)P(A)\] 這兩個等式是完全等價的。我們將他們連起來:
+我們可以將 \(P(A\cap S)\) 寫成: +\[P(A\cap S)=P(A|S)P(S)\\or\\ +P(A\cap S)=P(S|A)P(A)\] +這兩個等式是完全等價的。我們將他們連起來:
\[P(S|A)P(A)=P(A|S)P(S)\\ \Rightarrow P(S|A)=\frac{P(A|S)P(S)}{P(A)}\]
-是不是看起來又像是寫了一堆廢話? 沒錯,你看出來是一堆廢話的時候,證明你也同意這背後的簡單邏輯。
+是不是看起來又像是寫了一堆廢話? +沒錯,你看出來是一堆廢話的時候,證明你也同意這背後的簡單邏輯。
再繼續,我們可以利用另外一個廢話:
\[
\because S+\bar{S}=1\\
@@ -1319,11 +1347,11 @@ \[P(S|A)=\frac{P(A|S)P(S)}{P(A|S)P(S)+P(A|\bar{S})P(\bar{S})}\] 回到上面說到的哮喘人中有多少比例吸菸的問題。可以繼續使用概率樹來方便的計算: \[\begin{align}
P(S|A) &= \frac{P(A|S)P(S)}{P(A|S)P(S)+P(A|\bar{S})P(\bar{S})} \\
&= \frac{0.09\times0.2}{0.09\times0.2+0.07\times0.8} \\
&= 0.24
-\end{align}\]
+\end{align}\]第 2 章 Bayes 貝葉斯理論
所以我們的結論就是,在已知該人羣中有20%的人有吸菸習慣(\(P(S)\)),吸菸的人有9%的概率有哮喘(\(P(A|S)\)),不吸菸的人有7%的概率有哮喘(\(P(A|\bar{S})\))的前提下,有個人前來門診,發現是哮喘患者,那麼這個人有24% 的概率是一個菸民(\(P(S|A)\))。
lrt
的估計回歸參數發生了怎樣的變化?lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。lrt
也加入進隨機效應部分。schgend
改成 mean_girl
會給出怎樣的結果呢?gcse
,解釋變量是 lrt
。Fixed_reml2
模型中去,結果有發生顯著的變化嗎?school
level)的殘差。lrt
的估計回歸參數發生了怎樣的變化?lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。lrt
也加入進隨機效應部分。schgend
改成 mean_girl
會給出怎樣的結果呢?gcse
,解釋變量是 lrt
。Fixed_reml2
模型中去,結果有發生顯著的變化嗎?school
level)的殘差。可以發現在兩者和的方差公式展開之後多了一部分 \(E[(X-E(X))(Y-E(Y))]\)。 這個多出來的一部分就說明了二者 \((X, Y)\) 之間的關係。它被定義爲協方差 (Covariance): \[Cov(X,Y) = E[(X-E(X))(Y-E(Y))]\]
+可以發現在兩者和的方差公式展開之後多了一部分 \(E[(X-E(X))(Y-E(Y))]\)。 這個多出來的一部分就說明了二者 \((X, Y)\) 之間的關係。它被定義爲協方差 (Covariance): +\[Cov(X,Y) = E[(X-E(X))(Y-E(Y))]\]
所以:
\[Var(X+Y)=Var(X)+Var(Y)+2Cov(X,Y)\]
要記住,協方差只能用於評價\(X,Y\)之間的線性關係 (Linear Association)。
@@ -1339,7 +1363,8 @@偉大的中心極限定理告訴我們:
當樣本量足夠大時,樣本均值 \(\bar{X}_n\) 的分佈爲正態分佈,這個特性與樣本來自的人羣的分佈 \(X_i\) 無關。
再說一遍:
-如果對象是獨立同分佈 i.i.d (identically and independently distributed)。那麼它的總體期望和方差分別是: \(E(X)=\mu;\;Var(X)=\sigma^2\)。 根據中心極限定理,可以得到:
+如果對象是獨立同分佈 i.i.d (identically and independently distributed)。那麼它的總體期望和方差分別是: \(E(X)=\mu;\;Var(X)=\sigma^2\)。 +根據中心極限定理,可以得到:
中心極限定理告訴我們,當樣本量足夠大時: \[X\sim N(n\pi, n\pi(1-\pi))\]
中心極限定理告訴我們,當樣本量足夠大時: +\[X\sim N(n\pi, n\pi(1-\pi))\]
那麼原先的 \(X\) 可以被視爲是將這無數的小時間段的 \(X_i\) 相加。應用中心極限定理: \[X=\sum_{i=1}^nX_i\sim N(\frac{n\mu}{n}, \frac{n\mu}{n})\]
那麼原先的 \(X\) 可以被視爲是將這無數的小時間段的 \(X_i\) 相加。應用中心極限定理: +\[X=\sum_{i=1}^nX_i\sim N(\frac{n\mu}{n}, \frac{n\mu}{n})\]
需要注意的是,這段時間 (\(t\)) 內發生的事件次數 (\(\lambda\)) : \(\lambda t =\mu>10\) ,這樣的正態分佈模擬才能成立。
# 快來看實際用傻瓜算法計算獲得的概率:
-1-pbinom(60, size=100, prob=0.5)
## [1] 0.0176
-# 快來看用中心極限定理模擬正態分佈獲得的概率:
-1-pnorm((60.5-50)/sqrt(25))
## [1] 0.01786
# 快來看實際用傻瓜算法計算獲得的概率:
-pbinom(38, size=48, prob=0.75)-pbinom(30, size=48, prob=0.75)
## [1] 0.7578
-# 快來看用中心極限定理模擬正態分佈獲得的概率:
-pnorm((38.5-36)/sqrt(9)) - pnorm((30.5-36)/sqrt(9))
## [1] 0.7643
# 快來看實際用傻瓜算法計算獲得的概率:
-ppois(20, lambda=30)
## [1] 0.03528
-# 快來看用中心極限定理模擬正態分佈獲得的概率:
-pnorm((20.5-30)/sqrt(30))
## [1] 0.04142
這兩個其實有些小差距。不過看下圖,其模擬還是很到位的。只是正態分佈的面積明顯確實比泊松分佈的小柱子面積要大一些。
# 快來看實際用傻瓜算法計算獲得的概率:
-ppois(40, lambda=60)
## [1] 0.003983
-# 快來看用中心極限定理模擬正態分佈獲得的概率:
-pnorm((40.5-60)/sqrt(60))
## [1] 0.005911
假定 \(X_1, X_2\) 是兩個連續隨機變量: \[
+ 假定 \(X_1, X_2\) 是兩個連續隨機變量:
+\[
E(X_1)=\mu_1, Var(X_1)=\sigma_1^2 \\
- E(X_2)=\mu_2, Var(X_2)=\sigma_2^2 \\
- Corr(X_1, X_2)=\rho \Rightarrow Cov(X_1, X_2)=\rho\sigma_1\sigma_2=\sigma_{12}
+E(X_2)=\mu_2, Var(X_2)=\sigma_2^2 \\
+Corr(X_1, X_2)=\rho \Rightarrow Cov(X_1, X_2)=\rho\sigma_1\sigma_2=\sigma_{12}
\] 利用矩陣的標記法,可以將 \(X_1, X_2\) 標記爲 \(\textbf{X}=(X_1, X_2)^T\), 即:
那麼這兩個變量的邊緣分佈 (marginal distribution) 也服從正態分佈: \[X_1\sim N(\mu_1,\sigma_1^2), X_2\sim N(\mu, \sigma_2^2)\]
那麼這兩個變量的邊緣分佈 (marginal distribution) 也服從正態分佈: +\[X_1\sim N(\mu_1,\sigma_1^2), X_2\sim N(\mu, \sigma_2^2)\]
反之亦然。
上面的概念過於抽象,用血壓的例子:
收縮期血壓和舒張期血壓各自服從正態分佈。那麼可以用上面的概念來寫出已知舒張期血壓時,收縮期血壓的分佈。
條件期望: \[E(\text{SBP|DBP})=130+\frac{0.75\times15}{10}(\text{DBP}-90)\]
實際如果來了一個病人,他說他只記得自己測的舒張期血壓是95:
他的收縮期血壓的期望值就可以用上面的式子計算: \[E(\text{SBP|DBP}=95)=136\]
條件方差爲: \[Var(\text{SBP|DBP})=15^2(1-0.75^2)=98.4\approx9.92^2<15^2\]
條件期望: +\[E(\text{SBP|DBP})=130+\frac{0.75\times15}{10}(\text{DBP}-90)\]
實際如果來了一個病人,他說他只記得自己測的舒張期血壓是95:
+他的收縮期血壓的期望值就可以用上面的式子計算:
+\[E(\text{SBP|DBP}=95)=136\]
條件方差爲: +\[Var(\text{SBP|DBP})=15^2(1-0.75^2)=98.4\approx9.92^2<15^2\]
所以當我們知道了這個人的一部分信息以後,推測他的另一個相關連的變量變得更加準確(方差變小)了。
lrt
的估計回歸參數發生了怎樣的變化?lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。lrt
也加入進隨機效應部分。schgend
改成 mean_girl
會給出怎樣的結果呢?gcse
,解釋變量是 lrt
。Fixed_reml2
模型中去,結果有發生顯著的變化嗎?school
level)的殘差。lrt
的估計回歸參數發生了怎樣的變化?lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。lrt
也加入進隨機效應部分。schgend
改成 mean_girl
會給出怎樣的結果呢?gcse
,解釋變量是 lrt
。Fixed_reml2
模型中去,結果有發生顯著的變化嗎?school
level)的殘差。這個表中的人造數據,其結構是一目了然的,它的第二層級數量是 5,每層的個體數量是 2。這是一個平衡數據。由於這是個我們人爲模擬的數據,圖 58.7 也顯示它沒有隨機誤差,所有數據都在各自的直線上。
-dt <- read.csv("backupfiles/hierexample.csv", header = T)
-names(dt) <- c("Cluster", "id", "X", "Y", "Xbar", "Ybar")
-dt$Cluster <- as.factor(dt$Cluster)
-ggthemr('fresh')
-
-ggplot(dt, aes(x = X, y = Y, shape = Cluster, colour = Cluster)) + geom_point(size =5) +
- theme(axis.text = element_text(size = 15),
- axis.text.x = element_text(size = 15),
- axis.text.y = element_text(size = 15)) +
- labs(x = "X", y = "Y") +
- theme(axis.title = element_text(size = 17), axis.text = element_text(size = 8)) +
-theme(legend.position = "bottom", legend.direction = "horizontal") + theme(legend.text = element_text(size = 19), legend.title = element_text(size = 19))
dt <- read.csv("backupfiles/hierexample.csv", header = T)
+names(dt) <- c("Cluster", "id", "X", "Y", "Xbar", "Ybar")
+dt$Cluster <- as.factor(dt$Cluster)
+ggthemr('fresh')
+
+ggplot(dt, aes(x = X, y = Y, shape = Cluster, colour = Cluster)) + geom_point(size =5) +
+ theme(axis.text = element_text(size = 15),
+ axis.text.x = element_text(size = 15),
+ axis.text.y = element_text(size = 15)) +
+ labs(x = "X", y = "Y") +
+ theme(axis.title = element_text(size = 17), axis.text = element_text(size = 8)) +
+theme(legend.position = "bottom", legend.direction = "horizontal") + theme(legend.text = element_text(size = 19), legend.title = element_text(size = 19))
\[ \hat Y_{ij} = 5.33 - 0.33 X_{ij} \]
-ggthemr('fresh')
-
-ggplot(dt, aes(x = X, y = Y)) + geom_point(size = 5, shape = 23) +
- geom_smooth(method = "lm", se = FALSE, linetype = 2) +
- theme(axis.text = element_text(size = 15),
- axis.text.x = element_text(size = 15),
- axis.text.y = element_text(size = 15)) +
- labs(x = "X", y = "Y") +
- theme(axis.title = element_text(size = 17), axis.text = element_text(size = 8)) +
-theme(legend.position = "bottom", legend.direction = "horizontal") + theme(legend.text = element_text(size = 19), legend.title = element_text(size = 19))
ggthemr('fresh')
+
+ggplot(dt, aes(x = X, y = Y)) + geom_point(size = 5, shape = 23) +
+ geom_smooth(method = "lm", se = FALSE, linetype = 2) +
+ theme(axis.text = element_text(size = 15),
+ axis.text.x = element_text(size = 15),
+ axis.text.y = element_text(size = 15)) +
+ labs(x = "X", y = "Y") +
+ theme(axis.title = element_text(size = 17), axis.text = element_text(size = 8)) +
+theme(legend.position = "bottom", legend.direction = "horizontal") + theme(legend.text = element_text(size = 19), legend.title = element_text(size = 19))
\[ \hat{\bar{Y}}_j = 8.0 - 1.0 \bar{X}_j \]
-ggthemr('fresh')
-
-ggplot(dt, aes(x = X, y = Y)) + geom_point(size =5, shape=23) +
- geom_smooth(method = "lm", se = FALSE, linetype = 2) +
- geom_abline(intercept = 8, slope = -1) +
- geom_point(aes(x = Xbar, y=Ybar, size = 5)) +
- theme(axis.text = element_text(size = 15),
- axis.text.x = element_text(size = 15),
- axis.text.y = element_text(size = 15)) +
- labs(x = "X", y = "Y") +
- theme(axis.title = element_text(size = 17), axis.text = element_text(size = 8)) + theme(legend.position="none")
ggthemr('fresh')
+
+ggplot(dt, aes(x = X, y = Y)) + geom_point(size =5, shape=23) +
+ geom_smooth(method = "lm", se = FALSE, linetype = 2) +
+ geom_abline(intercept = 8, slope = -1) +
+ geom_point(aes(x = Xbar, y=Ybar, size = 5)) +
+ theme(axis.text = element_text(size = 15),
+ axis.text.x = element_text(size = 15),
+ axis.text.y = element_text(size = 15)) +
+ labs(x = "X", y = "Y") +
+ theme(axis.title = element_text(size = 17), axis.text = element_text(size = 8)) + theme(legend.position="none")
生物學悖論常見於我們認爲某分層數據中層級變量之間的關系,同樣適用與層級中的個體之間: 例如比較 A 國 和 B 國之間心血管疾病的發病率,發現 A 國國民食鹽平均攝入量高於 B 國,很多人可能就會下結論說食鹽攝入量高的個體,心血管疾病發病的危險度較高。然而,這樣的推論很多時候是錯誤的。
-曾經在 (W. S. Robinson 1950) 論文中舉過的著名例子: 該研究調查美國每個州的移民比例,和該州相應的識字率之間的關系。研究者發現,移民比例較高的州,其識字率也較高 (相關系數 0.53)。由此就有人下結論說移民越多,那個州的教育水平會比較高。但是實際情況是,把每個個體的受教育水平和該個體本身是不是移民做了相關系數分析之後發現,這個關系其實是負相關 (-0.11)。所以說在州的水平作出的統計學推斷-移民多的州受教育水平高-是不正確的。之所以在州水平發現移民比例和受教育水平之間的正關系,是因爲移民傾向於居住在教育水平本來就比較高的本土出生美國人的州。
+曾經在 (Robinson 1950) 論文中舉過的著名例子: 該研究調查美國每個州的移民比例,和該州相應的識字率之間的關系。研究者發現,移民比例較高的州,其識字率也較高 (相關系數 0.53)。由此就有人下結論說移民越多,那個州的教育水平會比較高。但是實際情況是,把每個個體的受教育水平和該個體本身是不是移民做了相關系數分析之後發現,這個關系其實是負相關 (-0.11)。所以說在州的水平作出的統計學推斷-移民多的州受教育水平高-是不正確的。之所以在州水平發現移民比例和受教育水平之間的正關系,是因爲移民傾向於居住在教育水平本來就比較高的本土出生美國人的州。
其中 \(j\) 是所屬層級編號。該模型中的 \(\varepsilon_{ij}\) 被認爲服從均值爲零,方差爲 \(\sigma_{\varepsilon}^2\) 的正態分布。該模型也可以簡寫爲:
\[ Y_{ij} = \alpha_j + \beta_1X_{ij} + \epsilon_{ij} -\] 一樣一預案 這樣的模型在等級線性回歸模型中被認爲是固定效應模型 fixed effect model。它其實是默認給五個層級五個不同的截距,每層內部 \(X,Y\) 之間的關系 (斜率) 則被認爲是完全相同的 (namely the within cluster models are the same)。
+\] +一樣一預案 +這樣的模型在等級線性回歸模型中被認爲是固定效應模型 fixed effect model。它其實是默認給五個層級五個不同的截距,每層內部 \(X,Y\) 之間的關系 (斜率) 則被認爲是完全相同的 (namely the within cluster models are the same)。本課剛開始的例子中有個數據是來自 6 所不同醫院 72 名患者的收縮期血壓的數據。我們現在來分析這些人中血壓和年齡之間的關系。下面的散點圖重現了六所醫院的72名患者的血壓和年齡。
## Warning: New theme missing the following elements: axis.ticks.length.x, axis.ticks.length.x.top,
## axis.ticks.length.x.bottom, axis.ticks.length.y, axis.ticks.length.y.left, axis.ticks.length.y.right
@@ -1941,15 +1966,15 @@ 下面在 R 裏擬合這個固定效應模型:
-Bp <- read_dta("backupfiles/bp.dta")
-
-Bp$hosp <- as.factor(Bp$hosp)
-Bp <- Bp %>%
- mutate(c_age = age - mean(age))
-# 通過指定截距爲零,獲取每個醫院的回歸線的截距
-Model0 <- lm(bp ~ 0 + c_age + hosp, data = Bp)
-
-summary(Model0)
Bp <- read_dta("backupfiles/bp.dta")
+
+Bp$hosp <- as.factor(Bp$hosp)
+Bp <- Bp %>%
+ mutate(c_age = age - mean(age))
+# 通過指定截距爲零,獲取每個醫院的回歸線的截距
+Model0 <- lm(bp ~ 0 + c_age + hosp, data = Bp)
+
+summary(Model0)
##
## Call:
## lm(formula = bp ~ 0 + c_age + hosp, data = Bp)
@@ -1973,11 +1998,11 @@ 58.4.5 固定效應模型 fixed e
## Residual standard error: 19.136 on 65 degrees of freedom
## Multiple R-squared: 0.97954, Adjusted R-squared: 0.97733
## F-statistic: 444.46 on 7 and 65 DF, p-value: < 2.22e-16
-# 先生成一個新的醫院變量 hops1 = 1。然後使用偏 F 檢驗法
-# 檢驗控制了患者的年齡以後,這六所醫院的截距是否各自不相同。
-Bp$hosp1 <- Bp$hosp[1]
-mod2 <- lm(bp ~ 0 + c_age + as.numeric(hosp1), data = Bp)
-anova(Model0, mod2)
# 先生成一個新的醫院變量 hops1 = 1。然後使用偏 F 檢驗法
+# 檢驗控制了患者的年齡以後,這六所醫院的截距是否各自不相同。
+Bp$hosp1 <- Bp$hosp[1]
+mod2 <- lm(bp ~ 0 + c_age + as.numeric(hosp1), data = Bp)
+anova(Model0, mod2)
## Analysis of Variance Table
##
## Model 1: bp ~ 0 + c_age + hosp
@@ -2022,28 +2047,28 @@ 58.6.2 問題
hsb_selected <- read_dta("backupfiles/hsb_selected.dta")
-length(unique(hsb_selected$schoolid)) ## number of school = 160
hsb_selected <- read_dta("backupfiles/hsb_selected.dta")
+length(unique(hsb_selected$schoolid)) ## number of school = 160
## [1] 160
-## create a subset data with only the first observation of each school
-hsb <- hsb_selected[!duplicated(hsb_selected$schoolid), ]
-
-## about 44 % of the schools are Catholic schools
-with(hsb, tab1(sector, graph = FALSE, decimal = 2))
## create a subset data with only the first observation of each school
+hsb <- hsb_selected[!duplicated(hsb_selected$schoolid), ]
+
+## about 44 % of the schools are Catholic schools
+with(hsb, tab1(sector, graph = FALSE, decimal = 2))
## sector :
## Frequency Percent Cum. percent
## 0 90 56.25 56.25
## 1 70 43.75 100.00
## Total 160 100.00 100.00
-## among all the pupils, about 53% are females
-with(hsb_selected, tab1(female, graph = FALSE, decimal = 2))
## among all the pupils, about 53% are females
+with(hsb_selected, tab1(female, graph = FALSE, decimal = 2))
## female :
## Frequency Percent Cum. percent
## 0 3390 47.18 47.18
## 1 3795 52.82 100.00
## Total 7185 100.00 100.00
-## among all the pupils, about 27.5% are from ethnic minorities
-with(hsb_selected, tab1(minority, graph = FALSE, decimal = 2))
## among all the pupils, about 27.5% are from ethnic minorities
+with(hsb_selected, tab1(minority, graph = FALSE, decimal = 2))
## minority :
## Frequency Percent Cum. percent
## 0 5211 72.53 72.53
@@ -2052,11 +2077,11 @@ 58.6.3 將 High-School-and-Beyond
hsb5 <- subset(hsb_selected, schoolid < 1320)
-Mean_ses_math <- ddply(hsb5,~schoolid,summarise,mean_ses=mean(ses),mean_math=mean(mathach))
-## the mean SES score ranges from -0.4255 to +0.5280
-## the mean Maths score ranges from 7.636 to 16.255
-Mean_ses_math
hsb5 <- subset(hsb_selected, schoolid < 1320)
+Mean_ses_math <- ddply(hsb5,~schoolid,summarise,mean_ses=mean(ses),mean_math=mean(mathach))
+## the mean SES score ranges from -0.4255 to +0.5280
+## the mean Maths score ranges from 7.636 to 16.255
+Mean_ses_math
## schoolid mean_ses mean_math
## 1 1224 -0.43438298 9.7154468
## 2 1288 0.12159999 13.5108000
@@ -2066,27 +2091,27 @@ 58.6.4 爲了簡便起見,接
## plot the scatter of mathach and ses among these 5 schools
-
-ggplot(hsb5, aes(x = ses, y = mathach)) + geom_point() +
- theme_bw() +
- theme(axis.text = element_text(size = 15),
- axis.text.x = element_text(size = 15),
- axis.text.y = element_text(size = 15)) +
- labs(x = "SES", y = "Math achievement") +
- xlim(-2.05, 2.05)+
- ylim(-10, 30) +
- theme(axis.title = element_text(size = 17), axis.text = element_text(size = 8))
## plot the scatter of mathach and ses among these 5 schools
+
+ggplot(hsb5, aes(x = ses, y = mathach)) + geom_point() +
+ theme_bw() +
+ theme(axis.text = element_text(size = 15),
+ axis.text.x = element_text(size = 15),
+ axis.text.y = element_text(size = 15)) +
+ labs(x = "SES", y = "Math achievement") +
+ xlim(-2.05, 2.05)+
+ ylim(-10, 30) +
+ theme(axis.title = element_text(size = 17), axis.text = element_text(size = 8))
Total_reg <- lm(mathach ~ ses, data = hsb5)
-## the total regression model gives an estimated regression coefficient for the SES
-## of each pupil equal to 3.31 (SE=0.66)
-summary(Total_reg)
Total_reg <- lm(mathach ~ ses, data = hsb5)
+## the total regression model gives an estimated regression coefficient for the SES
+## of each pupil equal to 3.31 (SE=0.66)
+summary(Total_reg)
##
## Call:
## lm(formula = mathach ~ ses, data = hsb5)
@@ -2105,14 +2130,14 @@ 58.6.5 先無視掉學校這一
## Residual standard error: 6.4708 on 186 degrees of freedom
## Multiple R-squared: 0.11886, Adjusted R-squared: 0.11412
## F-statistic: 25.09 on 1 and 186 DF, p-value: 1.2667e-06
-hsb5$Pred_T <- Total_reg$fitted.values # save the fitted values to the dataset
Btw_reg <- lm(mean_math ~ mean_ses, data = Mean_ses_math)
-## the regression model for the school level variables (between model) gives
-## an estimated regression coefficient of 7.29 (SE=1.41)
-summary(Btw_reg)
Btw_reg <- lm(mean_math ~ mean_ses, data = Mean_ses_math)
+## the regression model for the school level variables (between model) gives
+## an estimated regression coefficient of 7.29 (SE=1.41)
+summary(Btw_reg)
##
## Call:
## lm(formula = mean_math ~ mean_ses, data = Mean_ses_math)
@@ -2131,33 +2156,33 @@ 58.6.6 用各個學校 SES 和數
## Residual standard error: 1.2418 on 3 degrees of freedom
## Multiple R-squared: 0.89949, Adjusted R-squared: 0.86598
## F-statistic: 26.847 on 1 and 3 DF, p-value: 0.013956
-Mean_ses_math$Pred_B <- Btw_reg$fitted.values # save the fitted values to the dataset
Within_schl1 <- lm(mathach ~ ses, data = hsb5[hsb5$schoolid == 1224,])
-Within_schl2 <- lm(mathach ~ ses, data = hsb5[hsb5$schoolid == 1288,])
-Within_schl3 <- lm(mathach ~ ses, data = hsb5[hsb5$schoolid == 1296,])
-Within_schl4 <- lm(mathach ~ ses, data = hsb5[hsb5$schoolid == 1308,])
-Within_schl5 <- lm(mathach ~ ses, data = hsb5[hsb5$schoolid == 1317,])
-# the within school regressions gives estimated slopes which have a mean of 1.65
-# and which ranges between 0.126 and 3.255
-summary(c(Within_schl1$coefficients[2], Within_schl2$coefficients[2],
- Within_schl3$coefficients[2], Within_schl4$coefficients[2],
- Within_schl5$coefficients[2]))
Within_schl1 <- lm(mathach ~ ses, data = hsb5[hsb5$schoolid == 1224,])
+Within_schl2 <- lm(mathach ~ ses, data = hsb5[hsb5$schoolid == 1288,])
+Within_schl3 <- lm(mathach ~ ses, data = hsb5[hsb5$schoolid == 1296,])
+Within_schl4 <- lm(mathach ~ ses, data = hsb5[hsb5$schoolid == 1308,])
+Within_schl5 <- lm(mathach ~ ses, data = hsb5[hsb5$schoolid == 1317,])
+# the within school regressions gives estimated slopes which have a mean of 1.65
+# and which ranges between 0.126 and 3.255
+summary(c(Within_schl1$coefficients[2], Within_schl2$coefficients[2],
+ Within_schl3$coefficients[2], Within_schl4$coefficients[2],
+ Within_schl5$coefficients[2]))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.12602 1.07596 1.27391 1.64799 2.50858 3.25545
-# the SEs ranging between 1.21 and 3.00
-summary(c(summary(Within_schl1)$coefficients[4],
- summary(Within_schl2)$coefficients[4],
- summary(Within_schl3)$coefficients[4],
- summary(Within_schl4)$coefficients[4],
- summary(Within_schl5)$coefficients[4]))
# the SEs ranging between 1.21 and 3.00
+summary(c(summary(Within_schl1)$coefficients[4],
+ summary(Within_schl2)$coefficients[4],
+ summary(Within_schl3)$coefficients[4],
+ summary(Within_schl4)$coefficients[4],
+ summary(Within_schl5)$coefficients[4]))
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.2090 1.4359 1.7652 1.8987 2.0797 3.0034
-hsb5$Pred_W <- c(Within_schl1$fitted.values, Within_schl2$fitted.values,
- Within_schl3$fitted.values, Within_schl4$fitted.values,
- Within_schl5$fitted.values) ## save the predicted value into the dataset
Mean <- Mean_ses_math[, 1:3]
-names(Mean) <- c("schoolid", "ses", "Pred_W")
-
-
-ggplot(hsb5, aes(x = ses, y = Pred_W, group = schoolid)) +
- geom_line(linetype = 2, size = 1) +
- geom_abline(intercept = Total_reg$coefficients[1], slope = Total_reg$coefficients[2],
- colour = "dark blue") +
- geom_abline(intercept = Btw_reg$coefficients[1], slope = Btw_reg$coefficients[2],
- colour = "red") +
- geom_point(data = Mean, shape = 17, size = 4, colour = "Red") +
- theme_bw() +
- theme(axis.text = element_text(size = 15),
- axis.text.x = element_text(size = 15),
- axis.text.y = element_text(size = 15)) +
- labs(x = "SES", y = "Fitted regression lines (Maths achievement)") +
- xlim(-2.05, 2.05)+
- ylim(5, 20) +
- theme(axis.title = element_text(size = 17), axis.text = element_text(size = 8)) +
- theme(plot.caption = element_text(size = 12,
- hjust = 0)) + labs(caption = "Black dash line: Within regression model;
-Blue solid line: Total regression model;
-Red solid line: Between regression model;
-Red triangle: School mean values")
Mean <- Mean_ses_math[, 1:3]
+names(Mean) <- c("schoolid", "ses", "Pred_W")
+
+
+ggplot(hsb5, aes(x = ses, y = Pred_W, group = schoolid)) +
+ geom_line(linetype = 2, size = 1) +
+ geom_abline(intercept = Total_reg$coefficients[1], slope = Total_reg$coefficients[2],
+ colour = "dark blue") +
+ geom_abline(intercept = Btw_reg$coefficients[1], slope = Btw_reg$coefficients[2],
+ colour = "red") +
+ geom_point(data = Mean, shape = 17, size = 4, colour = "Red") +
+ theme_bw() +
+ theme(axis.text = element_text(size = 15),
+ axis.text.x = element_text(size = 15),
+ axis.text.y = element_text(size = 15)) +
+ labs(x = "SES", y = "Fitted regression lines (Maths achievement)") +
+ xlim(-2.05, 2.05)+
+ ylim(5, 20) +
+ theme(axis.title = element_text(size = 17), axis.text = element_text(size = 8)) +
+ theme(plot.caption = element_text(size = 12,
+ hjust = 0)) + labs(caption = "Black dash line: Within regression model;
+Blue solid line: Total regression model;
+Red solid line: Between regression model;
+Red triangle: School mean values")
Fixed_reg <- lm(mathach ~ ses + factor(schoolid), data = hsb5)
-
-## Fitting a fixed effect model to these data is equivalent to forcing
-## a common slope onto the five within regression models. It gives an
-## estimated slope of 1.789 (SE=0.76), close to their average of 1.64799.
-## Note that controlling for female, minority, and sector but not for
-## schoolid leads to roughly the same estimate (slope = 1.68, SE=0.75)
-
-summary(Fixed_reg)
Fixed_reg <- lm(mathach ~ ses + factor(schoolid), data = hsb5)
+
+## Fitting a fixed effect model to these data is equivalent to forcing
+## a common slope onto the five within regression models. It gives an
+## estimated slope of 1.789 (SE=0.76), close to their average of 1.64799.
+## Note that controlling for female, minority, and sector but not for
+## schoolid leads to roughly the same estimate (slope = 1.68, SE=0.75)
+
+summary(Fixed_reg)
##
## Call:
## lm(formula = mathach ~ ses + factor(schoolid), data = hsb5)
@@ -2233,7 +2258,7 @@ 58.6.10 用這 5 個學校的數
## Residual standard error: 6.2362 on 182 degrees of freedom
## Multiple R-squared: 0.1992, Adjusted R-squared: 0.1772
## F-statistic: 9.0544 on 5 and 182 DF, p-value: 1.0512e-07
-summary(lm(mathach ~ ses + female + minority + sector, data = hsb5))
##
## Call:
## lm(formula = mathach ~ ses + female + minority + sector, data = hsb5)
@@ -2258,9 +2283,9 @@ 58.6.10 用這 5 個學校的數
pefr <- read_dta("backupfiles/pefr.dta")
-# the data are in wide format
-pefr
## # A tibble: 17 x 5
## id wp1 wp2 wm1 wm2
## <dbl> <dbl> <dbl> <dbl> <dbl>
@@ -2281,13 +2306,13 @@ 58.6.11 讀入 PEFR 數據。
-# transform data into long format
-pefr_long <- pefr %>%
- gather(key, value, -id) %>%
- separate(key, into = c("measurement", "occasion"), sep = 2) %>%
- arrange(id, occasion) %>%
- spread(measurement, value)
-pefr_long
# transform data into long format
+pefr_long <- pefr %>%
+ gather(key, value, -id) %>%
+ separate(key, into = c("measurement", "occasion"), sep = 2) %>%
+ arrange(id, occasion) %>%
+ spread(measurement, value)
+pefr_long
## # A tibble: 34 x 4
## id occasion wm wp
## <dbl> <chr> <dbl> <dbl>
@@ -2302,21 +2327,21 @@ 58.6.11 讀入 PEFR 數據。
-## figure shows slightly closer agreement between the repeated measures of standard Wright,
-## than between those of Mini Wright
-
-ggplot(pefr_long, aes(x = id, y = wp, fill = occasion)) +
- geom_point(size = 4, shape = 21) +
- geom_hline(yintercept = mean(pefr_long$wp), colour = "red") +
- theme_bw() +
- scale_x_continuous(breaks = 1:17)+
- theme(axis.text = element_text(size = 15),
- axis.text.x = element_text(size = 15),
- axis.text.y = element_text(size = 15)) +
- labs(x = "Subject ID", y = "W Measurements") +
- theme(axis.title = element_text(size = 17), axis.text = element_text(size = 8))+
- theme(legend.text = element_text(size = 19),
- legend.title = element_text(size = 19))
## figure shows slightly closer agreement between the repeated measures of standard Wright,
+## than between those of Mini Wright
+
+ggplot(pefr_long, aes(x = id, y = wp, fill = occasion)) +
+ geom_point(size = 4, shape = 21) +
+ geom_hline(yintercept = mean(pefr_long$wp), colour = "red") +
+ theme_bw() +
+ scale_x_continuous(breaks = 1:17)+
+ theme(axis.text = element_text(size = 15),
+ axis.text.x = element_text(size = 15),
+ axis.text.y = element_text(size = 15)) +
+ labs(x = "Subject ID", y = "W Measurements") +
+ theme(axis.title = element_text(size = 17), axis.text = element_text(size = 8))+
+ theme(legend.text = element_text(size = 19),
+ legend.title = element_text(size = 19))
wp
兩次測量平均值# the means range from 171.5 to 644.5
-
-with(pefr_long, summ(wp, by = id, graph = FALSE))
## For id = 1
## obs. mean median s.d. min. max.
## 2 492 492 2.828 490 494
@@ -2399,7 +2424,7 @@ 58.6.12 求每個患者的
wp
變異。再用 lme4::lmer
擬合用 id
作隨機效應的混合效應模型。確認後者報告的 Std.Dev for id effect
其實可以用 ANOVA 結果的 \(\sqrt{\frac{\text{MMS-MSE}}{n}}\) (n 是每個個體重複測量值的個數)。with(pefr_long, anova(lm(wp~factor(id))))
## Analysis of Variance Table
##
## Response: wp
@@ -2408,8 +2433,8 @@ 58.6.13 在 R 裏先用 ANOVA 分
## Residuals 17 3983 234.29
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
-#library(lme4)
-( fit <- lmer(wp ~ (1|id), data=pefr_long) )
## Linear mixed model fit by REML ['lmerModLmerTest']
## Formula: wp ~ (1 | id)
## Data: pefr_long
@@ -2422,20 +2447,20 @@ 58.6.13 在 R 裏先用 ANOVA 分
## Fixed Effects:
## (Intercept)
## 447.88
-sqrt((27600 - 234)/2)
## [1] 116.97436
wp
,解釋變量爲 id
的簡單線性迴歸模型。用數學表達式描述這個模型。Reg <- lm(wp ~ factor(id), data = pefr_long)
-
-# The fixed effect regression model leads to the same ANOVA
-# table. To the same estimate of the residual SD = (15.307)
-# However, it does not give an estimate of the "SD of id effect"
-# Instead it gives estimates of mean PEFR for participant number 1
-# = 492 and estimates of the difference in means from him/her
-# for all the other 16 pariticipants
-anova(Reg)
Reg <- lm(wp ~ factor(id), data = pefr_long)
+
+# The fixed effect regression model leads to the same ANOVA
+# table. To the same estimate of the residual SD = (15.307)
+# However, it does not give an estimate of the "SD of id effect"
+# Instead it gives estimates of mean PEFR for participant number 1
+# = 492 and estimates of the difference in means from him/her
+# for all the other 16 pariticipants
+anova(Reg)
## Analysis of Variance Table
##
## Response: wp
@@ -2444,7 +2469,7 @@ 58.6.14 擬合結果變量爲
-summary(Reg)
##
## Call:
## lm(formula = wp ~ factor(id), data = pefr_long)
@@ -2489,10 +2514,10 @@ 58.6.14 擬合結果變量爲
58.6.15 將 wp
中心化之後,重新擬合相同的模型,把截距去除掉。寫下這個模型的數學表達式。
-Reg1 <- lm((wp - mean(wp)) ~ 0 + factor(id), data = pefr_long)
-
-# it leads to the same ANOVA table again, same residual SD
-anova(Reg1)
+Reg1 <- lm((wp - mean(wp)) ~ 0 + factor(id), data = pefr_long)
+
+# it leads to the same ANOVA table again, same residual SD
+anova(Reg1)
## Analysis of Variance Table
##
## Response: (wp - mean(wp))
@@ -2501,7 +2526,7 @@ 58.6.15 將 wp
中
## Residuals 17 3983 234.29
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
-summary(Reg1)
+
##
## Call:
## lm(formula = (wp - mean(wp)) ~ 0 + factor(id), data = pefr_long)
@@ -2547,16 +2572,16 @@ 58.6.15 將 wp
中
58.6.16 計算這些迴歸係數 (其實是不同羣之間的隨機截距) 的均值和標準差。
-# the individual level intercepts have mean zero and SD = 117.47, larger than the estimated
-# Std.Dev for id effect.
-Reg1$coefficients
+# the individual level intercepts have mean zero and SD = 117.47, larger than the estimated
+# Std.Dev for id effect.
+Reg1$coefficients
## factor(id)1 factor(id)2 factor(id)3 factor(id)4 factor(id)5 factor(id)6 factor(id)7
## 44.117647 -51.882353 66.117647 -30.382353 25.117647 136.117647 -33.882353
## factor(id)8 factor(id)9 factor(id)10 factor(id)11 factor(id)12 factor(id)13 factor(id)14
## -11.382353 196.117647 -16.882353 -29.382353 196.617647 -176.882353 37.117647
## factor(id)15 factor(id)16 factor(id)17
## -276.382353 -50.382353 -23.882353
-summ(Reg1$coefficients, graph = FALSE)
+
## obs. mean median s.d. min. max.
## 17 0 -16.882 117.473 -276.382 196.618
@@ -2571,7 +2596,7 @@ References
Raudenbush, Stephen W, and Anthony S Bryk. 2002. Hierarchical Linear Models: Applications and Data Analysis Methods. Vol. 1. Sage.
Robinson, W. S. 1950. “Ecological Correlations and the Behavior of Individuals.” American Sociological Review 15 (3). [American Sociological Association, Sage Publications, Inc.]: 351–57. http://www.jstor.org/stable/2087176.
+Robinson, W. S. 1950. “Ecological Correlations and the Behavior of Individuals.” American Sociological Review 15 (3). [American Sociological Association, Sage Publications, Inc.]: 351–57. http://www.jstor.org/stable/2087176.
Snijders, Tom, and Roel Bosker. 1999. “Multilevel Analysis: An Introduction to Basic and Applied Multilevel Analysis.” London: Sage.
@@ -2638,7 +2663,7 @@lrt
的估計回歸參數發生了怎樣的變化?lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。lrt
也加入進隨機效應部分。schgend
改成 mean_girl
會給出怎樣的結果呢?gcse
,解釋變量是 lrt
。Fixed_reml2
模型中去,結果有發生顯著的變化嗎?school
level)的殘差。lrt
的估計回歸參數發生了怎樣的變化?lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。lrt
也加入進隨機效應部分。schgend
改成 mean_girl
會給出怎樣的結果呢?gcse
,解釋變量是 lrt
。Fixed_reml2
模型中去,結果有發生顯著的變化嗎?school
level)的殘差。均值是:
\[ \text{E}(Y) = n\frac{a}{a + b} \]
那麼我們在進行這項試驗時我們可以預測,20名患者中有療效人數出現的概率,我們將它的預測概率分布圖和先驗概率分布放在一起:
-# # Beta Binomial Predictive distribution function
-# BetaBinom <- Vectorize(function(rp){
-# log.val <- lchoose(np, rp) + lbeta(rp+a+r,b+n-r+np-rp) - lbeta(a+r,b+n-r)
-# return(exp(log.val))
-# })
-par(mfrow=c(1,2))
-
-pi <- Vectorize(function(theta) dbeta(theta, 9.2,13.8))
-curve(pi, xlab="Probabiltiy of response", ylab="Density", main="(a) Prior distribution Beta(9.2, 13.8)", frame=FALSE, lwd=2)
-
-# or we can use the built-in function for beta-binomial distribution in package TailRank:
-library(TailRank)
-N <- 20; a <- 9.2; b <- 13.8; y <- 0:N
-yy <- dbb(y, N, a, b)
-barplot(yy, xlab = "Number of successes", names.arg = 0:20, axes = F,
- ylim = c(0, 0.16), main = "(b) predictive distribution")
-axis(2,at=seq(0,0.16,0.02))
# # Beta Binomial Predictive distribution function
+# BetaBinom <- Vectorize(function(rp){
+# log.val <- lchoose(np, rp) + lbeta(rp+a+r,b+n-r+np-rp) - lbeta(a+r,b+n-r)
+# return(exp(log.val))
+# })
+par(mfrow=c(1,2))
+
+pi <- Vectorize(function(theta) dbeta(theta, 9.2,13.8))
+curve(pi, xlab="Probabiltiy of response", ylab="Density", main="(a) Prior distribution Beta(9.2, 13.8)", frame=FALSE, lwd=2)
+
+# or we can use the built-in function for beta-binomial distribution in package TailRank:
+library(TailRank)
+N <- 20; a <- 9.2; b <- 13.8; y <- 0:N
+yy <- dbb(y, N, a, b)
+barplot(yy, xlab = "Number of successes", names.arg = 0:20, axes = F,
+ ylim = c(0, 0.16), main = "(b) predictive distribution")
+axis(2,at=seq(0,0.16,0.02))
下面的代碼展示了如何在 R 裏連接OpenBUGS進行蒙特卡羅運算和調出其結果的過程:
-library(BRugs)
-
-# bugpath <- "C:/Users/Chao/Documents/LSHTMlearningnote/" # in letsnote win10
-# bugpath <- "C:/Users/AMU-phealth/Documents/LSHTMlearningnote/" # in epson win10
-# bugpath <- "~/Documents/LSHTMlearningnote/" # in epson ubuntu 18.04
-bugpath <- getwd()
-
-# Step 1 check model
-modelCheck(paste(bugpath, "/backupfiles/coinmodel.txt", sep = ""))
-# there is no data so just compile the model
-modelCompile(numChains = 1)
-# There is no need to provide initial values as this is
-# a Monte Carlo forward sampling from a known distribution
-# but the program still requires initial values to begin
-# generate a random value.
-modelGenInits() #
-# Set monitors on nodes of interest
-samplesSet(c("P8", "y"))
-# Generate 1000 iterations
-modelUpdate(1000)
-
-#### SHOW POSTERIOR STATISTICS
-sample.statistics <- samplesStats("*")
-print(sample.statistics)
-
-#### PUT THE SAMPLED VALUES IN ONE R DATA FRAME:
-chain <- data.frame(P8 = samplesSample("P8"),
- y = samplesSample("y"))
-samplesHistory("*", mfrow = c(2,1), ask=FALSE)
library(BRugs)
+
+# bugpath <- "C:/Users/Chao/Documents/LSHTMlearningnote/" # in letsnote win10
+# bugpath <- "C:/Users/AMU-phealth/Documents/LSHTMlearningnote/" # in epson win10
+# bugpath <- "~/Documents/LSHTMlearningnote/" # in epson ubuntu 18.04
+bugpath <- getwd()
+
+# Step 1 check model
+modelCheck(paste(bugpath, "/backupfiles/coinmodel.txt", sep = ""))
+# there is no data so just compile the model
+modelCompile(numChains = 1)
+# There is no need to provide initial values as this is
+# a Monte Carlo forward sampling from a known distribution
+# but the program still requires initial values to begin
+# generate a random value.
+modelGenInits() #
+# Set monitors on nodes of interest
+samplesSet(c("P8", "y"))
+# Generate 1000 iterations
+modelUpdate(1000)
+
+#### SHOW POSTERIOR STATISTICS
+sample.statistics <- samplesStats("*")
+print(sample.statistics)
+
+#### PUT THE SAMPLED VALUES IN ONE R DATA FRAME:
+chain <- data.frame(P8 = samplesSample("P8"),
+ y = samplesSample("y"))
+samplesHistory("*", mfrow = c(2,1), ask=FALSE)
下面的代碼展示了如何在R裏連接JAGS進行蒙特卡羅運算和調出其結果的過程:
-library(runjags)
-library(rjags)
-library(R2jags)
-bugpath <- getwd()
-
-# Step 1 check model
-jagsModel <- jags.model(file = paste(bugpath, "/backupfiles/coinmodel.txt", sep = ""), n.chains = 1)
library(runjags)
+library(rjags)
+library(R2jags)
+bugpath <- getwd()
+
+# Step 1 check model
+jagsModel <- jags.model(file = paste(bugpath, "/backupfiles/coinmodel.txt", sep = ""), n.chains = 1)
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
@@ -1629,17 +1653,17 @@ 82.5 Practical Bayesian Statistic
## Total graph size: 6
##
## Initializing model
-# Step 2 update 1000 iterations
-update(jagsModel, n.iter = 1000, progress.bar="none")
-
-# Step 3 set monitor variables
-
-codaSamples <- coda.samples(
- jagsModel,
- variable.names = c("P8", "y"),
- n.iter = 1000, progress.bar="none")
-
-summary(codaSamples)
# Step 2 update 1000 iterations
+update(jagsModel, n.iter = 1000, progress.bar="none")
+
+# Step 3 set monitor variables
+
+codaSamples <- coda.samples(
+ jagsModel,
+ variable.names = c("P8", "y"),
+ n.iter = 1000, progress.bar="none")
+
+summary(codaSamples)
##
## Iterations = 1001:2000
## Thinning interval = 1
@@ -1650,19 +1674,19 @@ 82.5 Practical Bayesian Statistic
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
-## P8 0.057 0.23196 0.0073352 0.0073352
-## y 4.987 1.57267 0.0497323 0.0497323
+## P8 0.051 0.22011 0.0069604 0.0069604
+## y 5.007 1.54121 0.0487373 0.0534944
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## P8 0 0 0 0 1
## y 2 4 5 6 8
-# Show the trace plot
-library(mcmcplots)
-# ggSample <- ggs(codaSamples)
-# ggs_traceplot(ggSample)
-mcmcplots::traplot(codaSamples)
# Show the trace plot
+library(mcmcplots)
+# ggSample <- ggs(codaSamples)
+# ggs_traceplot(ggSample)
+mcmcplots::traplot(codaSamples)
OpenBUGS Codes:
-# Step 1 check model
-modelCheck(paste(bugpath, "/backupfiles/coinmodel30.txt", sep = ""))
-# there is no data so just compile the model
-modelCompile(numChains = 1)
-# There is no need to provide initial values as this is
-# a Monte Carlo forward sampling from a known distribution
-# but the program still requires initial values to begin
-# generate a random value.
-modelGenInits() #
-# Set monitors on nodes of interest#### SPECIFY, WHICH PARAMETERS TO TRACE:
-parameters <- c("P15", "y")
-samplesSet(parameters)
-# Generate 1000 iterations
-modelUpdate(10000)
-
-#### SHOW POSTERIOR STATISTICS
-sample.statistics <- samplesStats("*")
-print(sample.statistics)
-
-#### PUT THE SAMPLED VALUES IN ONE R DATA FRAME:
-chain <- data.frame(P15 = samplesSample("P15"),
- y = samplesSample("y"))
-#### PLOT THE HISTOGRAMS OF THE SAMPLED VALUES
-## samplesDensity("*", 1, mfrow = c(2,2), ask=NULL)
-
-for(p_ in parameters)
- {
- hist(chain[[p_]], main=p_,
- ylab=NA, xlab=NA, #prob = TRUE,
- nclas=50, col="red")
- }
# Step 1 check model
+modelCheck(paste(bugpath, "/backupfiles/coinmodel30.txt", sep = ""))
+# there is no data so just compile the model
+modelCompile(numChains = 1)
+# There is no need to provide initial values as this is
+# a Monte Carlo forward sampling from a known distribution
+# but the program still requires initial values to begin
+# generate a random value.
+modelGenInits() #
+# Set monitors on nodes of interest#### SPECIFY, WHICH PARAMETERS TO TRACE:
+parameters <- c("P15", "y")
+samplesSet(parameters)
+# Generate 1000 iterations
+modelUpdate(10000)
+
+#### SHOW POSTERIOR STATISTICS
+sample.statistics <- samplesStats("*")
+print(sample.statistics)
+
+#### PUT THE SAMPLED VALUES IN ONE R DATA FRAME:
+chain <- data.frame(P15 = samplesSample("P15"),
+ y = samplesSample("y"))
+#### PLOT THE HISTOGRAMS OF THE SAMPLED VALUES
+## samplesDensity("*", 1, mfrow = c(2,2), ask=NULL)
+
+for(p_ in parameters)
+ {
+ hist(chain[[p_]], main=p_,
+ ylab=NA, xlab=NA, #prob = TRUE,
+ nclas=50, col="red")
+ }
JAGS code:
-# Step 1 check model
-jagsModel <- jags.model(file = paste(bugpath, "/backupfiles/coinmodel30.txt", sep = ""),
- n.chains = 1)
# Step 1 check model
+jagsModel <- jags.model(file = paste(bugpath, "/backupfiles/coinmodel30.txt", sep = ""),
+ n.chains = 1)
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
@@ -1719,19 +1743,19 @@ 82.5 Practical Bayesian Statistic
## Total graph size: 6
##
## Initializing model
-# Step 2 update 1000 iterations
-update(jagsModel, n.iter = 1000,
- progress.bar="none")
-
-# Step 3 set monitor variables
-
-codaSamples <- coda.samples(
- jagsModel,
- variable.names = c("P15", "y"),
- n.iter = 10000,
- progress.bar="none")
-
-summary(codaSamples)
# Step 2 update 1000 iterations
+update(jagsModel, n.iter = 1000,
+ progress.bar="none")
+
+# Step 3 set monitor variables
+
+codaSamples <- coda.samples(
+ jagsModel,
+ variable.names = c("P15", "y"),
+ n.iter = 10000,
+ progress.bar="none")
+
+summary(codaSamples)
##
## Iterations = 1001:11000
## Thinning interval = 1
@@ -1742,17 +1766,17 @@ 82.5 Practical Bayesian Statistic
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
-## P15 0.0154 0.12314 0.0012314 0.0012314
-## y 21.0024 2.50324 0.0250324 0.0256818
+## P15 0.0175 0.13113 0.0013113 0.0013113
+## y 20.9593 2.52442 0.0252442 0.0252442
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## P15 0 0 0 0 0
## y 16 19 21 23 26
-#### PLOT THE HISTOGRAMS OF THE SAMPLED VALUES
-ggSample <- ggs(codaSamples)
-ggs_histogram(ggSample, bins = 18)
#### PLOT THE HISTOGRAMS OF THE SAMPLED VALUES
+ggSample <- ggs(codaSamples)
+ggs_histogram(ggSample, bins = 18)
把這個模型存儲成drug-MC.txt
文件之後,可以使用OpenBUGS完成該模型的蒙特卡羅模擬試驗計算:
# Step 1 check model
-modelCheck(paste(bugpath, "/backupfiles/drug-MC.txt", sep = ""))
-# there is no data so just compile the model
-modelCompile(numChains = 1)
-# There is no need to provide initial values as this is
-# a Monte Carlo forward sampling from a known distribution
-# but the program still requires initial values to begin
-# generate a random value.
-modelGenInits() #
-# Set monitors on nodes of interest#### SPECIFY, WHICH PARAMETERS TO TRACE:
-parameters <- c("theta", "y", "P.crit")
-samplesSet(parameters)
-# Generate 1000 iterations
-modelUpdate(10000)
-
-#### SHOW POSTERIOR STATISTICS
-sample.statistics <- samplesStats("*")
-print(sample.statistics)
-
-#### PUT THE SAMPLED VALUES IN ONE R DATA FRAME:
-chain <- data.frame(theta = samplesSample("theta"),
- y = samplesSample("y"),
- P.crit = samplesSample("P.crit"))
-#### PLOT THE DENSITY and HISTOGRAMS OF THE SAMPLED VALUES
-##1. samplesDensity("*", 1, mfrow = c(2,2), ask=NULL)
-# or 2. by looping
-# for(p_ in parameters)
-# {
-# hist(chain[[p_]], main=p_,
-# ylab=NA, xlab=NA, #prob = TRUE,
-# nclas=50, col="red")
-# }
-par(mfrow=c(1,2))
-
-plot(density(chain$theta), main = "theta sample 10000",
- ylab = "P(theta)", xlab = "theta", col = "red")
-hist(chain$y, main = "y sample 10000", ylab = "P(Y)",
- xlab = "y", col = "red", prob = TRUE)
# Step 1 check model
+modelCheck(paste(bugpath, "/backupfiles/drug-MC.txt", sep = ""))
+# there is no data so just compile the model
+modelCompile(numChains = 1)
+# There is no need to provide initial values as this is
+# a Monte Carlo forward sampling from a known distribution
+# but the program still requires initial values to begin
+# generate a random value.
+modelGenInits() #
+# Set monitors on nodes of interest#### SPECIFY, WHICH PARAMETERS TO TRACE:
+parameters <- c("theta", "y", "P.crit")
+samplesSet(parameters)
+# Generate 1000 iterations
+modelUpdate(10000)
+
+#### SHOW POSTERIOR STATISTICS
+sample.statistics <- samplesStats("*")
+print(sample.statistics)
+
+#### PUT THE SAMPLED VALUES IN ONE R DATA FRAME:
+chain <- data.frame(theta = samplesSample("theta"),
+ y = samplesSample("y"),
+ P.crit = samplesSample("P.crit"))
+#### PLOT THE DENSITY and HISTOGRAMS OF THE SAMPLED VALUES
+##1. samplesDensity("*", 1, mfrow = c(2,2), ask=NULL)
+# or 2. by looping
+# for(p_ in parameters)
+# {
+# hist(chain[[p_]], main=p_,
+# ylab=NA, xlab=NA, #prob = TRUE,
+# nclas=50, col="red")
+# }
+par(mfrow=c(1,2))
+
+plot(density(chain$theta), main = "theta sample 10000",
+ ylab = "P(theta)", xlab = "theta", col = "red")
+hist(chain$y, main = "y sample 10000", ylab = "P(Y)",
+ xlab = "y", col = "red", prob = TRUE)
也可以使用JAGS完成該模型的蒙特卡羅模擬試驗計算:
-# Step 1 check model
-jagsModel <- jags.model(file = paste(bugpath,
- "/backupfiles/drug-MC.txt", sep = ""),
- n.chains = 1)
# Step 1 check model
+jagsModel <- jags.model(file = paste(bugpath,
+ "/backupfiles/drug-MC.txt", sep = ""),
+ n.chains = 1)
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
@@ -1824,19 +1848,19 @@ 82.5 Practical Bayesian Statistic
## Total graph size: 8
##
## Initializing model
-# Step 2 update 1000 iterations
-update(jagsModel, n.iter = 1000,
- progress.bar="none")
-
-# Step 3 set monitor variables
-parameters <- c("theta", "y", "P.crit")
-codaSamples <- coda.samples(
- jagsModel,
- variable.names = parameters,
- n.iter = 10000,
- progress.bar="none")
-
-summary(codaSamples)
# Step 2 update 1000 iterations
+update(jagsModel, n.iter = 1000,
+ progress.bar="none")
+
+# Step 3 set monitor variables
+parameters <- c("theta", "y", "P.crit")
+codaSamples <- coda.samples(
+ jagsModel,
+ variable.names = parameters,
+ n.iter = 10000,
+ progress.bar="none")
+
+summary(codaSamples)
##
## Iterations = 1001:11000
## Thinning interval = 1
@@ -1846,38 +1870,38 @@ 82.5 Practical Bayesian Statistic
## 1. Empirical mean and standard deviation for each variable,
## plus standard error of the mean:
##
-## Mean SD Naive SE Time-series SE
-## P.crit 0.01590 0.12509 0.0012509 0.0012509
-## theta 0.39992 0.10015 0.0010015 0.0010015
-## y 8.00270 2.92936 0.0292936 0.0292936
+## Mean SD Naive SE Time-series SE
+## P.crit 0.01610 0.125866 0.00125866 0.00125866
+## theta 0.39929 0.099985 0.00099985 0.00099985
+## y 8.02570 2.922036 0.02922036 0.02922036
##
## 2. Quantiles for each variable:
##
-## 2.5% 25% 50% 75% 97.5%
-## P.crit 0.00000 0.00000 0.00000 0.00000 0.00000
-## theta 0.21697 0.32841 0.39673 0.46831 0.60352
-## y 3.00000 6.00000 8.00000 10.00000 14.00000
-#### PLOT THE DENSITY and HISTOGRAMS OF THE SAMPLED VALUES
-# par(mfrow=c(1,2))
-#
-ggSample <- ggs(codaSamples)
-# ggSample %>%
-# filter(Parameter == "theta") %>%
-# ggs_density()
-#
-# ggSample %>%
-# filter(Parameter == "y") %>%
-# ggs_histogram(bin = 38)
-
-par(mfrow=c(1,2))
-Theta <- ggSample %>%
- filter(Parameter == "theta")
-Y <- ggSample %>%
- filter(Parameter == "y")
-plot(density(Theta$value), main = "theta sample 10000",
- ylab = "P(theta)", xlab = "theta", col = "red")
-hist(Y$value, main = "y sample 10000", ylab = "P(Y)",
- xlab = "y", col = "red", prob = TRUE)
#### PLOT THE DENSITY and HISTOGRAMS OF THE SAMPLED VALUES
+# par(mfrow=c(1,2))
+#
+ggSample <- ggs(codaSamples)
+# ggSample %>%
+# filter(Parameter == "theta") %>%
+# ggs_density()
+#
+# ggSample %>%
+# filter(Parameter == "y") %>%
+# ggs_histogram(bin = 38)
+
+par(mfrow=c(1,2))
+Theta <- ggSample %>%
+ filter(Parameter == "theta")
+Y <- ggSample %>%
+ filter(Parameter == "y")
+plot(density(Theta$value), main = "theta sample 10000",
+ ylab = "P(theta)", xlab = "theta", col = "red")
+hist(Y$value, main = "y sample 10000", ylab = "P(Y)",
+ xlab = "y", col = "red", prob = TRUE)
# Step 1 check model
-jagsModel <- jags.model(file = paste(bugpath,
- "/backupfiles/drug-MCuniform.txt", sep = ""),
- n.chains = 1)
# Step 1 check model
+jagsModel <- jags.model(file = paste(bugpath,
+ "/backupfiles/drug-MCuniform.txt", sep = ""),
+ n.chains = 1)
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
@@ -1907,19 +1931,19 @@ 82.5 Practical Bayesian Statistic
## Total graph size: 8
##
## Initializing model
-# Step 2 update 1000 iterations
-update(jagsModel, n.iter = 1000,
- progress.bar="none")
-
-# Step 3 set monitor variables
-parameters <- c("theta", "y", "P.crit")
-codaSamples <- coda.samples(
- jagsModel,
- variable.names = parameters,
- n.iter = 10000,
- progress.bar="none")
-
-summary(codaSamples)
# Step 2 update 1000 iterations
+update(jagsModel, n.iter = 1000,
+ progress.bar="none")
+
+# Step 3 set monitor variables
+parameters <- c("theta", "y", "P.crit")
+codaSamples <- coda.samples(
+ jagsModel,
+ variable.names = parameters,
+ n.iter = 10000,
+ progress.bar="none")
+
+summary(codaSamples)
##
## Iterations = 1001:11000
## Thinning interval = 1
@@ -1930,28 +1954,28 @@ 82.5 Practical Bayesian Statistic
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
-## P.crit 0.29050 0.45402 0.0045402 0.0045260
-## theta 0.50342 0.28934 0.0028934 0.0028934
-## y 10.08350 6.06925 0.0606925 0.0606925
+## P.crit 0.28860 0.45313 0.0045313 0.0044930
+## theta 0.50267 0.28888 0.0028888 0.0028561
+## y 10.03850 6.05737 0.0605737 0.0597831
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## P.crit 0.000000 0.00000 0.00000 1.00000 1.00000
-## theta 0.024581 0.25412 0.50261 0.75692 0.97429
+## theta 0.024136 0.25282 0.50536 0.75505 0.97424
## y 0.000000 5.00000 10.00000 15.00000 20.00000
-#### PLOT THE DENSITY and HISTOGRAMS OF THE SAMPLED VALUES
-ggSample <- ggs(codaSamples)
-
-par(mfrow=c(1,2))
-Theta <- ggSample %>%
- filter(Parameter == "theta")
-Y <- ggSample %>%
- filter(Parameter == "y")
-plot(density(Theta$value), main = "theta sample 10000",
- ylab = "P(theta)", xlab = "theta", col = "red")
-hist(Y$value, main = "y sample 10000", ylab = "P(Y)",
- xlab = "y", col = "red", prob = TRUE)
#### PLOT THE DENSITY and HISTOGRAMS OF THE SAMPLED VALUES
+ggSample <- ggs(codaSamples)
+
+par(mfrow=c(1,2))
+Theta <- ggSample %>%
+ filter(Parameter == "theta")
+Y <- ggSample %>%
+ filter(Parameter == "y")
+plot(density(Theta$value), main = "theta sample 10000",
+ ylab = "P(theta)", xlab = "theta", col = "red")
+hist(Y$value, main = "y sample 10000", ylab = "P(Y)",
+ xlab = "y", col = "red", prob = TRUE)
分別對這個模型嘗試蒙特卡羅模擬試驗100, 1000, 和100000次,比較蒙特卡羅模擬試驗給出的概率估計和理論值的差異。
-# Step 1 check model
-jagsModel <- jags.model(file = paste(bugpath,
- "/backupfiles/standardnormalMC.txt", sep = ""),
- n.chains = 1)
# Step 1 check model
+jagsModel <- jags.model(file = paste(bugpath,
+ "/backupfiles/standardnormalMC.txt", sep = ""),
+ n.chains = 1)
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
@@ -1985,19 +2009,19 @@ 82.5 Practical Bayesian Statistic
## Total graph size: 11
##
## Initializing model
-# Step 2 update 1000 iterations
-update(jagsModel, n.iter = 10,
- progress.bar="none")
-
-# Step 3 set monitor variables
-parameters <- c("p.1", "p.2", "x")
-codaSamples <- coda.samples(
- jagsModel,
- variable.names = parameters,
- n.iter = 100,
- progress.bar="none")
-
-summary(codaSamples)
# Step 2 update 1000 iterations
+update(jagsModel, n.iter = 10,
+ progress.bar="none")
+
+# Step 3 set monitor variables
+parameters <- c("p.1", "p.2", "x")
+codaSamples <- coda.samples(
+ jagsModel,
+ variable.names = parameters,
+ n.iter = 100,
+ progress.bar="none")
+
+summary(codaSamples)
##
## Iterations = 11:110
## Thinning interval = 1
@@ -2007,25 +2031,25 @@ 82.5 Practical Bayesian Statistic
## 1. Empirical mean and standard deviation for each variable,
## plus standard error of the mean:
##
-## Mean SD Naive SE Time-series SE
-## p.1 0.03000 0.17145 0.017145 0.017145
-## p.2 0.02000 0.14071 0.014071 0.014071
-## x 0.14143 0.91558 0.091558 0.091558
+## Mean SD Naive SE Time-series SE
+## p.1 0.010000 0.10000 0.010000 0.010000
+## p.2 0.000000 0.00000 0.000000 0.000000
+## x 0.052771 0.87209 0.087209 0.087209
##
## 2. Quantiles for each variable:
##
-## 2.5% 25% 50% 75% 97.5%
-## p.1 0.0000 0.00000 0.00000 0.00000 0.5250
-## p.2 0.0000 0.00000 0.00000 0.00000 0.0000
-## x -2.0206 -0.43501 0.23037 0.79533 1.5688
-# Generate 900 iterations
-codaSamples <- coda.samples(
- jagsModel,
- variable.names = parameters,
- n.iter = 1000,
- progress.bar="none")
-
-summary(codaSamples)
# Generate 900 iterations
+codaSamples <- coda.samples(
+ jagsModel,
+ variable.names = parameters,
+ n.iter = 1000,
+ progress.bar="none")
+
+summary(codaSamples)
##
## Iterations = 111:1110
## Thinning interval = 1
@@ -2035,25 +2059,25 @@ 82.5 Practical Bayesian Statistic
## 1. Empirical mean and standard deviation for each variable,
## plus standard error of the mean:
##
-## Mean SD Naive SE Time-series SE
-## p.1 0.022000 0.14676 0.0046409 0.0046409
-## p.2 0.011000 0.10435 0.0033000 0.0033000
-## x 0.022962 1.02639 0.0324573 0.0324573
+## Mean SD Naive SE Time-series SE
+## p.1 0.032000 0.17609 0.0055684 0.0055684
+## p.2 0.017000 0.12934 0.0040900 0.0040900
+## x -0.017248 1.03842 0.0328378 0.0352516
##
## 2. Quantiles for each variable:
##
-## 2.5% 25% 50% 75% 97.5%
-## p.1 0.0000 0.00000 0.000000 0.00000 0.0000
-## p.2 0.0000 0.00000 0.000000 0.00000 0.0000
-## x -1.8956 -0.68149 0.026981 0.68031 2.2715
-# Generate 100000 iterations
-codaSamples <- coda.samples(
- jagsModel,
- variable.names = parameters,
- n.iter = 100000,
- progress.bar="none")
-
-summary(codaSamples)
# Generate 100000 iterations
+codaSamples <- coda.samples(
+ jagsModel,
+ variable.names = parameters,
+ n.iter = 100000,
+ progress.bar="none")
+
+summary(codaSamples)
##
## Iterations = 1111:101110
## Thinning interval = 1
@@ -2063,17 +2087,17 @@ 82.5 Practical Bayesian Statistic
## 1. Empirical mean and standard deviation for each variable,
## plus standard error of the mean:
##
-## Mean SD Naive SE Time-series SE
-## p.1 0.0248900 0.15579 0.00049265 0.00047946
-## p.2 0.0097200 0.09811 0.00031025 0.00031025
-## x -0.0047882 1.00267 0.00317073 0.00314004
+## Mean SD Naive SE Time-series SE
+## p.1 0.0248900 0.155791 0.00049265 0.00049265
+## p.2 0.0098600 0.098807 0.00031246 0.00031246
+## x 0.0026182 1.004488 0.00317647 0.00317647
##
## 2. Quantiles for each variable:
##
-## 2.5% 25% 50% 75% 97.5%
-## p.1 0.0000 0.00000 0.0000000 0.00000 0.0000
-## p.2 0.0000 0.00000 0.0000000 0.00000 0.0000
-## x -1.9586 -0.68638 -0.0082853 0.67339 1.9616
+## 2.5% 25% 50% 75% 97.5%
+## p.1 0.0000 0.00000 0.0000000 0.00000 0.0000
+## p.2 0.0000 0.00000 0.0000000 0.00000 0.0000
+## x -1.9586 -0.67905 0.0025235 0.68442 1.9757
我們知道理論上 \(x\sim N(0,1^2)\),它的均值爲0,標準差爲1。我們也能看見蒙特卡羅模擬試驗的結果,隨着我們增加其重復取樣次數二越來越接近理論值。當取樣達到十萬次以上之後,可以看到蒙特卡羅的結果已經十分之接近真實值。在一開始剛剛重復100次蒙特卡羅時,我們可以看到p.1, p.2
的估計還很不準確,但是類似的,當蒙特卡羅採樣次數達到十萬次以上時,這兩個概率估計也已經十分接近真實值。另外值得注意的一點是,隨着蒙特卡羅樣本量增加,MC_error
也在變得越來越小(越來越精確)。事實上,這個Naive SE
本身約等於\(\frac{\text{sd}}{\sqrt{\text{sample size}}}\)。所以對\(x\)來說,經過1000次蒙特卡羅計算,\(\text{sd}(x) = 1.009\),那麼此時的Naive SE
\(=\frac{1.009}{\sqrt{1000}} \approx 0.0319\),十分接近計算機給出的Naive SE = 0.03244
。Naive SE
本身可以作爲這個\(x\)均值的估計精確度來理解,我們同時相信,真實的理論值會落在蒙特卡羅樣本均值\(\pm 2\times\) Naive SE
範圍內。
下面我們來探索一下 t分布。嘗試寫下一個BUGS模型,它的先驗概率分布是一個自由度爲4的t分布,y ~ dt(0,1,4)
。然後進行10000次蒙特卡羅採樣計算,並給出概率密度分布圖。
# Monte Carlo predictions
@@ -2082,10 +2106,10 @@ 82.5 Practical Bayesian Statistic
model{
y ~ dt(0, 1, 4)
}
-# Step 1 check model
-jagsModel <- jags.model(file = paste(bugpath,
- "/backupfiles/MCt.txt", sep = ""),
- n.chains = 1)
# Step 1 check model
+jagsModel <- jags.model(file = paste(bugpath,
+ "/backupfiles/MCt.txt", sep = ""),
+ n.chains = 1)
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
@@ -2095,19 +2119,19 @@ 82.5 Practical Bayesian Statistic
## Total graph size: 4
##
## Initializing model
-# Step 2 update 1000 iterations
-update(jagsModel, n.iter = 10,
- progress.bar="none")
-
-# Step 3 set monitor variables
-parameters <- c("y")
-codaSamples <- coda.samples(
- jagsModel,
- variable.names = parameters,
- n.iter = 10000,
- progress.bar="none")
-
-summary(codaSamples)
# Step 2 update 1000 iterations
+update(jagsModel, n.iter = 10,
+ progress.bar="none")
+
+# Step 3 set monitor variables
+parameters <- c("y")
+codaSamples <- coda.samples(
+ jagsModel,
+ variable.names = parameters,
+ n.iter = 10000,
+ progress.bar="none")
+
+summary(codaSamples)
##
## Iterations = 11:10010
## Thinning interval = 1
@@ -2118,21 +2142,21 @@ 82.5 Practical Bayesian Statistic
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
-## 0.011911 1.421280 0.014213 0.014213
+## -0.0097798 1.4437750 0.0144378 0.0150712
##
## 2. Quantiles for each variable:
##
-## 2.5% 25% 50% 75% 97.5%
-## -2.739387 -0.745638 0.009564 0.757920 2.823163
-#### PLOT THE DENSITY and HISTOGRAMS OF THE SAMPLED VALUES
-ggSample <- ggs(codaSamples)
-
-
-Y <- ggSample %>%
- filter(Parameter == "y")
-
-plot(density(Y$value), main = "y sample 10000",
- ylab = "P(y)", xlab = "y", col = "red")
#### PLOT THE DENSITY and HISTOGRAMS OF THE SAMPLED VALUES
+ggSample <- ggs(codaSamples)
+
+
+Y <- ggSample %>%
+ filter(Parameter == "y")
+
+plot(density(Y$value), main = "y sample 10000",
+ ylab = "P(y)", xlab = "y", col = "red")
# Step 1 check model
-jagsModel <- jags.model(file = paste(bugpath,
- "/backupfiles/MCcube.txt", sep = ""),
- n.chains = 1)
# Step 1 check model
+jagsModel <- jags.model(file = paste(bugpath,
+ "/backupfiles/MCcube.txt", sep = ""),
+ n.chains = 1)
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
@@ -2159,19 +2183,19 @@ 82.5 Practical Bayesian Statistic
## Total graph size: 5
##
## Initializing model
-# Step 2 update 1000 iterations
-update(jagsModel, n.iter = 10,
- progress.bar="none")
-
-# Step 3 set monitor variables
-parameters <- c("y", "ycubed")
-codaSamples <- coda.samples(
- jagsModel,
- variable.names = parameters,
- n.iter = 10000,
- progress.bar="none")
-
-summary(codaSamples)
# Step 2 update 1000 iterations
+update(jagsModel, n.iter = 10,
+ progress.bar="none")
+
+# Step 3 set monitor variables
+parameters <- c("y", "ycubed")
+codaSamples <- coda.samples(
+ jagsModel,
+ variable.names = parameters,
+ n.iter = 10000,
+ progress.bar="none")
+
+summary(codaSamples)
##
## Iterations = 11:10010
## Thinning interval = 1
@@ -2182,29 +2206,29 @@ 82.5 Practical Bayesian Statistic
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
-## y 1.0028 2.0033 0.020033 0.020033
-## ycubed 13.1007 41.1307 0.411307 0.411307
+## y 1.0067 1.9922 0.019922 0.019922
+## ycubed 13.4634 40.2868 0.402868 0.411162
##
## 2. Quantiles for each variable:
##
-## 2.5% 25% 50% 75% 97.5%
-## y -2.8519 -0.348185 1.0285 2.3496 4.9765
-## ycubed -23.1955 -0.042211 1.0881 12.9714 123.2458
-#### PLOT THE DENSITY and HISTOGRAMS OF THE SAMPLED VALUES
-ggSample <- ggs(codaSamples)
-
-par(mfrow=c(1,2))
-
-Y <- ggSample %>%
- filter(Parameter == "y")
-Ycubed <- ggSample %>%
- filter(Parameter == "ycubed")
-
-plot(density(Y$value), main = "y sample 10000",
- ylab = "P(y)", xlab = "y", col = "red")
-
-plot(density(Ycubed$value), main = "ycubed sample 100000",
- ylab = "P(ycubed)", xlab = "ycubed", col = "red")
#### PLOT THE DENSITY and HISTOGRAMS OF THE SAMPLED VALUES
+ggSample <- ggs(codaSamples)
+
+par(mfrow=c(1,2))
+
+Y <- ggSample %>%
+ filter(Parameter == "y")
+Ycubed <- ggSample %>%
+ filter(Parameter == "ycubed")
+
+plot(density(Y$value), main = "y sample 10000",
+ ylab = "P(y)", xlab = "y", col = "red")
+
+plot(density(Ycubed$value), main = "ycubed sample 100000",
+ ylab = "P(ycubed)", xlab = "ycubed", col = "red")
lrt
的估計回歸參數發生了怎樣的變化?lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。lrt
也加入進隨機效應部分。schgend
改成 mean_girl
會給出怎樣的結果呢?gcse
,解釋變量是 lrt
。Fixed_reml2
模型中去,結果有發生顯著的變化嗎?school
level)的殘差。lrt
的估計回歸參數發生了怎樣的變化?lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。lrt
也加入進隨機效應部分。schgend
改成 mean_girl
會給出怎樣的結果呢?gcse
,解釋變量是 lrt
。Fixed_reml2
模型中去,結果有發生顯著的變化嗎?school
level)的殘差。你完全可以另闢蹊徑在STATA或者R裏面寫下吉布斯採樣的算法,但是這並不簡單。幸好,許多軟件已經能夠幫助我們規避掉寫吉布斯採樣算法這一複雜的過程。
BUGS全稱是 Bayesian inference Using Gibbs Sampling。它是用來描述貝葉斯統計學模型的計算機語言。有三種流行的統計學軟件都使用BUGS語言來描述貝葉斯統計學模型,他們分別是 WinBUGS,OpenBUGS,和 JAGS。他們的語法十分接近R的語法,甚至可以直接在R的環境下運行(正如我這本書中在R裏連接JAGS運算貝葉斯統計推斷一樣)。
-WinBUGS 1.4.3是一個穩定的貝葉斯統計學軟件,但是它已經不再更新,也沒有再跟進開發。也正如其名字暗示的那樣,它能且只能運行在瘟倒死(windows)機器上。你可以從它的網站上下載並免費使用之: https://www.mrc-bsu.cam.ac.uk/software/bugs/the-bugs-project-winbugs/。
-OpenBUGS是開源的,且能夠在Linux機器上無縫運行。它也有自己的GUI(只有在瘟倒死上才有的功能),讓你實現用鼠標點擊也能完成貝葉斯統計學分析。它的主要網站是http://www.openbugs.net/w/Downloads。
+WinBUGS 1.4.3是一個穩定的貝葉斯統計學軟件,但是它已經不再更新,也沒有再跟進開發。也正如其名字暗示的那樣,它能且只能運行在瘟倒死(windows)機器上。你可以從它的網站上下載並免費使用之: https://www.mrc-bsu.cam.ac.uk/software/bugs/the-bugs-project-winbugs/。
+OpenBUGS是開源的,且能夠在Linux機器上無縫運行。它也有自己的GUI(只有在瘟倒死上才有的功能),讓你實現用鼠標點擊也能完成貝葉斯統計學分析。它的主要網站是http://www.openbugs.net/w/Downloads。
JAGS的全稱是 Just Another Gibbs Sampler,開發者是 Martyn Plummer。它沒有任何GUI,只能通過命令行來執行運算。
其他著名的軟件還有 Stan。它也是開源的自由軟件。
帶方向的連接線 (directed links)被用來表示邏輯依賴關系的方向(natural ordering of the dependence association)。 其實這連接線所帶的方向本身就是回歸模型中表示預測變量和結果變量之間的依賴關系。貝葉斯統計學中常用的有向無環圖 (directed acyclic graphs, DAGs),是我們常用的輔助建立貝葉斯模型的示意圖。
+帶方向的連接線 (directed links)被用來表示邏輯依賴關系的方向(natural ordering of the dependence association)。 +其實這連接線所帶的方向本身就是回歸模型中表示預測變量和結果變量之間的依賴關系。貝葉斯統計學中常用的有向無環圖 (directed acyclic graphs, DAGs),是我們常用的輔助建立貝葉斯模型的示意圖。
例如一個描述吸煙,肥胖和心髒病發病可能的關系的的有向無環圖如下圖 84.2。途中帶方向的連線(箭頭)表示預測變量和結果變量之間的依賴關系。
# Codes for R2JAGS
-
-
-Dat <- list(
-y = c(15)
-)
-
-# fit use R2jags package
-
-post.jags <- jags(
- data = Dat,
- parameters.to.save = c("theta"),
- n.iter = 1100,
- model.file = paste(bugpath,
- "/backupfiles/MCdrugPractical04.txt",
- sep = ""),
- n.chains = 1,
- n.thin = 1,
- n.burnin = 100,
- progress.bar = "none")
# Codes for R2JAGS
+
+
+Dat <- list(
+y = c(15)
+)
+
+# fit use R2jags package
+
+post.jags <- jags(
+ data = Dat,
+ parameters.to.save = c("theta"),
+ n.iter = 1100,
+ model.file = paste(bugpath,
+ "/backupfiles/MCdrugPractical04.txt",
+ sep = ""),
+ n.chains = 1,
+ n.thin = 1,
+ n.burnin = 100,
+ progress.bar = "none")
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
@@ -1627,7 +1651,7 @@ 84.7 Practical Bayesian Statistic
## Total graph size: 4
##
## Initializing model
-print(post.jags)
## Inference for Bugs model at "/home/ccwang/Documents/LSHTMlearningnote/backupfiles/MCdrugPractical04.txt", fit using jags,
## 1 chains, each with 1100 iterations (first 100 discarded)
## n.sims = 1000 iterations saved
@@ -1658,29 +1682,29 @@ 84.7 Practical Bayesian Statistic
- 嘗試使用JAGS/OpenBUGS來跑這個模型。記得你需要把數據加載到軟件裏面去。
-# Codes for R2JAGS
-
-
-Dat <- list(
- y = c(1, 3, 6, 8, 11, 15, 17, 19),
- n = c(20, 20, 20, 20, 20, 20, 20, 20),
- x = c(30, 32, 34, 36, 38, 40, 42, 44),
- N = 8
-)
-
-# fit use R2jags package
-
-post.jags <- jags(
- data = Dat,
- parameters.to.save = c("beta0", "beta1", "theta[6]"),
- n.iter = 1100,
- model.file = paste(bugpath,
- "/backupfiles/logistic-reg-model.txt",
- sep = ""),
- n.chains = 1,
- n.thin = 1,
- n.burnin = 100,
- progress.bar = "none")
+# Codes for R2JAGS
+
+
+Dat <- list(
+ y = c(1, 3, 6, 8, 11, 15, 17, 19),
+ n = c(20, 20, 20, 20, 20, 20, 20, 20),
+ x = c(30, 32, 34, 36, 38, 40, 42, 44),
+ N = 8
+)
+
+# fit use R2jags package
+
+post.jags <- jags(
+ data = Dat,
+ parameters.to.save = c("beta0", "beta1", "theta[6]"),
+ n.iter = 1100,
+ model.file = paste(bugpath,
+ "/backupfiles/logistic-reg-model.txt",
+ sep = ""),
+ n.chains = 1,
+ n.thin = 1,
+ n.burnin = 100,
+ progress.bar = "none")
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
@@ -1690,7 +1714,7 @@ 84.7 Practical Bayesian Statistic
## Total graph size: 53
##
## Initializing model
-print(post.jags)
+
## Inference for Bugs model at "/home/ccwang/Documents/LSHTMlearningnote/backupfiles/logistic-reg-model.txt", fit using jags,
## 1 chains, each with 1100 iterations (first 100 discarded)
## n.sims = 1000 iterations saved
@@ -1706,8 +1730,8 @@ 84.7 Practical Bayesian Statistic
- 對未知參數,也就是邏輯回歸的回歸系數
beta0, beta1
,和第六次試驗的治療成功百分比 theta[6]
設定監測追蹤其採樣軌跡。試着執行1000次MCMC採樣,把這三個跟蹤的未知參數的採樣歷史軌跡繪制下來。
-#### PLOT THE MCMC CHAINS:
-mcmcplots::traplot(post.jags, c("beta0", "beta1", "theta[6]"))
+
84.7 Practical Bayesian Statistic
- 接下來,我們給上面同一個邏輯回歸模型增加另一條MCMC採樣鏈,重復跑相同的模型1000次,繪制同樣是這三個未知參數的事後分布MCMC採樣歷史痕跡圖。
-# fit use R2jags package
-
-post.jags <- jags(
- data = Dat,
- parameters.to.save = c("beta0", "beta1", "theta[6]"),
- n.iter = 1000,
- model.file = paste(bugpath,
- "/backupfiles/logistic-reg-model.txt",
- sep = ""),
- n.chains = 2,
- n.thin = 1,
- n.burnin = 0,
- progress.bar = "none")
+# fit use R2jags package
+
+post.jags <- jags(
+ data = Dat,
+ parameters.to.save = c("beta0", "beta1", "theta[6]"),
+ n.iter = 1000,
+ model.file = paste(bugpath,
+ "/backupfiles/logistic-reg-model.txt",
+ sep = ""),
+ n.chains = 2,
+ n.thin = 1,
+ n.burnin = 0,
+ progress.bar = "none")
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
@@ -1740,7 +1764,7 @@ 84.7 Practical Bayesian Statistic
## Total graph size: 53
##
## Initializing model
-mcmcplots::traplot(post.jags, c("beta0", "beta1", "theta[6]"))
+
84.7 Practical Bayesian Statistic
使用兩個不同的起始值作爲MCMC的採樣起點時,每個未知參數分別獲得兩條不同顏色的歷史痕跡圖。和之前只有一條MCMC採樣鏈時一樣,採樣的起始部分(大約100次左右)都有一些不穩定的值。等到100次採樣過後,我們發現每個參數的採樣結果都趨向於比較穩定,也就是方差,變化小了很多。但是藍色鏈,紅色鏈一直到200-300次採樣之後才逐漸互相交叉重疊。下面的圖把起初的500次採樣刨除了之後重新對每個未知參數的MCMC採樣繪制歷史痕跡。
-# samplesHistory("*", mfrow = c(3,1), beg = 501, ask = FALSE)
-Simulated <- coda::as.mcmc(post.jags)
-ggSample <- ggs(Simulated)
-
-ggSample %>%
- filter(Iteration >= 500 & Parameter %in% c("beta0", "beta1", "theta[6]")) %>%
- ggs_traceplot()
+# samplesHistory("*", mfrow = c(3,1), beg = 501, ask = FALSE)
+Simulated <- coda::as.mcmc(post.jags)
+ggSample <- ggs(Simulated)
+
+ggSample %>%
+ filter(Iteration >= 500 & Parameter %in% c("beta0", "beta1", "theta[6]")) %>%
+ ggs_traceplot()
84.7 Practical Bayesian Statistic
500-1000次之間的隨機採樣被放大了觀察之後,我們發現 beta0, beta1
的兩條MCMC鏈條的重疊程度並不理想,不像theta[6]
那樣呈現令人滿意地重疊,兩條採樣鏈上下扭動,且在一些地方差異較大。
繪制每個參數的MCMC鏈的自相關圖 (autocorrelation),下面的圖中只繪制了500-1000次範圍內採樣的自相關圖。
-ggSample %>%
- filter(Iteration >= 500 & Parameter %in% c("beta0", "beta1", "theta[6]")) %>%
- ggs_autocorrelation()
+ggSample %>%
+ filter(Iteration >= 500 & Parameter %in% c("beta0", "beta1", "theta[6]")) %>%
+ ggs_autocorrelation()
84.7 Practical Bayesian Statistic
- 重新在OpenBUGS裏跑這個邏輯回歸模型,這一次,嘗試自己給
beta0, beta1
設置起始值。然後更新模型,採集MCMC鏈樣本10000次。這次嘗試同時使用繪制歷史痕跡圖(視覺檢查),和計算Brooks-Gelman-Rubin診斷參數及其圖示來判斷事後概率分布採樣是否達到收斂。你能判斷這個模型需要拋出掉多少一開始採集的樣本嗎(burn-in)?
-Dat <- list(
- y = c(1, 3, 6, 8, 11, 15, 17, 19),
- n = c(20, 20, 20, 20, 20, 20, 20, 20),
- x = c(30, 32, 34, 36, 38, 40, 42, 44),
- N = 8
-)
-
-# fit use rjags package
-
-inits <- function (){
- list (beta0=rnorm(1), beta1=runif(1) )
-}
-
-post.jags <- jags(
- data = Dat,
- inits = inits,
- parameters.to.save = c("beta0", "beta1", "theta[6]"),
- n.iter = 5000,
- model.file = paste(bugpath,
- "/backupfiles/logistic-reg-model.txt",
- sep = ""),
- n.chains = 2,
- n.thin = 1,
- n.burnin = 0,
- progress.bar = "none")
-
-mcmcplots::traplot(post.jags, c("beta0", "beta1", "theta[6]"))
+Dat <- list(
+ y = c(1, 3, 6, 8, 11, 15, 17, 19),
+ n = c(20, 20, 20, 20, 20, 20, 20, 20),
+ x = c(30, 32, 34, 36, 38, 40, 42, 44),
+ N = 8
+)
+
+# fit use rjags package
+
+# inits <- function (){
+# list (beta0=rnorm(1), beta1=runif(1) )
+# }
+
+post.jags <- jags(
+ data = Dat,
+ # inits = inits,
+ parameters.to.save = c("beta0", "beta1", "theta[6]"),
+ n.iter = 5000,
+ model.file = paste(bugpath,
+ "/backupfiles/logistic-reg-model.txt",
+ sep = ""),
+ n.chains = 2,
+ n.thin = 1,
+ n.burnin = 0,
+ progress.bar = "none")
+
+mcmcplots::traplot(post.jags, c("beta0", "beta1", "theta[6]"))
-
Simulated <- coda::as.mcmc(post.jags)
-# postsamples <- buildMCMC("*")
-gelman.diag(Simulated)
+
## Potential scale reduction factors:
##
## Point est. Upper C.I.
@@ -1823,7 +1847,7 @@ 84.7 Practical Bayesian Statistic
## Multivariate psrf
##
## 2.08
-gelman.plot(Simulated)
+
84.7 Practical Bayesian Statistic
- 確認了你想要刨除的初始樣本量(burn-in),繼續再進行MCMC採樣直到獲得1,000,000個事後概率分布樣本。此時你對獲得的事後概率分布樣本量提供的參數估計精確度滿意否?嘗試報告此時獲得的參數們的事後均值,及其95%可信區間。
-Dat <- list(
- y = c(1, 3, 6, 8, 11, 15, 17, 19),
- n = c(20, 20, 20, 20, 20, 20, 20, 20),
- x = c(30, 32, 34, 36, 38, 40, 42, 44),
- N = 8
-)
-
-# inits <- function (){
-# list (beta0=rnorm(1), beta1=runif(1) )
-# }
-
-post.jags <- jags(
- data = Dat,
- # inits = inits,
- parameters.to.save = c("beta0", "beta1", "theta[6]"),
- n.iter = 502000,
- model.file = paste(bugpath,
- "/backupfiles/logistic-reg-model.txt",
- sep = ""),
- n.chains = 2,
- n.thin = 1,
- n.burnin = 2000,
- progress.bar = "none")
+Dat <- list(
+ y = c(1, 3, 6, 8, 11, 15, 17, 19),
+ n = c(20, 20, 20, 20, 20, 20, 20, 20),
+ x = c(30, 32, 34, 36, 38, 40, 42, 44),
+ N = 8
+)
+
+# inits <- function (){
+# list (beta0=rnorm(1), beta1=runif(1) )
+# }
+
+post.jags <- jags(
+ data = Dat,
+ # inits = inits,
+ parameters.to.save = c("beta0", "beta1", "theta[6]"),
+ n.iter = 502000,
+ model.file = paste(bugpath,
+ "/backupfiles/logistic-reg-model.txt",
+ sep = ""),
+ n.chains = 2,
+ n.thin = 1,
+ n.burnin = 2000,
+ progress.bar = "none")
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
@@ -1934,36 +1958,36 @@ 84.7 Practical Bayesian Statistic
beta0 ~ dunif(-100, 100)
beta1 ~ dunif(-100, 100)
}
-# JAGS codes:
-#
-Dat <- list(
- y = c(1, 3, 6, 8, 11, 15, 17, 19),
- n = c(20, 20, 20, 20, 20, 20, 20, 20),
- x = c(30, 32, 34, 36, 38, 40, 42, 44),
- N = 8
-)
-
-# step 1 check model
-jagsModel <- jags.model(
- file = paste(bugpath,
- "/backupfiles/logistic-reg-model-centred.txt",
- sep = ""),
- data = Dat,
- n.chains = 2,
- inits = inits,
- quiet = TRUE)
-# Step 2 update 10000 iterations
-
-update(jagsModel, n.iter = 1, progress.bar = "none")
-
-# Step 3 set monitor variables
-
-codaSamples <- coda.samples(
- jagsModel, variable.names = c("beta0", "beta1", "theta[6]"),
- n.iter = 10000, progress.bar = "none"
-)
-
-mcmcplots::traplot(codaSamples, c("beta0", "beta1", "theta[6]"))
+# JAGS codes:
+#
+Dat <- list(
+ y = c(1, 3, 6, 8, 11, 15, 17, 19),
+ n = c(20, 20, 20, 20, 20, 20, 20, 20),
+ x = c(30, 32, 34, 36, 38, 40, 42, 44),
+ N = 8
+)
+
+# step 1 check model
+jagsModel <- jags.model(
+ file = paste(bugpath,
+ "/backupfiles/logistic-reg-model-centred.txt",
+ sep = ""),
+ data = Dat,
+ n.chains = 2,
+ # inits = inits,
+ quiet = TRUE)
+# Step 2 update 10000 iterations
+
+update(jagsModel, n.iter = 1, progress.bar = "none")
+
+# Step 3 set monitor variables
+
+codaSamples <- coda.samples(
+ jagsModel, variable.names = c("beta0", "beta1", "theta[6]"),
+ n.iter = 10000, progress.bar = "none"
+)
+
+mcmcplots::traplot(codaSamples, c("beta0", "beta1", "theta[6]"))
84.7 Practical Bayesian Statistic
用中心化之後的模型我們發現需要10000或更多的起始樣本來達到事後概率分布的收斂。BGR診斷圖 84.14 也提示我們大概在10000次之後的重復採樣才能達到收斂。所以我們決定要刨除起始10000次採集的樣本。(burn-in = 10000)
-gelman.diag(codaSamples)
+
## Potential scale reduction factors:
##
## Point est. Upper C.I.
@@ -1982,7 +2006,7 @@ 84.7 Practical Bayesian Statistic
## Multivariate psrf
##
## 1
-gelman.plot(codaSamples)
+
84.7 Practical Bayesian Statistic
## 2 chains, each with 20000 iterations (first 10000 discarded)
## n.sims = 20000 iterations saved
## mu.vect sd.vect 2.5% 25% 50% 75% 97.5% Rhat n.eff
-## beta0 0.002 0.206 -0.398 -0.139 0.001 0.140 0.412 1.001 20000
-## beta1 0.377 0.057 0.269 0.337 0.375 0.413 0.494 1.001 19000
-## theta[6] 0.753 0.049 0.654 0.721 0.755 0.787 0.844 1.001 13000
-## deviance 25.782 2.046 23.759 24.302 25.143 26.601 31.325 1.001 20000
+## beta0 -0.002 0.208 -0.404 -0.141 -0.002 0.136 0.408 1.002 2300
+## beta1 0.376 0.057 0.272 0.336 0.374 0.413 0.493 1.001 20000
+## theta[6] 0.752 0.049 0.651 0.719 0.754 0.786 0.842 1.001 4100
+## deviance 25.769 2.068 23.758 24.304 25.140 26.538 31.408 1.001 20000
##
## For each parameter, n.eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor (at convergence, Rhat=1).
@@ -2042,46 +2066,47 @@ 84.7 Practical Bayesian Statistic
ED95 <- (logit(0.95) - beta0)/beta1 + mean(x[]) # dose that gives 95% of maximal response
logit(P35) <- beta0 + beta1 * (35 - mean(x[])) # probability of positive reaction if given 35mg drugs
}
-# R2JAGS codes:
-Dat <- list(
- y = c(1, 3, 6, 8, 11, 15, 17, 19),
- n = c(20, 20, 20, 20, 20, 20, 20, 20),
- x = c(30, 32, 34, 36, 38, 40, 42, 44),
- N = 8
-)
-post.jags <- jags(
- data = Dat,
- inits = inits,
- parameters.to.save = c("ED95", "OR", "P35", "beta0", "beta1"),
- n.iter = 20000,
- model.file = paste(bugpath,
- "/backupfiles/logistic-reg-model-centred-stat.txt",
- sep = ""),
- n.chains = 2,
- n.thin = 1,
- n.burnin = 10000,
- progress.bar = "none")
+# R2JAGS codes:
+Dat <- list(
+ y = c(1, 3, 6, 8, 11, 15, 17, 19),
+ n = c(20, 20, 20, 20, 20, 20, 20, 20),
+ x = c(30, 32, 34, 36, 38, 40, 42, 44),
+ N = 8
+)
+
+post.jags <- jags(
+ data = Dat,
+ # inits = inits,
+ parameters.to.save = c("ED95", "OR", "P35", "beta0", "beta1"),
+ n.iter = 20000,
+ model.file = paste(bugpath,
+ "/backupfiles/logistic-reg-model-centred-stat.txt",
+ sep = ""),
+ n.chains = 2,
+ n.thin = 1,
+ n.burnin = 10000,
+ progress.bar = "none")
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
## Graph information:
## Observed stochastic nodes: 8
## Unobserved stochastic nodes: 2
-## Total graph size: 74
+## Total graph size: 76
##
## Initializing model
-# mcmcplots::traplot(post.jags, c("beta0", "beta1", "theta[6]"))
-print(post.jags)
+
## Inference for Bugs model at "/home/ccwang/Documents/LSHTMlearningnote/backupfiles/logistic-reg-model-centred-stat.txt", fit using jags,
## 2 chains, each with 20000 iterations (first 10000 discarded)
## n.sims = 20000 iterations saved
## mu.vect sd.vect 2.5% 25% 50% 75% 97.5% Rhat n.eff
-## ED95 45.037 1.376 42.754 44.081 44.895 45.833 48.134 1.001 14000
-## OR 1.458 0.083 1.310 1.400 1.453 1.509 1.636 1.001 20000
-## P35 0.322 0.050 0.227 0.288 0.322 0.356 0.422 1.001 3800
-## beta0 -0.002 0.202 -0.402 -0.138 -0.003 0.136 0.393 1.002 2500
-## beta1 0.375 0.057 0.270 0.337 0.373 0.412 0.492 1.001 20000
-## deviance 25.723 2.037 23.760 24.288 25.103 26.451 31.239 1.001 16000
+## ED95 45.021 1.374 42.700 44.073 44.883 45.800 48.139 1.001 20000
+## OR 1.459 0.083 1.309 1.402 1.453 1.510 1.636 1.001 12000
+## P35 0.322 0.050 0.227 0.288 0.321 0.355 0.422 1.001 12000
+## beta0 -0.002 0.203 -0.400 -0.137 -0.002 0.133 0.395 1.001 20000
+## beta1 0.376 0.057 0.269 0.338 0.374 0.412 0.492 1.001 12000
+## deviance 25.733 2.053 23.762 24.272 25.085 26.530 31.161 1.001 20000
##
## For each parameter, n.eff is a crude measure of effective sample size,
## and Rhat is the potential scale reduction factor (at convergence, Rhat=1).
@@ -2090,24 +2115,24 @@ 84.7 Practical Bayesian Statistic
## pD = 2.1 and DIC = 27.8
## DIC is an estimate of expected predictive error (lower deviance is better).
所以,藥物每增加劑量1mg,有療效的比值比是OR = 1.46 (95%CrI: 1.31, 1.63)。能夠達到95%患者都有療效的劑量是 45.02 mg (95% CrI: 42.7, 48.2 mg)。如果給予患者藥物劑量爲 35 mg,患者的疼痛能夠得到緩解(有療效)的概率是 32.3% (95% CrI: 22.7%, 42.3%)。跟着看到這裏的你是不是也覺得貝葉斯的結果和過程能夠更加豐富地回答我們想知道的問題呢?
-#### PLOT THE DENSITY and HISTOGRAMS OF THE SAMPLED VALUES
-par(mfrow=c(3,1))
-Simulated <- coda::as.mcmc(post.jags)
-ggSample <- ggs(Simulated)
-
-ED95 <- ggSample %>%
- filter(Parameter == "ED95")
-OR <- ggSample %>%
- filter(Parameter == "OR")
-P35 <- ggSample %>%
- filter(Parameter == "P35")
-
-plot(density(ED95$value), main = "ED95 sample",
- ylab = "P(ED95)", xlab = "Distribution of ED95", col = "red")
-plot(density(OR$value), main = "OR sample",
- ylab = "P(OR)", xlab = "Distribution of OR", col = "red")
-plot(density(P35$value), main = "P35 sample",
- ylab = "P(P35)", xlab = "Distribution of P35", col = "red")
+#### PLOT THE DENSITY and HISTOGRAMS OF THE SAMPLED VALUES
+par(mfrow=c(3,1))
+Simulated <- coda::as.mcmc(post.jags)
+ggSample <- ggs(Simulated)
+
+ED95 <- ggSample %>%
+ filter(Parameter == "ED95")
+OR <- ggSample %>%
+ filter(Parameter == "OR")
+P35 <- ggSample %>%
+ filter(Parameter == "P35")
+
+plot(density(ED95$value), main = "ED95 sample",
+ ylab = "P(ED95)", xlab = "Distribution of ED95", col = "red")
+plot(density(OR$value), main = "OR sample",
+ ylab = "P(OR)", xlab = "Distribution of OR", col = "red")
+plot(density(P35$value), main = "P35 sample",
+ ylab = "P(P35)", xlab = "Distribution of P35", col = "red")
References
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/author.html b/docs/author.html
index 291b67325..be4492b41 100644
--- a/docs/author.html
+++ b/docs/author.html
@@ -6,7 +6,7 @@
我是誰 | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1365,7 +1388,7 @@ 我是誰
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/baseline-adjustment-using-ancova.html b/docs/baseline-adjustment-using-ancova.html
index e25da22cc..c08f84f10 100644
--- a/docs/baseline-adjustment-using-ancova.html
+++ b/docs/baseline-adjustment-using-ancova.html
@@ -6,7 +6,7 @@
第 34 章 Baseline Adjustment using ANCOVA | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1364,7 +1387,7 @@ 第 34 章 Baseline Adjustment us
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/bernoulli.html b/docs/bernoulli.html
index a1812a434..082bdc7ae 100644
--- a/docs/bernoulli.html
+++ b/docs/bernoulli.html
@@ -6,7 +6,7 @@
第 4 章 伯努利分佈 Bernoulli distribution | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1332,7 +1355,8 @@ 第 4 章 伯努利分佈 Bernoul
\]
-- 證明 根據方差的定義:
+- 證明
+根據方差的定義:
\[
@@ -1409,7 +1433,7 @@ 第 4 章 伯努利分佈 Bernoul
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/binomial.html b/docs/binomial.html
index abbc86c9e..f519d6121 100644
--- a/docs/binomial.html
+++ b/docs/binomial.html
@@ -6,7 +6,7 @@
第 5 章 二項分佈的概念 Binomial distribution | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1357,7 +1380,8 @@ 5.3 樂透中獎概率問題:
- 開獎時從 \(59\) 個號碼球中隨機抽取 \(6\) 個
- 如果六個號碼全部猜中(不分順序),你可以成爲百萬富翁。請問一次猜中全部 \(6\) 個號碼的概率是多少?
-從 \(59\) 個號碼中隨機取出任意 \(6\) 個號碼的方法有 \(^{59}C_6\) 種。 \[^{59}C_6=\frac{59!}{6!(59-6)!}=45,057,474\]
+從 \(59\) 個號碼中隨機取出任意 \(6\) 個號碼的方法有 \(^{59}C_6\) 種。
+\[^{59}C_6=\frac{59!}{6!(59-6)!}=45,057,474\]
每次選取六個號碼做爲一組的可能性相同,所以,你買了一組樂透號碼,能中獎的概率就是 \(1/45,057,474 = 0.00000002219\)。你還會再去買彩票麼?
5.3.1 如果我只想中其中的 \(3\) 個號碼,概率有多大?
@@ -1433,7 +1457,7 @@ 5.3.1 如果我只想中其中的
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/bookdown_files/figure-html/06-RobustStatistic-22-1.png b/docs/bookdown_files/figure-html/06-RobustStatistic-22-1.png
index eb818482c..3a5d62085 100644
Binary files a/docs/bookdown_files/figure-html/06-RobustStatistic-22-1.png and b/docs/bookdown_files/figure-html/06-RobustStatistic-22-1.png differ
diff --git a/docs/bookdown_files/figure-html/06-RobustStatistic-24-1.png b/docs/bookdown_files/figure-html/06-RobustStatistic-24-1.png
index 1d7680988..05888a81c 100644
Binary files a/docs/bookdown_files/figure-html/06-RobustStatistic-24-1.png and b/docs/bookdown_files/figure-html/06-RobustStatistic-24-1.png differ
diff --git a/docs/bookdown_files/figure-html/12-Bayesian-stats-6-1.png b/docs/bookdown_files/figure-html/12-Bayesian-stats-6-1.png
index 4a552e931..67266531a 100644
Binary files a/docs/bookdown_files/figure-html/12-Bayesian-stats-6-1.png and b/docs/bookdown_files/figure-html/12-Bayesian-stats-6-1.png differ
diff --git a/docs/bookdown_files/figure-html/12-Bayesian-stats-7-1.png b/docs/bookdown_files/figure-html/12-Bayesian-stats-7-1.png
index 1156d23d2..bf4b3d730 100644
Binary files a/docs/bookdown_files/figure-html/12-Bayesian-stats-7-1.png and b/docs/bookdown_files/figure-html/12-Bayesian-stats-7-1.png differ
diff --git a/docs/bookdown_files/figure-html/12-JAGS-stats-4-1.png b/docs/bookdown_files/figure-html/12-JAGS-stats-4-1.png
index 475bf244a..279dc0586 100644
Binary files a/docs/bookdown_files/figure-html/12-JAGS-stats-4-1.png and b/docs/bookdown_files/figure-html/12-JAGS-stats-4-1.png differ
diff --git a/docs/bookdown_files/figure-html/JAGS00-1.png b/docs/bookdown_files/figure-html/JAGS00-1.png
index 1a5a12e8a..7d594f793 100644
Binary files a/docs/bookdown_files/figure-html/JAGS00-1.png and b/docs/bookdown_files/figure-html/JAGS00-1.png differ
diff --git a/docs/bookdown_files/figure-html/JAGS01-1.png b/docs/bookdown_files/figure-html/JAGS01-1.png
index 4427f9203..62d9829f5 100644
Binary files a/docs/bookdown_files/figure-html/JAGS01-1.png and b/docs/bookdown_files/figure-html/JAGS01-1.png differ
diff --git a/docs/bookdown_files/figure-html/JAGS02-1.png b/docs/bookdown_files/figure-html/JAGS02-1.png
index b38d4c655..72ee3f09d 100644
Binary files a/docs/bookdown_files/figure-html/JAGS02-1.png and b/docs/bookdown_files/figure-html/JAGS02-1.png differ
diff --git a/docs/bookdown_files/figure-html/OpenBUGSPractical0407-1.png b/docs/bookdown_files/figure-html/OpenBUGSPractical0407-1.png
index ee72f0ee9..a336c8b83 100644
Binary files a/docs/bookdown_files/figure-html/OpenBUGSPractical0407-1.png and b/docs/bookdown_files/figure-html/OpenBUGSPractical0407-1.png differ
diff --git a/docs/bookdown_files/figure-html/OpenBUGSPractical040808-1.png b/docs/bookdown_files/figure-html/OpenBUGSPractical040808-1.png
index ddbec52e3..043bc8e63 100644
Binary files a/docs/bookdown_files/figure-html/OpenBUGSPractical040808-1.png and b/docs/bookdown_files/figure-html/OpenBUGSPractical040808-1.png differ
diff --git a/docs/bookdown_files/figure-html/OpenBUGSPractical0415-1.png b/docs/bookdown_files/figure-html/OpenBUGSPractical0415-1.png
index 3575414a4..c06104d91 100644
Binary files a/docs/bookdown_files/figure-html/OpenBUGSPractical0415-1.png and b/docs/bookdown_files/figure-html/OpenBUGSPractical0415-1.png differ
diff --git a/docs/bookdown_files/figure-html/OpenBUGSPractical0418-1.png b/docs/bookdown_files/figure-html/OpenBUGSPractical0418-1.png
index 970dc3a46..639ed85cb 100644
Binary files a/docs/bookdown_files/figure-html/OpenBUGSPractical0418-1.png and b/docs/bookdown_files/figure-html/OpenBUGSPractical0418-1.png differ
diff --git a/docs/bookdown_files/figure-html/OpenBUGSPractical0419-1.png b/docs/bookdown_files/figure-html/OpenBUGSPractical0419-1.png
index a787abe5b..b0c500d6a 100644
Binary files a/docs/bookdown_files/figure-html/OpenBUGSPractical0419-1.png and b/docs/bookdown_files/figure-html/OpenBUGSPractical0419-1.png differ
diff --git a/docs/bookdown_files/figure-html/OpenBUGSPractical0420-1.png b/docs/bookdown_files/figure-html/OpenBUGSPractical0420-1.png
index c10b5c5a1..86a9acdd3 100644
Binary files a/docs/bookdown_files/figure-html/OpenBUGSPractical0420-1.png and b/docs/bookdown_files/figure-html/OpenBUGSPractical0420-1.png differ
diff --git a/docs/bookdown_files/figure-html/OpenBUGSPractical0420-2.png b/docs/bookdown_files/figure-html/OpenBUGSPractical0420-2.png
index e713058bf..bdb0e9b8a 100644
Binary files a/docs/bookdown_files/figure-html/OpenBUGSPractical0420-2.png and b/docs/bookdown_files/figure-html/OpenBUGSPractical0420-2.png differ
diff --git a/docs/bookdown_files/figure-html/R-OpenBUGSPractical0425-1.png b/docs/bookdown_files/figure-html/R-OpenBUGSPractical0425-1.png
index 1fffb803a..2a7afc38a 100644
Binary files a/docs/bookdown_files/figure-html/R-OpenBUGSPractical0425-1.png and b/docs/bookdown_files/figure-html/R-OpenBUGSPractical0425-1.png differ
diff --git a/docs/bookdown_files/figure-html/propscore00-1.png b/docs/bookdown_files/figure-html/propscore00-1.png
index a8d28dbe8..71b05f5e5 100644
Binary files a/docs/bookdown_files/figure-html/propscore00-1.png and b/docs/bookdown_files/figure-html/propscore00-1.png differ
diff --git "a/docs/causal-languages-\345\233\240\346\236\234\346\216\250\346\226\267\347\232\204\350\252\236\346\263\225.html" "b/docs/causal-languages-\345\233\240\346\236\234\346\216\250\346\226\267\347\232\204\350\252\236\346\263\225.html"
index bd45c16ba..77a222e25 100644
--- "a/docs/causal-languages-\345\233\240\346\236\234\346\216\250\346\226\267\347\232\204\350\252\236\346\263\225.html"
+++ "b/docs/causal-languages-\345\233\240\346\236\234\346\216\250\346\226\267\347\232\204\350\252\236\346\263\225.html"
@@ -6,7 +6,7 @@
第 91 章 Causal Languages 因果推斷的語法 | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1403,7 +1426,10 @@ 91.2.1 初步分析
\]
-Definition 91.1
1. Estimand (被估計量): 我們想要知道的到底是什麼,例如,英國人口中收縮期血壓的平均值,這是一個無法知道,但是肯定存在確定量的量 (unknown fixed quantity)。
2. Estimator (估計量): 數據用來計算被估計量的某種方程,例如,隨機從街上找來100個人,測量他們的血壓,求出來的平均值,這是一個隨機變量 (random variable),會隨着隨機挑選的樣本而變化。
3. Estimate (估計): 給定數據下,計算獲得的具體的量,例如,我做了隨機抽取100名英國人血壓測量的實驗,獲得 130.4 mmHg 這個平均收縮期血壓的測量值,這是一個可以計算成爲已知的確定量 (known fixed quantity)。
+Definition 91.1
+1. Estimand (被估計量): 我們想要知道的到底是什麼,例如,英國人口中收縮期血壓的平均值,這是一個無法知道,但是肯定存在確定量的量 (unknown fixed quantity)。
+2. Estimator (估計量): 數據用來計算被估計量的某種方程,例如,隨機從街上找來100個人,測量他們的血壓,求出來的平均值,這是一個隨機變量 (random variable),會隨着隨機挑選的樣本而變化。
+3. Estimate (估計): 給定數據下,計算獲得的具體的量,例如,我做了隨機抽取100名英國人血壓測量的實驗,獲得 130.4 mmHg 這個平均收縮期血壓的測量值,這是一個可以計算成爲已知的確定量 (known fixed quantity)。
@@ -1437,8 +1463,9 @@ 91.3 更加正規的方法
91.3.1 因果推斷使用的語言
概率與統計學
概率論和統計學中使用的詞匯和語言,允許我們估計,描述很多觀察變量之間的聯合分布 (joint distribution),例如均值,方差,協方差,四分位,回歸系數,相關系數等。
-但是這一標準的統計學語言,卻無法描述這些聯合分布在受到外在影響或者幹預 (external intervention) 之後,會發生怎樣的變化。用這裏使用的例子來說就是,當所有患者都被提供了靜脈內鐵劑支持,(注意到這是和現實情況不一樣的) 患者的生存概率會是多少?
-因果關系的思考,其實是在追問這樣一個問題: 當暴露變量 \(X\),可以改變,並且以與我們觀察到的相反的形式出現,那麼結果變量 \(Y\) 會發生怎樣的變化?本書,我們使用 (Neyman 1923) 當年創立,後來被 (D. B. Rubin 1974) 發展的概念: 潛在結果框架 (potential outcome framework)。
+但是這一標準的統計學語言,卻無法描述這些聯合分布在受到外在影響或者幹預 (external intervention) 之後,會發生怎樣的變化。用這裏使用的例子來說就是,當所有患者都被提供了靜脈內鐵劑支持,(注意到這是和現實情況不一樣的)
+患者的生存概率會是多少?
+因果關系的思考,其實是在追問這樣一個問題: 當暴露變量 \(X\),可以改變,並且以與我們觀察到的相反的形式出現,那麼結果變量 \(Y\) 會發生怎樣的變化?本書,我們使用 (Neyman 1923) 當年創立,後來被 (Rubin 1974) 發展的概念: 潛在結果框架 (potential outcome framework)。
Definition 91.2 潛在結果 Potential outcome: 定義 \(Y(x)\) 爲當暴露變量 \(X\) 取假設的值 (x) 時,結果變量的取值。
@@ -1447,7 +1474,9 @@ 91.3.1 因果推斷使用的語
對於一個而二分類暴露變量 \(X\),每個個體/研究對象,我們賦予它一個潛在的可能取值結果的概念: \(Y(0)\) 和 \(Y(1)\)。\(Y(0)\) 的意思是當暴露變量是 \(0\) 的時候該對象可能的取值,\(Y(1)\) 的意思是當暴露變量是 \(1\) 的時候,該對象可能的取值。但是,在現實情況下,我們只能觀察到二者中的一種結果。在我們的例題中,當一個患者真的接受了靜脈鐵劑補充,那麼他/她的觀察結果 \(Y = Y(1)\),也就是此時觀察結果等於暴露爲 \((1)\) 時的潛在結果 \(Y(1)\)。對與這個患者來說,他/她在沒有接受經脈鐵劑補充的情況下的另一種潛在結果 \(Y(0)\) 是沒有被觀測到的。但是,這個患者的潛在結果 \(Y(0)\),表示的是他/她如果接受沒有接受靜脈鐵劑補充的話,他/她在90天時死亡/存活的潛在結果。
-Definition 91.3 用潛在結果表示邊緣和條件因果對比 marginal and conditional causal contrast:
邊緣對比: \[E\{Y(1)\} - E\{ Y(0) \}\] 條件對比: \[E\{ Y(1) | \mathbf{V=v} \} - E\{ Y(0) | \mathbf{V=v} \}\]
+Definition 91.3 用潛在結果表示邊緣和條件因果對比 marginal and conditional causal contrast:
+邊緣對比: \[E\{Y(1)\} - E\{ Y(0) \}\]
+條件對比: \[E\{ Y(1) | \mathbf{V=v} \} - E\{ Y(0) | \mathbf{V=v} \}\]
這種潛在結果的統計框架,曾經被 (Dawid 2000) 批判爲一種不恰當的方法。以爲潛在結果框架建議說存在另一個完全不存在的平行宇宙,使得觀察對象在一個空間裏做一件事,在另一個空間裏做一件相反的事情,看會發生怎樣的結果,這實際是在說存在這種潛在的完美聯合分布。另外,我們會在第四章中看見的,在潛在結果的框架下,我們可能問的問題是,當所有人都接受了暴露變量時,暴露和結果之間的因果效應的平均值:
@@ -2088,7 +2117,9 @@ 91.3.4 鑑定 identification
\]
-Definition 91.4 標準化和 g computation 公式: \[\text{Pr}\{ Y(x) = 1 \} = \sum_c\text{Pr}(Y=1|X=x,C=c)\text{Pr}(C=c)\] 是我們在因果推斷中說的 g computation 公式的簡單例子。這個過程在流行病學中,被命名爲標準化 standardisation。所以,條件因果效應 (conditional causal effect):
+Definition 91.4 標準化和 g computation 公式:
+\[\text{Pr}\{ Y(x) = 1 \} = \sum_c\text{Pr}(Y=1|X=x,C=c)\text{Pr}(C=c)\]
+是我們在因果推斷中說的 g computation 公式的簡單例子。這個過程在流行病學中,被命名爲標準化 standardisation。所以,條件因果效應 (conditional causal effect):
\[\text{Pr}(Y=1 | X=1, C=c) - \text{Pr}(Y=1 | X=1, C=c)\]
在這個 g computation 的過程中,被根據條件變量 C 在人羣衆的分布給標準化了。就是在這個根據條件變量的分布標準化 (或者叫邊際化 marginalisation) 的過程中,條件效應的含義華麗轉身變成了邊際因果效應 (marginal causal risk difference)。
@@ -2099,7 +2130,8 @@ 91.3.4 鑑定 identification
鑑定 identification 指的是,因果被估計量利用因果推斷的假設把無法觀察的概率分布用可以觀察的數據的分布推導計算的過程。
估計 estimation 指的是,當我們對因果關系鑑定完畢之後,接下來進行的用實際觀察數據來估計被估計量的過程。這個過程通常不需要再進行公式推導,會使用統計模型,這些統計模型自己又自帶另外的一些前提假設。
所以,鑑定過程的前提假設是因果推斷的命根,最最底層的前提。接下來的數據計算或者模型擬合帶來的別的假設和鑑定過程的假設有本質的區別。區分這兩個過程的另一目的還包括鑑定過程的前提假設基本上是無法找到統計學方法進行檢驗的 (untestable structual assumptions) 結構性假設。
-對機器學習的一點點暗示: 在因果推斷中新興的一個重要話題 – 機器學習 (machine learning,或者叫做 data-adaptive estimation techniques 數據適應性估計技巧) 在當今大數據時代顯得特別突出。常有人認爲,數據適應性估計技巧可以用於預測,但是不能用於因果推斷。這其實只能是說對了一部分。機器學習本身,其實是在給定了 (一大堆) 變量之後,尋找某個變量的最佳預測量。但是從我們目前爲止在因果推斷中的推導來看,相信你也能看出來,因果推斷本身也包括了預測的過程。因果推斷的第一個部分 – 鑑定過程是處理的是因果之間的前提假設,以及判斷因果推斷中用到的參數怎樣和已觀察到的數據在這裏因果條件下連接起來 – 這個部分是不能放到機器中去的。但是因果推斷的第二部分 – 估計 – 就是純粹的預測過程啦。這裏想強調的是機器學習的方法,可以被用在因果推斷的第二部分,而不是第一部分。第一部分還是要由人來完成。(Laan and Rose 2017)
+對機器學習的一點點暗示:
+在因果推斷中新興的一個重要話題 – 機器學習 (machine learning,或者叫做 data-adaptive estimation techniques 數據適應性估計技巧) 在當今大數據時代顯得特別突出。常有人認爲,數據適應性估計技巧可以用於預測,但是不能用於因果推斷。這其實只能是說對了一部分。機器學習本身,其實是在給定了 (一大堆) 變量之後,尋找某個變量的最佳預測量。但是從我們目前爲止在因果推斷中的推導來看,相信你也能看出來,因果推斷本身也包括了預測的過程。因果推斷的第一個部分 – 鑑定過程是處理的是因果之間的前提假設,以及判斷因果推斷中用到的參數怎樣和已觀察到的數據在這裏因果條件下連接起來 – 這個部分是不能放到機器中去的。但是因果推斷的第二部分 – 估計 – 就是純粹的預測過程啦。這裏想強調的是機器學習的方法,可以被用在因果推斷的第二部分,而不是第一部分。第一部分還是要由人來完成。(Laan and Rose 2017)
@@ -2224,7 +2256,7 @@ References
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/causal-mediation-analysis.html b/docs/causal-mediation-analysis.html
index 8fa085138..ac3f6da35 100644
--- a/docs/causal-mediation-analysis.html
+++ b/docs/causal-mediation-analysis.html
@@ -6,7 +6,7 @@
第 97 章 Causal mediation analysis | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1364,7 +1387,7 @@ 第 97 章 Causal mediation analy
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/chi-square-distribution.html b/docs/chi-square-distribution.html
index 0b7922b6d..9fa1404fc 100644
--- a/docs/chi-square-distribution.html
+++ b/docs/chi-square-distribution.html
@@ -6,7 +6,7 @@
第 11 章 卡方分佈 Chi-square distribution | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1301,7 +1324,8 @@ 第 11 章 卡方分佈 Chi-squar
11.1 卡方分佈的期望和方差的證明
當 \(X\sim N(0,1)\) 時, \(X^2\sim \mathcal{X}_1^2\)
-如果 \(X_1, \dots, X_n\stackrel{i.i.d}{\sim} N(0,1)\), 那麼 \(\sum_{i=1}^nX_i^2\sim\mathcal{X}_n^2\)
+如果 \(X_1, \dots, X_n\stackrel{i.i.d}{\sim} N(0,1)\),
+那麼 \(\sum_{i=1}^nX_i^2\sim\mathcal{X}_n^2\)
其中: \(\mathcal{X}_n^2\) 表示自由度爲 \(n\) 的卡方分佈。
且 \(X_m^2+X_n^2=\mathcal{X}_{m+n}^2\)
@@ -1334,7 +1358,8 @@ 11.3.1 下面來求 \(u=x^3, v=e^{(-\frac{x^2}{2})},t=-\frac{x^2}{2}\) 可以推導:
+
令 \(u=x^3, v=e^{(-\frac{x^2}{2})},t=-\frac{x^2}{2}\)
+可以推導:
\[
\begin{aligned}
\frac{dv}{dx} &= \frac{dv}{dt}\frac{dt}{dx} \\
@@ -1368,7 +1393,8 @@ 11.3.1 下面來求 \(I=\int_{-\infty}^{+\infty}e^{(-\frac{x^2}{2})}dx\\ \Rightarrow I^2=\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty}e^{(-\frac{x^2+y^2}{2})}dxdy\)
接下來需要用到 座標轉換的知識,將 \(x,y\) 表示的笛卡爾座標,轉換爲用角度 \(\theta\) 和半徑 \(r\) 表示的形式。之後的證明可以在油管上看到,但是我還是繼續證明下去。
-直角座標系 (cartesian coordinators) 和 極座標系 (polar coordinators) 之間轉換的關係如下:
+直角座標系 (cartesian coordinators) 和
+極座標系 (polar coordinators) 之間轉換的關係如下:
\[
\begin{aligned}
x&=r\:cos\theta\\
@@ -1376,7 +1402,8 @@ 11.3.1 下面來求 \(E(X_1^4)\)。 在那之前我們先求 \(I^2\)。 注意轉換座標系統以後,\(\theta\in[0,2\pi], r\in[0,+\infty]\)
+座標轉換以後可以繼續求 \(E(X_1^4)\)。 在那之前我們先求 \(I^2\)。
+注意轉換座標系統以後,\(\theta\in[0,2\pi], r\in[0,+\infty]\)
\[
\begin{aligned}
I^2 &= \int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty}e^{(-\frac{x^2+y^2}{2})}dxdy \\
@@ -1415,7 +1442,8 @@ 11.4 把上面的推導擴展
-結論:\(X_1, \dots, X_n\stackrel{i.i.d}{\sim} N(0,1)\) 時,\(\sum_{i=1}^nX_i^2\sim\mathcal{X}_n^2\) 服從卡方分佈,其期望 \(E(X_n^2)=n\),方差 \(Var(X_n^2)=2n\)。 根據中心極限定理(Section 8)
+結論:\(X_1, \dots, X_n\stackrel{i.i.d}{\sim} N(0,1)\) 時,\(\sum_{i=1}^nX_i^2\sim\mathcal{X}_n^2\) 服從卡方分佈,其期望 \(E(X_n^2)=n\),方差 \(Var(X_n^2)=2n\)。
+根據中心極限定理(Section 8)
\[n\rightarrow \infty, X_n^2\sim N(n, 2n)\]
@@ -1480,7 +1508,7 @@ 11.4 把上面的推導擴展
第 66 章 Cluster analysis/unsupervised learning 聚類分析 | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1502,7 +1525,7 @@ References
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git "a/docs/cox-\346\257\224\344\276\213\351\242\250\351\232\252\346\250\241\345\236\213.html" "b/docs/cox-\346\257\224\344\276\213\351\242\250\351\232\252\346\250\241\345\236\213.html"
index 7730d9fd0..cf44ce7b6 100644
--- "a/docs/cox-\346\257\224\344\276\213\351\242\250\351\232\252\346\250\241\345\236\213.html"
+++ "b/docs/cox-\346\257\224\344\276\213\351\242\250\351\232\252\346\250\241\345\236\213.html"
@@ -6,7 +6,7 @@
第 74 章 Cox 比例風險模型 | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1301,7 +1324,7 @@ 第 74 章 Cox 比例風險模型
\[
h(t|x) = h_0(t)e^{\beta^Tx}
\]
-其中,\(h_0(t)\) 是被比較的基線組成員(baseline individual)的風險度 (hazard),在 Weibull 模型或者指數模型中,這個基線風險 (baseline hazard) 是需要被模型根據數據來進行參數估計的 (parameterized)。但是,1972年,神一樣的人物 Cox (D. R. Cox 1972) 提出,其實我們不需要對這個基線風險進行“無謂”的估計,可以無視它在模型中的存在。正因為如此,這個模型被冠以發明者的名字 Cox proportional hazards model。因為此模型不對基線風險進行任何估計,但是對預測變量對於風險的效果 (effect of the explanatory variable) 用模型中的 \(\beta\) 進行參數估計,所以,它又是一種典型的半參數化模型 (semi-parametric model)。
+其中,\(h_0(t)\) 是被比較的基線組成員(baseline individual)的風險度 (hazard),在 Weibull 模型或者指數模型中,這個基線風險 (baseline hazard) 是需要被模型根據數據來進行參數估計的 (parameterized)。但是,1972年,神一樣的人物 Cox (Cox 1972) 提出,其實我們不需要對這個基線風險進行“無謂”的估計,可以無視它在模型中的存在。正因為如此,這個模型被冠以發明者的名字 Cox proportional hazards model。因為此模型不對基線風險進行任何估計,但是對預測變量對於風險的效果 (effect of the explanatory variable) 用模型中的 \(\beta\) 進行參數估計,所以,它又是一種典型的半參數化模型 (semi-parametric model)。
Cox 比例風險模型下的似然:
\[
L= \prod_{i=1}^n\{ h_0(t)e^{\beta^Tx_i}\exp(-\int_0^{t_i} h_0(u)e^{\beta^Tx_i}du) \}^{\delta_i}\{ \exp(-\int_0^{t_i} h_0(u)e^{\beta^Tx_i}du) \}^{1-\delta_i}
@@ -1406,7 +1429,7 @@ References
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/crude-and-stratified-rate-ratios.html b/docs/crude-and-stratified-rate-ratios.html
index 9fb019f7c..29fa91f18 100644
--- a/docs/crude-and-stratified-rate-ratios.html
+++ b/docs/crude-and-stratified-rate-ratios.html
@@ -6,7 +6,7 @@
第 98 章 Crude and stratified rate ratios | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1300,29 +1323,29 @@
第 98 章 Crude and stratified rate ratios
本章內容不討論任何理論的東西,着重強調用 R 進行實際數據的分析,並加強對輸出結果的理解。
此次實戰演練的目的是學會怎樣計算死亡率比 (Rate Ratios, RR)。學會用 Mantel-Haenszel 法總結 RR,並討論其意義。
-whitehal <- read_dta("backupfiles/whitehal.dta")
-whitehal$followupyrs <- (whitehal$timeout - whitehal$timein)/365.25
-max(whitehal$followupyrs*365.25) # time difference in days
+
## Time difference of 7078.9927 days
-summary(whitehal$followupyrs <- as.numeric(whitehal$followupyrs)) # time difference in years
+
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.15063 17.16357 17.96020 16.46116 18.56262 19.38123
-# categorize agein into groups (40-44, 45-49, 50-54, ... , 65-69)
-whitehal$agecat <- cut(whitehal$agein, breaks = seq(40, 70, 5), right = FALSE)
-with(whitehal, table(agecat))
+# categorize agein into groups (40-44, 45-49, 50-54, ... , 65-69)
+whitehal$agecat <- cut(whitehal$agein, breaks = seq(40, 70, 5), right = FALSE)
+with(whitehal, table(agecat))
## agecat
## [40,45) [45,50) [50,55) [55,60) [60,65) [65,70)
## 277 445 362 340 215 38
-# examine how mortality rates change with age at entry
-#
-# with(whitehal %>% group_by(agecat) %>%
-# summarise(D = sum(all),
-# Y = sum(followupyrs)),
-# cbind(whitehal$agecat, pois.exact(x = D, pt = Y/1000)))
-
-
-## rate ratios and 95% CIs for each age category compare with [40,44) age group
-Model0 <- glm(all ~ agecat + offset(log(followupyrs)), family = poisson(link = "log"), data = whitehal); ci.exp(Model0)
+# examine how mortality rates change with age at entry
+#
+# with(whitehal %>% group_by(agecat) %>%
+# summarise(D = sum(all),
+# Y = sum(followupyrs)),
+# cbind(whitehal$agecat, pois.exact(x = D, pt = Y/1000)))
+
+
+## rate ratios and 95% CIs for each age category compare with [40,44) age group
+Model0 <- glm(all ~ agecat + offset(log(followupyrs)), family = poisson(link = "log"), data = whitehal); ci.exp(Model0)
## exp(Est.) 2.5% 97.5%
## (Intercept) 0.0048794197 0.0032705278 0.0072797841
## agecat[45,50) 1.1740971320 0.7154062934 1.9268827913
@@ -1330,40 +1353,40 @@ 第 98 章 Crude and stratified r
## agecat[55,60) 4.5784055712 2.9519678778 7.1009572062
## agecat[60,65) 6.6882727538 4.2856745766 10.4377949444
## agecat[65,70) 17.1110624279 10.1140257533 28.9487553770
-## The rate ratios are increasing with age although there is no statistical evidence
-## at 5% level that the rate among 45-49 year olds is different to the rate among men
-## who are <40 years
-
-
-# with(whitehal %>% group_by(grade) %>%
-# summarise(D = sum(all),
-# Y = sum(followupyrs)),
-# cbind(whitehal$grade, pois.exact(x = D, pt = Y/1000)))
-
-Model1 <- glm(all ~ factor(grade) + offset(log(followupyrs)), family = poisson(link = "log"), data = whitehal); ci.exp(Model1)
+## The rate ratios are increasing with age although there is no statistical evidence
+## at 5% level that the rate among 45-49 year olds is different to the rate among men
+## who are <40 years
+
+
+# with(whitehal %>% group_by(grade) %>%
+# summarise(D = sum(all),
+# Y = sum(followupyrs)),
+# cbind(whitehal$grade, pois.exact(x = D, pt = Y/1000)))
+
+Model1 <- glm(all ~ factor(grade) + offset(log(followupyrs)), family = poisson(link = "log"), data = whitehal); ci.exp(Model1)
## exp(Est.) 2.5% 97.5%
## (Intercept) 0.010865404 0.0095233128 0.012396632
## factor(grade)2 2.305446287 1.8947528380 2.805158793
-## There is strong evidence that the all cause mortality rate differs between high
-## and low grade workers.
-
-## To examine whether the estimated RR for grade is confounded by age at entry
-## we compare the crude RR =2.31 (1.90, 2.81) with the Mantel-Haenszel summary
-## estimate.
-
-whitehal_table <- aggregate(cbind(all, followupyrs) ~ grade + agecat, data=whitehal, sum)
-stmh_array <- array(c(4, 20, 693.1284,4225.4893,
- 10,35, 1363.821,6491.072,
- 30,52, 1399.63, 4660.12, 51,67, 1832.169,3449.846,
- 59,42, 1660.597,1434.251,
- 28,5, 316.23840, 79.00879),
- dim=c(2,2,6),
- dimnames = list(
- Grade=c("2","1"),
- c("death", "Person_years"),
- Agecat=names(table(whitehal$agecat))
- ))
-stmh_array
+## There is strong evidence that the all cause mortality rate differs between high
+## and low grade workers.
+
+## To examine whether the estimated RR for grade is confounded by age at entry
+## we compare the crude RR =2.31 (1.90, 2.81) with the Mantel-Haenszel summary
+## estimate.
+
+whitehal_table <- aggregate(cbind(all, followupyrs) ~ grade + agecat, data=whitehal, sum)
+stmh_array <- array(c(4, 20, 693.1284,4225.4893,
+ 10,35, 1363.821,6491.072,
+ 30,52, 1399.63, 4660.12, 51,67, 1832.169,3449.846,
+ 59,42, 1660.597,1434.251,
+ 28,5, 316.23840, 79.00879),
+ dim=c(2,2,6),
+ dimnames = list(
+ Grade=c("2","1"),
+ c("death", "Person_years"),
+ Agecat=names(table(whitehal$agecat))
+ ))
+stmh_array
## , , Agecat = [40,45)
##
##
@@ -1405,8 +1428,8 @@ 第 98 章 Crude and stratified r
## Grade death Person_years
## 2 28 316.23840
## 1 5 79.00879
-mhgrade_age <- epi.2by2(stmh_array, method = "cohort.time", units = 1000)
-mhgrade_age
+
## Outcome + Time at risk Inc rate *
## Exposed + 182 7266 25.0
## Exposed - 221 20340 10.9
@@ -1424,22 +1447,22 @@ 第 98 章 Crude and stratified r
## Wald confidence limits
## M-H: Mantel-Haenszel; CI: confidence interval
## * Outcomes per 1000 units of population time at risk
-## Overall estimate and Wald 95% confidence intervals,
-## controlling for agecate
-mhgrade_age$massoc$IRR.mh.wald
+## Overall estimate and Wald 95% confidence intervals,
+## controlling for agecate
+mhgrade_age$massoc$IRR.mh.wald
## est lower upper
## 1 1.429211 1.1598631 1.7611078
-mhgrade_age$massoc$chisq.mh ## p-value for age-adjusted MH rate ratio
+
## test.statistic df p.value
## 1 11.132745 5 0.00084816981
-## The Mantel-Haenszel summary estimate RR = 1.43 (1.16, 1.76).
-## The result shows that the crude estimate of the effect of grade was
-## partly confounded by age at entry.
-
-## To assess whether there is effect modification betwee grade and
-## agecat we examine the stratum specific estimates and assess
-## whether there is evidence of important variation between them.
-mhgrade_age$massoc$IRR.strata.wald
+## The Mantel-Haenszel summary estimate RR = 1.43 (1.16, 1.76).
+## The result shows that the crude estimate of the effect of grade was
+## partly confounded by age at entry.
+
+## To assess whether there is effect modification betwee grade and
+## agecat we examine the stratum specific estimates and assess
+## whether there is evidence of important variation between them.
+mhgrade_age$massoc$IRR.strata.wald
## est lower upper
## 1 1.2192515 0.30302945 3.6397113
## 2 1.3598500 0.60057144 2.8059055
@@ -1447,13 +1470,13 @@ 第 98 章 Crude and stratified r
## 4 1.4332751 0.97576189 2.0941999
## 5 1.2132872 0.80308583 1.8474726
## 6 1.3991002 0.53338106 4.6404658
-## The result indicates that the data are compatible with the assumption
-## of no interaction/effect modification (p=0.79)
-
-## test for unequal RRs (effect modification):
-mhgrade_age$res$RR.homog
+## The result indicates that the data are compatible with the assumption
+## of no interaction/effect modification (p=0.79)
+
+## test for unequal RRs (effect modification):
+mhgrade_age$res$RR.homog
## NULL
-## Hence, we do not need to present the stratum-specific estimates.
+
@@ -1517,7 +1540,7 @@ 第 98 章 Crude and stratified r
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/further-issues.html b/docs/further-issues.html
index 4aae46eb1..67c0d03ba 100644
--- a/docs/further-issues.html
+++ b/docs/further-issues.html
@@ -6,7 +6,7 @@
第 70 章 Further issues | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1364,7 +1387,7 @@ 第 70 章 Further issues
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/generalized-estimating-equation.html b/docs/generalized-estimating-equation.html
index e9e6be1cb..f848505b4 100644
--- a/docs/generalized-estimating-equation.html
+++ b/docs/generalized-estimating-equation.html
@@ -6,7 +6,7 @@
第 65 章 Generalized Estimating Equation | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1360,7 +1383,7 @@ 第 65 章 Generalized Estimating
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git "a/docs/graphical-models-\345\233\240\346\236\234\346\216\250\346\226\267\347\232\204\345\234\226\345\275\242\346\250\241\345\236\213.html" "b/docs/graphical-models-\345\233\240\346\236\234\346\216\250\346\226\267\347\232\204\345\234\226\345\275\242\346\250\241\345\236\213.html"
index 927862d98..06f41c3a1 100644
--- "a/docs/graphical-models-\345\233\240\346\236\234\346\216\250\346\226\267\347\232\204\345\234\226\345\275\242\346\250\241\345\236\213.html"
+++ "b/docs/graphical-models-\345\233\240\346\236\234\346\216\250\346\226\267\347\232\204\345\234\226\345\275\242\346\250\241\345\236\213.html"
@@ -6,7 +6,7 @@
第 92 章 Graphical Models 因果推斷的圖形模型 | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1318,21 +1341,21 @@ 92.1 統計學中的有向無環
\end{aligned}
\]
那麼這些變量之間關系對應的 DAG 圖就是這樣子:
-g <- dagitty('dag {
- V6 [pos="2,0"]
- V4 [pos="1,-1"]
- V5 [pos="1,1"]
- V2 [pos="-1,-1"]
- V3 [pos="-1,1"]
- V1 [pos="-2,0"]
-
- V1 -> V4
- V1 -> V5 -> V6
- V1 -> V3 -> V4
- V2 -> V3 -> V4
- V2 -> V5 -> V6
-}')
-plot(g)
+g <- dagitty('dag {
+ V6 [pos="2,0"]
+ V4 [pos="1,-1"]
+ V5 [pos="1,1"]
+ V2 [pos="-1,-1"]
+ V3 [pos="-1,1"]
+ V1 [pos="-2,0"]
+
+ V1 -> V4
+ V1 -> V5 -> V6
+ V1 -> V3 -> V4
+ V2 -> V3 -> V4
+ V2 -> V5 -> V6
+}')
+plot(g)
92.2.2 D 分離 d-separation
3.& V_3 \rightarrow V_4 \leftarrow V_1 \rightarrow V_5 \rightarrow V_6
\end{aligned}
\]
-其中,第 2,3 條通路,是被 \(V_2\) 阻斷了的,但是第 1 條沒有被 \(V_2\) 阻斷。 因此,我不能說 \(V_2\) 把 \(V_3\) 到 \(V_6\) 之間的全部通路阻斷 (d-分離,d-separation)。
+其中,第 2,3 條通路,是被 \(V_2\) 阻斷了的,但是第 1 條沒有被 \(V_2\) 阻斷。
+因此,我不能說 \(V_2\) 把 \(V_3\) 到 \(V_6\) 之間的全部通路阻斷 (d-分離,d-separation)。
@@ -1524,7 +1548,7 @@ 92.2.2 D 分離 d-separation
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/index.html b/docs/index.html
index 2e7114272..be362c23f 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -6,7 +6,7 @@
醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1299,7 +1322,7 @@
醫學統計學
-2020-02-25
+2020-03-12
前言
@@ -1311,7 +1334,9 @@ 前言
在開始倒計時離開倫敦的時刻,我再次翻閱這些思考過的,痛苦過的,糾結過的,忐忑過的,這一年的學習筆記,感慨良多。倫敦衛生與熱帶醫學院 曾經是,現在依然還是我魂牽夢繞的學院,它的歷史積澱,它的小巧精緻,在它的樓道里度過的每一天都是那麼的充實而值得感動。這本書不僅僅是我的統計學學習的心路歷程,還傾注了這裡每一位老師,每一個一起奮鬥過的同學,我們的歡聲笑語,我們的喜怒哀樂。如果你的電腦/手機/iPad屏幕上打開了這本書,說明你將要或者已經是我的同仁,如果此生有幸相聚,我一定會敬你一杯。祝你學業有成,和我一起用數據科學解開這個世界的奧秘。為更平等的醫療,為了更自由的人類社會,奮鬥永不停止。
-王超辰
2017年9月於倫敦
2018年7月於倫敦衛生與熱帶醫學院
+王超辰
+2017年9月於倫敦
+2018年7月於倫敦衛生與熱帶醫學院
@@ -1319,10 +1344,10 @@ 前言
References
-Xie, Yihui. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. http://yihui.name/knitr/.
+Xie, Yihui. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. http://yihui.name/knitr/.
-Xie, Yihui. 2018. Bookdown: Authoring Books and Technical Documents with R Markdown. https://CRAN.R-project.org/package=bookdown.
+Xie, Yihui. 2018. Bookdown: Authoring Books and Technical Documents with R Markdown. https://CRAN.R-project.org/package=bookdown.
@@ -1386,7 +1411,7 @@ References
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/interaction.html b/docs/interaction.html
index 6f98bdf89..d767feee5 100644
--- a/docs/interaction.html
+++ b/docs/interaction.html
@@ -6,7 +6,7 @@
第 32 章 交互作用 Interactions | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1367,13 +1390,13 @@ 32.1.3 兩個二分類變量之
32.1.4 兩個連續變量之間的交互作用
前面 (Section 32.1.2) 已經討論過,一個是連續型變量 \(X_1\),另一個是分類變量時 \(X_2\),線性迴歸的交互作用項迴歸係數的含義是因變量 \(Y\) 和連續性變量 \(X_1\) 在不同的 \(X_2\) 組中的迴歸係數之差(斜率之差)。但是,當兩個預測變量 \(X_1, X_2\) 都是連續型變量時,交互作用項的迴歸係數該如何解釋呢?直觀的說,此時的交互作用項迴歸係數應該被理解爲:預測變量 \(X_2\) 每增加一個單位時,\(Y, X_1\) 之間關係的迴歸方程的斜率變化。爲了更好地解釋這個概念,我們沿用前面兒童年齡和身長預測其身高的模型 (Section 30.3.3),加入年齡和身高的交互作用項結果如下:
-growgam1 <- read_dta("backupfiles/growgam1.dta")
-growgam1$sex <- as.factor(growgam1$sex)
-
-Model1 <- lm(wt ~ age + len + age*len, data=growgam1)
-# or equivalently use lm(wt ~ age*len, data=growgam1)
-
-summary(Model1)
+growgam1 <- read_dta("backupfiles/growgam1.dta")
+growgam1$sex <- as.factor(growgam1$sex)
+
+Model1 <- lm(wt ~ age + len + age*len, data=growgam1)
+# or equivalently use lm(wt ~ age*len, data=growgam1)
+
+summary(Model1)
##
## Call:
## lm(formula = wt ~ age + len + age * len, data = growgam1)
@@ -1394,7 +1417,7 @@ 32.1.4 兩個連續變量之間
## Residual standard error: 0.94 on 186 degrees of freedom
## Multiple R-squared: 0.753, Adjusted R-squared: 0.749
## F-statistic: 189 on 3 and 186 DF, p-value: <2e-16
-confint(Model1)
+
## 2.5 % 97.5 %
## (Intercept) -8.2859887 -0.753130
## age -0.4919562 -0.077844
@@ -1425,17 +1448,17 @@ 32.1.4 兩個連續變量之間
\end{aligned}
\]
所以你會看到每個給定的兒童年齡時的方程身長預測體重的方程都是線性方程,截距和斜率都在變化。兒童的年齡每增加 \(1\) 個月,身長和體重的相關係數增加 \(0.0034 \text{kg/cm}\)。除了迴歸係數,其餘的數字都是不能用正常的數據來理解的 (沒有兒童身長 或者 體重會等於零)。如果非要解釋,那麼需要把數據全部中心化 (Section 27.3.1)。
-epiDisplay::summ(growgam1$age, graph=FALSE); epiDisplay::summ(growgam1$len, graph=FALSE)
+
## obs. mean median s.d. min. max.
## 190 16.979 16 8.337 5 36
## obs. mean median s.d. min. max.
## 190 76.697 76.05 7.156 60.1 95.5
-growgam1$age_c <- growgam1$age-mean(growgam1$age)
-growgam1$len_c <- growgam1$len-mean(growgam1$len)
-
-Model2 <- lm(wt ~ age_c + len_c + age_c*len_c, data=growgam1)
-# or equivalently use lm(wt ~ age_c*len_c, data=growgam1)
-summary(Model2)
+growgam1$age_c <- growgam1$age-mean(growgam1$age)
+growgam1$len_c <- growgam1$len-mean(growgam1$len)
+
+Model2 <- lm(wt ~ age_c + len_c + age_c*len_c, data=growgam1)
+# or equivalently use lm(wt ~ age_c*len_c, data=growgam1)
+summary(Model2)
##
## Call:
## lm(formula = wt ~ age_c + len_c + age_c * len_c, data = growgam1)
@@ -1456,7 +1479,7 @@ 32.1.4 兩個連續變量之間
## Residual standard error: 0.94 on 186 degrees of freedom
## Multiple R-squared: 0.753, Adjusted R-squared: 0.749
## F-statistic: 189 on 3 and 186 DF, p-value: <2e-16
-confint(Model2)
+
## 2.5 % 97.5 %
## (Intercept) 9.2822177 9.657345
## age_c -0.0582290 0.009680
@@ -1531,7 +1554,7 @@ 32.1.4 兩個連續變量之間
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
- var src = "";
+ var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
diff --git a/docs/intro-Bayes.html b/docs/intro-Bayes.html
index 1abc3e054..c5edbfcac 100644
--- a/docs/intro-Bayes.html
+++ b/docs/intro-Bayes.html
@@ -6,7 +6,7 @@
第 40 章 貝葉斯統計入門 | 醫學統計學
-
+
@@ -24,7 +24,7 @@
-
+
@@ -61,41 +61,66 @@
@@ -575,7 +600,7 @@
- 38.1 定義
- 39 The sandwich estimator
-- VII 貝葉斯統計
+- VII 貝葉斯統計 Introduction to Bayesian Statistics
- 40 貝葉斯統計入門
- 40.1 概率論推斷的複習
- 40.2 貝葉斯概率推理/逆概率 Bayesian reasoning/inverse probability
@@ -871,18 +896,17 @@
- 61.5 隨機效應的方差
- 61.6 模型效果評估
- 61.7 練習題
-- 61.7.1 將數據導入軟件裏,
-- 61.7.2 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
-- 61.7.3 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
-- 61.7.4 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
-- 61.7.5 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
-- 61.7.6 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
-- 61.7.7 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
-- 61.7.8 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
-- 61.7.9 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
-- 61.7.10 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
-- 61.7.11 計算這個模型的第二階級(level 2,
school
level)的殘差。
-- 61.7.12 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
+- 61.7.1 先忽略學校編號爲 48 的學校,擬合一個只有固定效應 (簡單線性回歸模型),結果變量是 GCSE,解釋變量是 LRT 和學校。
+- 61.7.2 僅有固定效應模型的學校變量變更爲學校類型 (男校女校或混合校),從這個新模型的結果來看,你是否認爲學校類型,和學校編號本身相比能夠解釋相同的學校層面的方差?
lrt
的估計回歸參數發生了怎樣的變化?
+- 61.7.3 使用限制性極大似然法擬合一個隨機截距模型。記錄此時的限制性對數似然的大小 (log-likelihood)。用
lmerTest::rand
命令對隨機效應部分的方差是否爲零做檢驗,指明該檢驗的零假設是什麼,並解釋其結果的含義。
+- 61.7.4 在前一題的隨機截距模型中加入
schgend
變量,作爲解釋隨機截距的一個自變量,觀察輸出結果,解釋其是否有意義。記錄這個模型的限制性似然。
+- 61.7.5 擬合隨機截距隨機斜率模型,固定效應部分的
lrt
也加入進隨機效應部分。
+- 61.7.6 通過上面幾個模型計算獲得的似然,嘗試檢驗隨機斜率標準差,以及該標準差和隨機截距標準差的協相關是否有意義。
+- 61.7.7 模型中的
schgend
改成 mean_girl
會給出怎樣的結果呢?
+- 61.7.8 現在我們把注意力改爲關心學校編號爲 48 的學校的情況。用且禁用它一所學校的數據,擬合一個簡單線性回歸,結果變量是
gcse
,解釋變量是 lrt
。
+- 61.7.9 這次不排除 48 號學校,擬合所有學校的數據進入
Fixed_reml2
模型中去,結果有發生顯著的變化嗎?
+- 61.7.10 計算這個模型的第二階級(level 2,
school
level)的殘差。
+- 61.7.11 計算這個模型的第一階級(level 1, student)殘差,分析其分布,查看第48所學校的殘差表現如何。
- 62 縱向研究數據 longitudinal data 1
@@ -1152,8 +1176,7 @@
- 87.1.2 雙重索引BUGS語言標記法
- 87.2 邏輯回歸 Bayesian Logistic Regression
- 87.3 貝葉斯泊鬆回歸 Bayesian Poisson Regression
- 87.4 GLM in a Bayesian way
@@ -1301,7 +1324,7 @@ 第 40 章 貝葉斯統計入門<
@@ -1318,16 +1341,18 @@ 第 40 章 貝葉斯統計入門<
推薦書目:
-- “Principles of Statistical Inference” by D.R. Cox (D. Cox 2006)
+- “Principles of Statistical Inference” by D.R. Cox (Cox 2006)
- “Bayesian Data Analysis” by Gelman, Carlin, Stern, Dunson, Vehtari, and Rubin (Gelman et al. 2013), website for the book
- “Bayesian Biostatistics” by Vehtari and Rubin (Lesaffre and Lawson 2012)
貝葉斯統計推斷,提供了不同於概率論推斷的另一種考察和解決問題的思路。所有的思考,都源於貝葉斯定理 Bayes’ Theorem (Section 2)。起源於英國統計學家托馬斯貝葉斯 (Thomas Bayes) 死後被好友 Richard Price 整理發表的論文: “An essay towards solving a problem in the doctrine of chances.”
概率論推斷與貝葉斯推斷的中心都圍繞似然 likelihood (Section 12) 的概念。然而二者對似然提供的信息之理解和解釋完全不同。即在對於觀察數據提供的信息的理解,和如何應用已有信息來影響未來決策(或提供預測)的問題上常常被認爲是統計學中形成鮮明對比的兩種哲學理念。過去幾個世紀二者之間孰優孰劣的爭論相當激烈。但是,從實際應用的角度來看,我們目前更關心哪種思維能更加實用地描述和模擬真實世界。幸運地是,多數情況下,二者的差距不大。所以無法簡單地從一個實驗或者一次爭論中得出誰更出色的結論。現在的統計學家們通常不再如同信仰之爭那樣的互相水火不容,而是從實用性角度來判斷一些實際情況下,採用哪種思想能使計算過程更加簡便或者計算結果更加接近真實情況。
-請思考如下的問題: 什麼是概率? What is probability?
+請思考如下的問題:
+什麼是概率? What is probability?
- 概率論思想下的定義:某事件在多次重複觀察實驗結果中發生次數所佔的比例。
The probability of an event is the limit of its relative frequency in a large number of trials."
-- 貝葉斯思想下的定義:概率是你相信某事件會發生的可能性。
Probability is a measure of the degree of belief about an event.
+- 貝葉斯思想下的定義:概率是你相信某事件會發生的可能性。
Probability is a measure of the degree of belief
+about an event.
40.1 概率論推斷的複習
@@ -1400,7 +1425,7 @@ 40.2.1 演繹推理 deductive rea
40.2.2 如何給可能性定量 Quantifying plausibility
-進行可能性定量之前,R.T. Cox 制定了如下的規則(R. T. Cox 1946):
+進行可能性定量之前,R.T. Cox 制定了如下的規則(Cox 1946):
- \(\text{plausibility}(A)\) 是一個有邊界的實數;
- 傳遞性,transitivity:如果
@@ -1442,7 +1467,8 @@
40.2.2 如何給可能性定量 Q
40.3 貝葉斯推理的統計學實現
-在經典概率論中,概率分佈的標記 \(f_X(x;\theta)\) 的涵義爲: 對於一個隨機變量 \(X\),它在我們假設的某種固定的真實(上帝才知道是多少的)參數 \(\theta\) 的分佈框架下,不斷重複相同的實驗之後獲得的概率分佈。
+在經典概率論中,概率分佈的標記 \(f_X(x;\theta)\) 的涵義爲:
+對於一個隨機變量 \(X\),它在我們假設的某種固定的真實(上帝才知道是多少的)參數 \(\theta\) 的分佈框架下,不斷重複相同的實驗之後獲得的概率分佈。
在貝葉斯統計推理中,一切都被看作是一個服從概率分佈的隨機變量。利用貝葉斯定理,我們將先驗隨機概率分佈 (prior probability distribution),和觀察數據作條件概率 (condition on the observed data),從而獲得事後概率分佈 (posterior probability distribution)。
40.3.1 醫學診斷測試 diagnostic testing
@@ -1507,7 +1533,7 @@ 40.3.3 說點小歷史
圖 40.1: Sir Ronald Fisher
-Ronald Aylmer Fisher (1890-1962) 推動了統計學在20世紀前半頁的重大發展。他鞏固了概率論統計學堅實的基礎,並且積極提倡這一套理論(R. A. Fisher 1922)。但是 Fisher 本人對於統計學的“統計學意義, level of significance” 的認識卻是隨着時間和他年齡的變化而變化的:
+Ronald Aylmer Fisher (1890-1962) 推動了統計學在20世紀前半頁的重大發展。他鞏固了概率論統計學堅實的基礎,並且積極提倡這一套理論(Fisher 1922)。但是 Fisher 本人對於統計學的“統計學意義, level of significance” 的認識卻是隨着時間和他年齡的變化而變化的: