Skip to content

Commit

Permalink
update for R 4.3.2, rstan 2.32.3, and ggplot2 3.4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
MatsuuraKentaro committed Dec 23, 2023
1 parent cad55cb commit 4813774
Show file tree
Hide file tree
Showing 116 changed files with 140 additions and 123 deletions.
41 changes: 28 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,22 @@ source('ex1.R')
```

## StanやRStanのバージョンアップに伴う補足
[こちら](update.md)
RStan 2.32.3 (2023年10月リリース)ではStan 2.26.1という比較的安定しているバージョンを使っています(2023/12/23現在の最新のStanは2.33.0 (2023年9月リリース))。

もし`rstan`パッケージが将来的に(3年後?5年後?)最新のStanを使用するときが来ましたら、本書にある配列の書き方を以下のように修正して読んでください。

| 修正前 | 修正後 |
|:------------|:------------|
| `real X[N];`| `array[N] real X;` |
| `int<lower=0> Y[N];` | `array[N] int<lower=0> Y;` |
| `vector[K] V[N];` | `array[N] vector[K] V;` |

この記法以外に古くなる部分は当面の間ありません。「はじめに」に書いた通り、モデリングの内容自体は簡単に古くなるものではなく、現在でもStanを使った統計モデリングを習得するにはベストな書籍と思っています。

[古いバージョンからの差分はこちら](update.md)

## `cmdstanr`パッケージとの使い分け
`cmdstanr`パッケージは最新のStanのリリースからそこまで間をあけずにリリースされる傾向があるため、最新のStanの機能を使いたい人には`cmdstanr`パッケージがオススメです。個人的には`rstan``cmdstanr`の使用感に大きな差はありません。`cmdstanr`は発展途中であり、推定結果からMCMCサンプルを取り出すときは`rstan`のほうが少し手間が少なく入門者向きと思います。

## 正誤表
[こちら](errata.md)
Expand All @@ -41,17 +56,17 @@ source('ex1.R')
| 主なソフトやパッケージ名 | 執筆時点 | サポートページ内の現状 |
|:-----------|:------------|:------------|
| OS | Windows 7 SP1 (64bit) | Windows 10 (64bit) |
| R | 3.3.1 | 4.1.2 |
| Rtools | Rtools34 | Rtools40v2 |
| rstan | 2.11.1 | 2.21.3 |
| ggplot | 2.1.0 | 3.3.5 |
| R | 3.3.1 | 4.3.2 |
| Rtools | Rtools34 | Rtools43 |
| rstan | 2.11.1 | 2.32.3 |
| ggplot | 2.1.0 | 3.4.4 |
| ggmcmc | 1.1 | 1.5.1.1 |
| GGally | 1.1.0 | 2.1.2 |
| patchwork | - | 1.1.1 |
| ellipse | 0.3.8 | 0.4.2 |
| hexbin | 1.27.1 | 1.28.2 |
| ggtern | 2.1.1 | 3.3.5 |
| ggrepel | 0.5 | 0.9.1 |
| mvtnorm | 1.0.5 | 1.1-3 |
| gtools | 3.5.0 | 3.9.2 |
| GGally | 1.1.0 | 2.2.0 |
| patchwork | - | 1.1.3 |
| ellipse | 0.3.8 | 0.5.0 |
| hexbin | 1.27.1 | 1.28.3 |
| ggtern | 2.1.1 | 3.4.2 |
| ggrepel | 0.5 | 0.9.4 |
| mvtnorm | 1.0.5 | 1.2-4 |
| gtools | 3.5.0 | 3.9.5 |
| NipponMap | - | 0.2 |
4 changes: 2 additions & 2 deletions chap04/exercise/ex4.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load('result-ex3.RData')

ms <- extract(fit)

prob <- mean(ms$mu1 < ms$mu2) #=> 0.9325
prob <- mean(ms$mu1 < ms$mu2) #=> 0.9265

# N_mcmc <- length(ms$mu1)
# prob <- sum(ms$mu1 < ms$mu2)/N_mcmc #=> 0.9325
# prob <- sum(ms$mu1 < ms$mu2)/N_mcmc #=> 0.9265
Binary file modified chap04/exercise/fig-ex1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions chap04/fig4-3.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ p <- ggplot() +
theme_bw(base_size=18) +
geom_ribbon(data=conf_95, aes(x=X, ymin=lwr, ymax=upr), alpha=1/6) +
geom_ribbon(data=conf_50, aes(x=X, ymin=lwr, ymax=upr), alpha=2/6) +
geom_line(data=conf_50, aes(x=X, y=fit), size=1) +
geom_line(data=conf_50, aes(x=X, y=fit), linewidth=1) +
geom_point(data=d, aes(x=X, y=Y), shape=1, size=3) +
labs(x='X', y='Y') + coord_cartesian(xlim=c(22, 61), ylim=c(200, 1400)) +
scale_y_continuous(breaks=seq(from=200, to=1400, by=400))
Expand All @@ -27,7 +27,7 @@ p <- ggplot() +
theme_bw(base_size=18) +
geom_ribbon(data=pred_95, aes(x=X, ymin=lwr, ymax=upr), alpha=1/6) +
geom_ribbon(data=pred_50, aes(x=X, ymin=lwr, ymax=upr), alpha=2/6) +
geom_line(data=pred_50, aes(x=X, y=fit), size=1) +
geom_line(data=pred_50, aes(x=X, y=fit), linewidth=1) +
geom_point(data=d, aes(x=X, y=Y), shape=1, size=3) +
labs(x='X', y='Y') + coord_cartesian(xlim=c(22, 61), ylim=c(200, 1400)) +
scale_y_continuous(breaks=seq(from=200, to=1400, by=400))
Expand Down
4 changes: 2 additions & 2 deletions chap04/fig4-7.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ p_x <- ggplot(df_mcmc, aes(x=a)) +
theme_bw(base_size=18) +
theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.text.y=element_blank(), axis.ticks.x=element_blank(), axis.ticks.y=element_blank()) +
coord_cartesian(xlim=x_range) +
geom_histogram(aes(y=..density..), colour='black', fill='white') +
geom_histogram(aes(y=after_stat(density)), colour='black', fill='white') +
geom_density(alpha=0.3, fill='gray20') +
scale_x_continuous(breaks=x_breaks) +
labs(x='', y='')
Expand All @@ -30,7 +30,7 @@ p_y <- ggplot(df_mcmc, aes(x=b)) +
theme_bw(base_size=18) +
theme(axis.title.y=element_blank(), axis.text.x=element_blank(), axis.text.y=element_blank(), axis.ticks.x=element_blank(), axis.ticks.y=element_blank()) +
coord_flip(xlim=y_range) +
geom_histogram(aes(y=..density..), colour='black', fill='white') +
geom_histogram(aes(y=after_stat(density)), colour='black', fill='white') +
geom_density(alpha=0.3, fill='gray20') +
scale_x_continuous(breaks=y_breaks) +
labs(x='', y='')
Expand Down
4 changes: 2 additions & 2 deletions chap04/fig4-8.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ p <- ggplot() +
theme_bw(base_size=18) +
geom_ribbon(data=d_est, aes(x=X, ymin=`2.5%`, ymax=`97.5%`), fill='black', alpha=1/6) +
geom_ribbon(data=d_est, aes(x=X, ymin=`25%`, ymax=`75%`), fill='black', alpha=2/6) +
geom_line(data=d_est, aes(x=X, y=`50%`), size=1) +
geom_line(data=d_est, aes(x=X, y=`50%`), linewidth=1) +
geom_point(data=d, aes(x=X, y=Y), shape=1, size=3) +
coord_cartesian(xlim=c(22, 61), ylim=c(200, 1400)) +
scale_y_continuous(breaks=seq(from=200, to=1400, by=400)) +
Expand All @@ -47,7 +47,7 @@ p <- ggplot() +
theme_bw(base_size=18) +
geom_ribbon(data=d_est, aes(x=X, ymin=`2.5%`, ymax=`97.5%`), fill='black', alpha=1/6) +
geom_ribbon(data=d_est, aes(x=X, ymin=`25%`, ymax=`75%`), fill='black', alpha=2/6) +
geom_line(data=d_est, aes(x=X, y=`50%`), size=1) +
geom_line(data=d_est, aes(x=X, y=`50%`), linewidth=1) +
geom_point(data=d, aes(x=X, y=Y), shape=1, size=3) +
coord_cartesian(xlim=c(22, 61), ylim=c(200, 1400)) +
scale_y_continuous(breaks=seq(from=200, to=1400, by=400)) +
Expand Down
Binary file modified chap04/output/fig4-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap04/output/fig4-8-right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions chap04/run-model4-4.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ p <- ggplot() +
theme_bw(base_size=18) +
geom_ribbon(data=d_est, aes(x=X, ymin=`2.5%`, ymax=`97.5%`), fill='black', alpha=1/6) +
geom_ribbon(data=d_est, aes(x=X, ymin=`25%`, ymax=`75%`), fill='black', alpha=2/6) +
geom_line(data=d_est, aes(x=X, y=`50%`), size=1) +
geom_line(data=d_est, aes(x=X, y=`50%`), linewidth=1) +
geom_point(data=d, aes(x=X, y=Y), shape=1, size=3) +
coord_cartesian(xlim=c(22, 61), ylim=c(200, 1400)) +
scale_y_continuous(breaks=seq(from=200, to=1400, by=400)) +
Expand All @@ -29,7 +29,7 @@ p <- ggplot() +
theme_bw(base_size=18) +
geom_ribbon(data=d_est, aes(x=X, ymin=`2.5%`, ymax=`97.5%`), fill='black', alpha=1/6) +
geom_ribbon(data=d_est, aes(x=X, ymin=`25%`, ymax=`75%`), fill='black', alpha=2/6) +
geom_line(data=d_est, aes(x=X, y=`50%`), size=1) +
geom_line(data=d_est, aes(x=X, y=`50%`), linewidth=1) +
geom_point(data=d, aes(x=X, y=Y), shape=1, size=3) +
coord_cartesian(xlim=c(22, 61), ylim=c(200, 1400)) +
scale_y_continuous(breaks=seq(from=200, to=1400, by=400)) +
Expand Down
Binary file modified chap05/exercise/fig-ex5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion chap05/fig5-1.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ for (i in 1:N_col) {
} else {
bw <- (max(x)-min(x))/10
p <- p + geom_histogram(binwidth=bw, aes(fill=A), color='grey5') +
geom_line(eval(bquote(aes(y=..count..*.(bw)))), stat='density')
geom_line(aes(y=after_stat(count)*bw), stat='density')
}
p <- p + geom_label(data=data.frame(x=-Inf, y=Inf, label=colnames(d)[i]), aes(x=x, y=y, label=label), hjust=0, vjust=1) +
scale_fill_manual(values=alpha(c('white', 'grey40'), 0.5))
Expand Down
2 changes: 1 addition & 1 deletion chap05/fig5-10.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ d_est$A <- as.factor(d_est$A)
p <- ggplot(data=d_est, aes(x=Y, y=`50%`)) +
theme_bw(base_size=18) +
coord_flip() +
geom_violin(trim=FALSE, size=1, color='grey80') +
geom_violin(trim=FALSE, linewidth=1, color='grey80') +
geom_point(aes(color=A), position=position_jitter(w=0.3, h=0), size=0.5) +
scale_color_manual(values=c('grey5', 'grey50')) +
labs(x='Y', y='q')
Expand Down
2 changes: 1 addition & 1 deletion chap05/fig5-11.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ p <- ggplot(data=d_est, aes(x=X, y=`50%`)) +
coord_fixed(ratio=1, xlim=c(0,1), ylim=c(0,1)) +
geom_abline(intercept=0, slope=1, alpha=0.5) +
geom_ribbon(aes(ymin=`10%`, ymax=`90%`), fill='black', alpha=2/6) +
geom_line(size=1) +
geom_line(linewidth=1) +
labs(x='False Positive', y='True Positive')
ggsave(file='output/fig5-11.png', plot=p, dpi=300, w=4, h=4)
2 changes: 1 addition & 1 deletion chap05/fig5-4.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bw <- 0.01
p <- ggplot(data=d_mode, aes(x=X)) +
theme_bw(base_size=18) +
geom_histogram(binwidth=bw, color='black', fill='white') +
geom_density(eval(bquote(aes(y=..count..*.(bw)))), alpha=0.5, color='black', fill='gray20') +
geom_density(aes(y=after_stat(count)*bw), alpha=0.5, color='black', fill='gray20') +
geom_rug(sides='b') +
stat_function(fun=function(x) nrow(d)*bw*dnorm(x, mean=0, sd=s_MAP), linetype='dashed') +
labs(x='value', y='density') +
Expand Down
2 changes: 1 addition & 1 deletion chap05/fig5-5.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ for (i in 1:N_col) {
theme_bw(base_size=14) +
theme(axis.text.x=element_text(angle=60, vjust=1, hjust=1)) +
geom_histogram(binwidth=bw, fill='white', color='grey5') +
geom_line(eval(bquote(aes(y=..count..*.(bw)))), stat='density') +
geom_line(eval(bquote(aes(y=after_stat(count)*.(bw)))), stat='density') +
geom_label(data=data.frame(x=-Inf, y=Inf, label=colnames(d)[i]), aes(x=x, y=y, label=label), hjust=0, vjust=1)
ggp <- putPlot(ggp, p, i, i)
}
Expand Down
2 changes: 1 addition & 1 deletion chap05/fig5-6.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ for (i in 1:N_col) {
} else {
bw <- (max(x)-min(x))/10
p <- p + geom_histogram(aes(fill=A), color='grey20', binwidth=bw) +
geom_line(eval(bquote(aes(y=..count..*.(bw)))), stat='density')
geom_line(eval(bquote(aes(y=after_stat(count)*.(bw)))), stat='density')
}
p <- p + geom_label(data=data.frame(x=-Inf, y=Inf, label=colnames(d)[i]), aes(x=x, y=y, label=label), hjust=0, vjust=1) +
scale_fill_manual(values=alpha(c('white', 'grey40'), 0.5))
Expand Down
2 changes: 1 addition & 1 deletion chap05/fig5-9.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ d$A <- as.factor(d$A)
p <- ggplot(d_est, aes(x=X, y=`50%`)) +
theme_bw(base_size=18) +
geom_ribbon(aes(ymin=`10%`, ymax=`90%`), fill='black', alpha=2/6) +
geom_line(size=1) +
geom_line(linewidth=1) +
geom_point(data=subset(d, A==0 & Weather=='A'), aes(x=Score, y=Y, color=A),
position=position_jitter(w=0, h=0.25), size=0.5) +
labs(x='Score', y='q') +
Expand Down
Binary file modified chap05/output/fig5-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap05/output/fig5-10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap05/output/fig5-4-left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap05/output/fig5-4-right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap05/output/fig5-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap05/output/fig5-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap05/output/fig5-9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap06/exercise/fig-ex3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion chap06/fig6-1.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ d <- data.frame(X=c(-Inf,-2,1), Xend=c(-2,1,Inf), Y=c(0,1/3,0), Yend=c(0,1/3,0))

p <- ggplot() +
theme_bw(base_size=18) +
geom_segment(data=d, aes(x=X, y=Y, xend=Xend, yend=Yend), size=2) +
geom_segment(data=d, aes(x=X, y=Y, xend=Xend, yend=Yend), linewidth=2) +
geom_point(data=data.frame(X=c(-2,1), Y=c(0,0)), aes(X,Y), size=3, shape=21, fill='white') +
geom_point(data=data.frame(X=c(-2,1), Y=c(1/3,1/3)), aes(X,Y), size=3, shape=21, fill='black') +
labs(x='y', y='density') + xlim(-3,3)
Expand Down
4 changes: 2 additions & 2 deletions chap06/fig6-4.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ p <- ggplot(data.frame(X=c(0, 1)), aes(x=X)) +
theme_bw(base_size=18) +
theme(legend.position='bottom', legend.direction ='vertical', legend.key.width=grid::unit(2.5,'line')) +
mapply(
function(a, b, co) stat_function(fun=dbeta, args=list(shape1=a, shape2=b), aes_q(linetype=co)),
function(a, b, co) stat_function(fun=dbeta, args=list(shape1=a, shape2=b), aes(linetype=co)),
d_para$a, d_para$b, d_para$gr
) +
scale_linetype_manual('parameter', values=c('solid', '52', '12'), labels=my_labs) +
Expand All @@ -22,7 +22,7 @@ p <- ggplot(data.frame(X=c(0, 1)), aes(x=X)) +
theme_bw(base_size=18) +
theme(legend.position='bottom', legend.direction ='vertical', legend.key.width=grid::unit(2.5,'line')) +
mapply(
function(a, b, co) stat_function(fun=dbeta, args=list(shape1=a, shape2=b), aes_q(linetype=co)),
function(a, b, co) stat_function(fun=dbeta, args=list(shape1=a, shape2=b), aes(linetype=co)),
d_para$a, d_para$b, d_para$gr
) +
scale_linetype_manual('parameter', values=c('solid', '52', '12'), labels=my_labs) +
Expand Down
8 changes: 4 additions & 4 deletions chap06/fig6-6.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ plot.tern <- function(d, file) {
theme(
plot.margin=margin(-5,-5,-5,-5,'mm'),
tern.axis.line=element_line(color=gray(0.2)),
tern.panel.grid.major=element_line(size=0.25, color=gray(0.2)),
tern.panel.grid.major=element_line(linewidth=0.25, color=gray(0.2)),
tern.axis.text=element_text(size=14, color=gray(0.4)),
tern.axis.arrow.show=TRUE,
tern.axis.arrow=element_line(color='black'),
tern.axis.arrow.sep=0.13
) +
theme_anticlockwise() + theme_rotate(45) +
Tlab('', labelarrow='$\\theta_2') +
Rlab('', labelarrow='$\\theta_1') +
Llab('', labelarrow='$\\theta_3') +
Tlab('', labelarrow='$\\theta_2$') +
Rlab('', labelarrow='$\\theta_1$') +
Llab('', labelarrow='$\\theta_3$') +
scale_T_continuous(breaks=lab_seq, labels=lab_seq) +
scale_R_continuous(breaks=lab_seq, labels=lab_seq) +
scale_L_continuous(breaks=lab_seq, labels=lab_seq) +
Expand Down
2 changes: 1 addition & 1 deletion chap06/fig6-7.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ p <- ggplot(data.frame(X=c(0, 5)), aes(x=X)) +
theme_bw(base_size=18) +
theme(legend.key.width=grid::unit(2.5,'line')) +
mapply(
function(b, co) stat_function(fun=dexp, args=list(rate=b), aes_q(linetype=co)),
function(b, co) stat_function(fun=dexp, args=list(rate=b), aes(linetype=co)),
d_para$b, d_para$gr
) +
scale_linetype_manual('parameter', values=c('solid', '12'), labels=my_labs) +
Expand Down
4 changes: 2 additions & 2 deletions chap06/fig6-9.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ p <- ggplot(data.frame(X=c(0, 6)), aes(x=X)) +
theme_bw(base_size=18) +
theme(legend.position='bottom', legend.direction ='vertical', legend.key.width=grid::unit(2.5,'line')) +
mapply(
function(a, b, co) stat_function(fun=dgamma, args=list(shape=a, rate=b), aes_q(linetype=co)),
function(a, b, co) stat_function(fun=dgamma, args=list(shape=a, rate=b), aes(linetype=co)),
d_para$a, d_para$b, d_para$gr
) +
scale_linetype_manual('parameter', values=c('solid', '52', '12'), labels=my_labs) +
Expand All @@ -24,7 +24,7 @@ p <- ggplot(data.frame(X=c(0, 6)), aes(x=X)) +
theme_bw(base_size=18) +
theme(legend.position='bottom', legend.direction ='vertical', legend.key.width=grid::unit(2.5,'line')) +
mapply(
function(a, b, co) stat_function(fun=dgamma, args=list(shape=a, rate=b), aes_q(linetype=co)),
function(a, b, co) stat_function(fun=dgamma, args=list(shape=a, rate=b), aes(linetype=co)),
d_para$a, d_para$b, d_para$gr
) +
scale_linetype_manual('parameter', values=c('solid', '52', '12'), labels=my_labs) +
Expand Down
Binary file modified chap06/output/fig6-6-left-bottom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap06/output/fig6-6-left-top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap06/output/fig6-6-right-bottom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap06/output/fig6-6-right-top.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion chap07/fig7-12-left.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ p <- ggplot() +
theme_bw(base_size=18) +
geom_ribbon(data=d_est, aes(x=X, ymin=`2.5%`, ymax=`97.5%`), fill='black', alpha=1/6) +
geom_ribbon(data=d_est, aes(x=X, ymin=`25%`, ymax=`75%`), fill='black', alpha=2/6) +
geom_line(data=d_est, aes(x=X, y=`50%`), size=1) +
geom_line(data=d_est, aes(x=X, y=`50%`), linewidth=1) +
geom_point(data=d, aes(x=X, y=Y), shape=1, size=3) +
labs(x='X', y='Y') +
coord_cartesian(xlim=c(-0.2, 11.2), ylim=c(-25, 75))
Expand Down
2 changes: 1 addition & 1 deletion chap07/fig7-12-right.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ p <- ggplot() +
theme_bw(base_size=18) +
geom_ribbon(data=d_est, aes(x=X, ymin=`2.5%`, ymax=`97.5%`), fill='black', alpha=1/6) +
geom_ribbon(data=d_est, aes(x=X, ymin=`25%`, ymax=`75%`), fill='black', alpha=2/6) +
geom_line(data=d_est, aes(x=X, y=`50%`), size=1) +
geom_line(data=d_est, aes(x=X, y=`50%`), linewidth=1) +
geom_point(data=d, aes(x=X, y=Y), shape=1, size=3) +
labs(x='X', y='Y') +
coord_cartesian(xlim=c(-0.2, 11.2), ylim=c(-25, 75))
Expand Down
2 changes: 1 addition & 1 deletion chap07/fig7-2-left.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ p <- ggplot() +
theme_bw(base_size=18) +
geom_ribbon(data=d_est, aes(x=X, ymin=`10%`, ymax=`90%`), fill='black', alpha=1/6) +
geom_ribbon(data=d_est, aes(x=X, ymin=`25%`, ymax=`75%`), fill='black', alpha=2/6) +
geom_line(data=d_est, aes(x=X, y=`50%`), size=1) +
geom_line(data=d_est, aes(x=X, y=`50%`), linewidth=1) +
geom_point(data=d, aes(x=X, y=Y), shape=1, size=2) +
coord_cartesian(xlim=c(11, 118), ylim=c(-50, 1900)) +
labs(x='Area', y='Y')
Expand Down
2 changes: 1 addition & 1 deletion chap07/fig7-2-right.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ p <- ggplot() +
theme_bw(base_size=18) +
geom_ribbon(data=d_est, aes(x=X, ymin=`10%`, ymax=`90%`), fill='black', alpha=1/6) +
geom_ribbon(data=d_est, aes(x=X, ymin=`25%`, ymax=`75%`), fill='black', alpha=2/6) +
geom_line(data=d_est, aes(x=X, y=`50%`), size=1) +
geom_line(data=d_est, aes(x=X, y=`50%`), linewidth=1) +
geom_point(data=d, aes(x=X, y=Y), shape=1, size=2) +
coord_cartesian(xlim=c(11, 118), ylim=c(-50, 1900)) +
labs(x='Area', y='Y')
Expand Down
2 changes: 1 addition & 1 deletion chap07/fig7-4-left.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bw <- 25
p <- ggplot(data=d_mode, aes(x=X)) +
theme_bw(base_size=18) +
geom_histogram(binwidth=bw, color='black', fill='white') +
geom_density(eval(bquote(aes(y=..count..*.(bw)))), alpha=0.5, color='black', fill='gray20') +
geom_density(aes(y=after_stat(count)*bw), alpha=0.5, color='black', fill='gray20') +
geom_rug(sides='b') +
stat_function(fun=function(x) nrow(d)*bw*dnorm(x, mean=0, sd=s_MAP), linetype='dashed') +
labs(x='value', y='count') +
Expand Down
2 changes: 1 addition & 1 deletion chap07/fig7-4-right.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bw <- 0.02
p <- ggplot(data=d_mode, aes(x=X)) +
theme_bw(base_size=18) +
geom_histogram(binwidth=bw, color='black', fill='white') +
geom_density(eval(bquote(aes(y=..count..*.(bw)))), alpha=0.5, color='black', fill='gray20') +
geom_density(aes(y=after_stat(count)*bw), alpha=0.5, color='black', fill='gray20') +
geom_rug(sides='b') +
stat_function(fun=function(x) nrow(d)*bw*dnorm(x, mean=0, sd=s_MAP), linetype='dashed') +
labs(x='value', y='count') +
Expand Down
2 changes: 1 addition & 1 deletion chap07/fig7-5-right.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ p <- ggplot() +
theme_bw(base_size=18) +
geom_ribbon(data=d_est, aes(x=X, ymin=`2.5%`, ymax=`97.5%`), fill='black', alpha=1/6) +
geom_ribbon(data=d_est, aes(x=X, ymin=`25%`, ymax=`75%`), fill='black', alpha=2/6) +
geom_line(data=d_est, aes(x=X, y=`50%`), size=0.5) +
geom_line(data=d_est, aes(x=X, y=`50%`), linewidth=0.5) +
geom_point(data=d, aes(x=X, y=Y), shape=1, size=2) +
labs(x='X', y='Y') +
scale_y_continuous(breaks=seq(from=0, to=100, by=50), limits=c(0, 130))
Expand Down
2 changes: 1 addition & 1 deletion chap07/fig7-6-left.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ d <- read.csv('input/data-conc.txt')

p <- ggplot(data=d, aes(x=Time, y=Y)) +
theme_bw(base_size=18) +
geom_line(size=1) +
geom_line(linewidth=1) +
geom_point(size=3) +
labs(x='Time (hour)', y='Y') +
scale_x_continuous(breaks=d$Time, limit=c(0,24)) +
Expand Down
2 changes: 1 addition & 1 deletion chap07/fig7-6-right.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ p <- ggplot() +
theme_bw(base_size=18) +
geom_ribbon(data=d_est, aes(x=X, ymin=`2.5%`, ymax=`97.5%`), fill='black', alpha=1/6) +
geom_ribbon(data=d_est, aes(x=X, ymin=`25%`, ymax=`75%`), fill='black', alpha=2/6) +
geom_line(data=d_est, aes(x=X, y=`50%`), size=0.5) +
geom_line(data=d_est, aes(x=X, y=`50%`), linewidth=0.5) +
geom_point(data=d, aes(x=Time, y=Y), size=3) +
labs(x='Time (hour)', y='Y') +
scale_x_continuous(breaks=d$Time, limit=c(0, 24)) +
Expand Down
6 changes: 3 additions & 3 deletions chap07/fig7-7.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ library(ggplot2)

p <- ggplot(data=data.frame(X=c(0,5)), aes(x=X)) +
theme_bw(base_size=18) + theme(legend.key.width=grid::unit(3,'line')) +
stat_function(fun=function(x) 2*exp(-1*x), aes(linetype='1'), size=1) +
stat_function(fun=function(x) 1.8/(1+50*exp(-2*x)), aes(linetype='2'), size=1) +
stat_function(fun=function(x) 8*(exp(-x) - exp(-2*x)), aes(linetype='3'), size=1) +
stat_function(fun=function(x) 2*exp(-1*x), aes(linetype='1'), linewidth=1) +
stat_function(fun=function(x) 1.8/(1+50*exp(-2*x)), aes(linetype='2'), linewidth=1) +
stat_function(fun=function(x) 8*(exp(-x) - exp(-2*x)), aes(linetype='3'), linewidth=1) +
scale_linetype_manual(values=c('solid', '52', '12')) +
labs(linetype='Model', x='Time', y='y')
ggsave(file='output/fig7-7.png', plot=p, dpi=300, w=5, h=3)
Binary file modified chap07/output/fig7-4-left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap07/output/fig7-4-right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap07/output/fig7-5-right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chap07/output/fig7-6-right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion chap08/exercise/ex1.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ p <- ggplot(d, aes(x=X, y=Y, shape=KID)) +
theme_bw(base_size=18) + theme(legend.key.width=grid::unit(2.5,'line')) +
facet_wrap(~KID) +
geom_ribbon(data=d2, aes(y=`50%`, ymin=`2.5%`, ymax=`97.5%`), fill='black', alpha=1/5) +
geom_line(data=d2, aes(y=`50%`), size=1, alpha=0.8) +
geom_line(data=d2, aes(y=`50%`), linewidth=1, alpha=0.8) +
geom_point(size=3) +
scale_shape_manual(values=c(16, 2, 4, 9)) +
labs(x='X', y='Y', shape='KID')
Expand Down
2 changes: 1 addition & 1 deletion chap08/exercise/ex2.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ p <- ggplot(d, aes(x=X, y=Y, shape=KID)) +
theme_bw(base_size=18) + theme(legend.key.width=grid::unit(2.5,'line')) +
facet_wrap(~KID) +
geom_ribbon(data=d3, aes(y=`50%`, ymin=`2.5%`, ymax=`97.5%`), fill='black', alpha=1/5) +
geom_line(data=d3, aes(y=`50%`), size=1, alpha=0.8) +
geom_line(data=d3, aes(y=`50%`), linewidth=1, alpha=0.8) +
geom_point(size=3) +
scale_shape_manual(values=c(16, 2, 4, 9)) +
labs(x='X', y='Y', shape='KID')
Expand Down
Loading

0 comments on commit 4813774

Please sign in to comment.