Skip to content

Commit

Permalink
+ theme readable, toc, code link
Browse files Browse the repository at this point in the history
  • Loading branch information
avehtari committed Jun 4, 2020
1 parent 37b175c commit 60de48a
Show file tree
Hide file tree
Showing 163 changed files with 2,971 additions and 1,975 deletions.
16 changes: 11 additions & 5 deletions AgePeriodCohort/births.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
#' title: "Regression and Other Stories: AgePeriodCohort"
#' author: "Andrew Gelman, Jennifer Hill, Aki Vehtari"
#' date: "`r format(Sys.Date())`"
#' output:
#' html_document:
#' theme: readable
#' toc: true
#' toc_depth: 2
#' toc_float: true
#' ---

#' Age-Period-Cohort - Demonstration of age adjustment to estimate
Expand All @@ -16,11 +22,11 @@ knitr::opts_chunk$set(message=FALSE, error=FALSE, warning=FALSE, comment=NA)
# switch this to TRUE to save figures in separate files
savefigs <- FALSE

#' **Load packages**
#' #### Load packages
library("rprojroot")
root<-has_dirname("ROS-Examples")$make_fix_file()

#' **Load data**
#' #### Load data
births <- read.table(root("AgePeriodCohort/data","births.txt"), header=TRUE)
mean_age_45_54 <- function(yr){
ages <- 45:54
Expand All @@ -29,15 +35,15 @@ mean_age_45_54 <- function(yr){
}
for (yr in 1989:2015) print(mean_age_45_54(yr), digits=3)

#' **Calculation**
#' #### Calculation
print((.5/10)* (.006423 - .003064)/.003064, digits=3)

#' **From life table**
#' #### From life table
deathpr_by_age <- c(.003064, .003322, .003589, .003863, .004148, .004458, .004800, .005165, .005554, .005971)
deathpr_male <- c(.003244, .003571, .003926, .004309, .004719, .005156, .005622, .006121, .006656, .007222)
deathpr_female <- c(.002069, .002270, .002486, .002716, .002960, .003226, .003505, .003779, .004040, .004301)

#' **Sum it up**
#' #### Sum it up
pop <- read.csv(root("AgePeriodCohort/data","US-EST00INT-ALLDATA.csv"))
years <- 1989:2013
deathpr_1 <- rep(NA, length(years))
Expand Down
16 changes: 11 additions & 5 deletions AgePeriodCohort/births.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
title: "Regression and Other Stories: AgePeriodCohort"
author: "Andrew Gelman, Jennifer Hill, Aki Vehtari"
date: "`r format(Sys.Date())`"
output:
html_document:
theme: readable
toc: true
toc_depth: 2
toc_float: true
---
Age-Period-Cohort - Demonstration of age adjustment to estimate
trends in mortality rates. See Chapter 3 in Regression and Other
Expand All @@ -16,14 +22,14 @@ knitr::opts_chunk$set(message=FALSE, error=FALSE, warning=FALSE, comment=NA)
savefigs <- FALSE
```

**Load packages**
#### Load packages

```{r }
library("rprojroot")
root<-has_dirname("ROS-Examples")$make_fix_file()
```

**Load data**
#### Load data

```{r }
births <- read.table(root("AgePeriodCohort/data","births.txt"), header=TRUE)
Expand All @@ -35,21 +41,21 @@ mean_age_45_54 <- function(yr){
for (yr in 1989:2015) print(mean_age_45_54(yr), digits=3)
```

**Calculation**
#### Calculation

```{r }
print((.5/10)* (.006423 - .003064)/.003064, digits=3)
```

**From life table**
#### From life table

```{r }
deathpr_by_age <- c(.003064, .003322, .003589, .003863, .004148, .004458, .004800, .005165, .005554, .005971)
deathpr_male <- c(.003244, .003571, .003926, .004309, .004719, .005156, .005622, .006121, .006656, .007222)
deathpr_female <- c(.002069, .002270, .002486, .002716, .002960, .003226, .003505, .003779, .004040, .004301)
```

**Sum it up**
#### Sum it up

```{r }
pop <- read.csv(root("AgePeriodCohort/data","US-EST00INT-ALLDATA.csv"))
Expand Down
7 changes: 7 additions & 0 deletions AgePeriodCohort/births_letter.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
#' title: "Regression and Other Stories: AgePeriodCohort"
#' author: "Andrew Gelman, Jennifer Hill, Aki Vehtari"
#' date: "`r format(Sys.Date())`"
#' output:
#' html_document:
#' theme: readable
#' toc: true
#' toc_depth: 2
#' toc_float: true
#' code_download: true
#' ---

#' Age-Period-Cohort - Age adjustment: additional plots. See Chapter
Expand Down
7 changes: 7 additions & 0 deletions AgePeriodCohort/births_letter.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
title: "Regression and Other Stories: AgePeriodCohort"
author: "Andrew Gelman, Jennifer Hill, Aki Vehtari"
date: "`r format(Sys.Date())`"
output:
html_document:
theme: readable
toc: true
toc_depth: 2
toc_float: true
code_download: true
---
Age-Period-Cohort - Age adjustment: additional plots. See Chapter
3 in Regression and Other Stories.
Expand Down
31 changes: 19 additions & 12 deletions Arsenic/arsenic_logistic_apc.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
#' title: "Regression and Other Stories: Arsenic"
#' author: "Andrew Gelman, Jennifer Hill, Aki Vehtari"
#' date: "`r format(Sys.Date())`"
#' output:
#' html_document:
#' theme: readable
#' toc: true
#' toc_depth: 2
#' toc_float: true
#' code_download: true
#' ---

#' Average predictive comparisons for a logistic regression model:
Expand All @@ -13,53 +20,53 @@
#+ setup, include=FALSE
knitr::opts_chunk$set(message=FALSE, error=FALSE, warning=FALSE, comment=NA)

#' **Load packages**
#' #### Load packages
library("rprojroot")
root<-has_dirname("ROS-Examples")$make_fix_file()
library("rstanarm")
library("loo")
invlogit <- plogis

#' **Load data**
#' #### Load data
wells <- read.csv(root("Arsenic/data","wells.csv"))
head(wells)
n <- nrow(wells)

#' **Predict switching with distance, arsenic, and education**
#' #### Predict switching with distance, arsenic, and education
#+ results='hide'
fit_7 <- stan_glm(switch ~ dist100 + arsenic + educ4,
family = binomial(link="logit"), data = wells)
#'
print(fit_7, digits=2)

#' **Average predictive difference in probability of switching,
#' comparing households that are next to, or 100 meters from, the nearest safe well**
#' #### Average predictive difference in probability of switching 1)
#' comparing households that are next to, or 100 meters from, the nearest safe well
b <- coef(fit_7)
hi <- 1
lo <- 0
delta <- invlogit (b[1] + b[2]*hi + b[3]*wells$arsenic + b[4]*wells$educ4) -
invlogit (b[1] + b[2]*lo + b[3]*wells$arsenic + b[4]*wells$educ4)
round(mean(delta), 2)

#' **Average predictive difference in probability of switching,
#' comparing households with existing arsenic levels of 0.5 and 1.0**
#' #### Average predictive difference in probability of switching 2)
#' comparing households with existing arsenic levels of 0.5 and 1.0
b <- coef(fit_7)
hi <- 1.0
lo <- 0.5
delta <- invlogit (b[1] + b[2]*wells$dist100 + b[3]*hi + b[4]*wells$educ4) -
invlogit (b[1] + b[2]*wells$dist100 + b[3]*lo + b[4]*wells$educ4)
round(mean(delta), 2)

#' **Average predictive difference in probability of switching,
#' comparing householders with 0 and 12 years of education**
#' #### Average predictive difference in probability of switching 3)
#' comparing householders with 0 and 12 years of education
b <- coef(fit_7)
hi <- 3
lo <- 0
delta <- invlogit (b[1]+b[2]*wells$dist100+b[3]*wells$arsenic+b[4]*hi) -
invlogit (b[1]+b[2]*wells$dist100+b[3]*wells$arsenic+b[4]*lo)
round(mean(delta), 2)

#' **Predict switching with distance, arsenic, education and interactions**
#' #### Predict switching with distance, arsenic, education and interactions
#+ results='hide'
wells$c_dist100 <- wells$dist100 - mean(wells$dist100)
wells$c_arsenic <- wells$arsenic - mean(wells$arsenic)
Expand All @@ -70,8 +77,8 @@ fit_8 <- stan_glm(switch ~ c_dist100 + c_arsenic + c_educ4 +
#'
print(fit_8, digits=2)

#' **Average predictive difference in probability of switching,
#' comparing households that are next to, or 100 meters from, the nearest safe well**
#' ### Average predictive difference in probability of switching 4)
#' comparing households that are next to, or 100 meters from, the nearest safe well
b <- coef(fit_8)
hi <- 1
lo <- 0
Expand Down
31 changes: 19 additions & 12 deletions Arsenic/arsenic_logistic_apc.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
title: "Regression and Other Stories: Arsenic"
author: "Andrew Gelman, Jennifer Hill, Aki Vehtari"
date: "`r format(Sys.Date())`"
output:
html_document:
theme: readable
toc: true
toc_depth: 2
toc_float: true
code_download: true
---
Average predictive comparisons for a logistic regression model:
wells in Bangladesh. See Chapter 14 in Regression and Other Stories.
Expand All @@ -13,7 +20,7 @@ wells in Bangladesh. See Chapter 14 in Regression and Other Stories.
knitr::opts_chunk$set(message=FALSE, error=FALSE, warning=FALSE, comment=NA)
```

**Load packages**
#### Load packages

```{r }
library("rprojroot")
Expand All @@ -23,15 +30,15 @@ library("loo")
invlogit <- plogis
```

**Load data**
#### Load data

```{r }
wells <- read.csv(root("Arsenic/data","wells.csv"))
head(wells)
n <- nrow(wells)
```

**Predict switching with distance, arsenic, and education**
#### Predict switching with distance, arsenic, and education

```{r results='hide'}
fit_7 <- stan_glm(switch ~ dist100 + arsenic + educ4,
Expand All @@ -44,8 +51,8 @@ fit_7 <- stan_glm(switch ~ dist100 + arsenic + educ4,
print(fit_7, digits=2)
```

**Average predictive difference in probability of switching,
comparing households that are next to, or 100 meters from, the nearest safe well**
#### Average predictive difference in probability of switching 1)
comparing households that are next to, or 100 meters from, the nearest safe well

```{r }
b <- coef(fit_7)
Expand All @@ -56,8 +63,8 @@ delta <- invlogit (b[1] + b[2]*hi + b[3]*wells$arsenic + b[4]*wells$educ4) -
round(mean(delta), 2)
```

**Average predictive difference in probability of switching,
comparing households with existing arsenic levels of 0.5 and 1.0**
#### Average predictive difference in probability of switching 2)
comparing households with existing arsenic levels of 0.5 and 1.0

```{r }
b <- coef(fit_7)
Expand All @@ -68,8 +75,8 @@ delta <- invlogit (b[1] + b[2]*wells$dist100 + b[3]*hi + b[4]*wells$educ4) -
round(mean(delta), 2)
```

**Average predictive difference in probability of switching,
comparing householders with 0 and 12 years of education**
#### Average predictive difference in probability of switching 3)
comparing householders with 0 and 12 years of education

```{r }
b <- coef(fit_7)
Expand All @@ -80,7 +87,7 @@ delta <- invlogit (b[1]+b[2]*wells$dist100+b[3]*wells$arsenic+b[4]*hi) -
round(mean(delta), 2)
```

**Predict switching with distance, arsenic, education and interactions**
#### Predict switching with distance, arsenic, education and interactions

```{r results='hide'}
wells$c_dist100 <- wells$dist100 - mean(wells$dist100)
Expand All @@ -97,8 +104,8 @@ fit_8 <- stan_glm(switch ~ c_dist100 + c_arsenic + c_educ4 +
print(fit_8, digits=2)
```

**Average predictive difference in probability of switching,
comparing households that are next to, or 100 meters from, the nearest safe well**
### Average predictive difference in probability of switching 4)
comparing households that are next to, or 100 meters from, the nearest safe well

```{r }
b <- coef(fit_8)
Expand Down
Loading

0 comments on commit 60de48a

Please sign in to comment.