forked from avehtari/ROS-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscalability.R
144 lines (130 loc) · 3.41 KB
/
scalability.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#' ---
#' title: "Regression and Other Stories: Scalability"
#' 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
#' ---
#' Demonstrate how the computation time scales with bigger data. See
#' Chapter 22 in Regression and Other Stories.
#'
#' -------------
#'
#+ setup, include=FALSE
knitr::opts_chunk$set(message=FALSE, error=FALSE, warning=FALSE, comment=NA)
#' #### Load packages
library(arm)
library(rstanarm)
options(mc.cores = parallel::detectCores())
library(tictoc)
#' ## Linear regression n=100K, p=100
#'
#' #### Create fake data with n=100\,000 and p=100
SEED <- 1656
set.seed(SEED)
n <- 1e5
k <- 100
x <- rnorm(n)
xn <- matrix(rnorm(n*(k-1)),nrow=n)
a <- 2
b <- 3
sigma <- 10
y <- a + b*x + sigma*rnorm(n)
fake <- data.frame(x, xn, y)
#' #### Fit using lm
tic()
fit1 <- lm(y ~ ., data=fake)
toc()
#display(fit1)
#' #### Fit using stan_glm and MCMC
#'
#' stan_glm is fast for linear regression with n>k and small or
#' moderate k (using OLS trick)
tic()
#+ results='hide'
fit2 <- stan_glm(y ~ ., data=fake, mean_PPD=FALSE,
refresh=500, seed=SEED, cores=1)
#+
toc()
#print(fit2, digits=2)
#' #### Fit using stan_glm and optimization
#'
#' Using optimization with normal approximation and Pareto smoothed
#' importance resampling provides coefficient standard errors, but
#' also diagnostic whether normal approximation at the mode is
#' appropriate.
tic()
fit3 <- stan_glm(y ~ ., data=fake, algorithm='optimizing', init=0)
toc()
#print(fit3, digits=2)
#' ## Logistic regression n=10K, p=100
#'
#' #### Create fake data with 10\,000 observations and p=100
SEED <- 1655
set.seed(SEED)
n <- 1e4
k <- 100
x <- rnorm(n)
xn <- matrix(rnorm(n*(k-1)),nrow=n)
a <- 2
b <- 3
sigma <- 1
y <- as.numeric(a + b*x + sigma*rnorm(n) > 0)
fake <- data.frame(x, xn, y)
#' #### Fit using glm
tic()
fit1 <- glm(y ~ ., data=fake, family=binomial())
toc()
#display(fit1)
#' #### Fit using stan_glm and MCMC
tic()
fit2 <- stan_glm(y ~ ., data=fake, family=binomial(), mean_PPD=FALSE,
init_r=0.1, seed=SEED)
toc()
#print(fit2, digits=2)
#' #### Fit using stan_glm and optimization
#'
#' Using optimization with normal approximation and Pareto smoothed
#' importance resampling provides coefficient standard errors, but
#' also diagnostic whether normal approximation at the mode is
#' appropriate.
tic()
fit3 <- stan_glm(y ~ ., data=fake, family=binomial(),
algorithm='optimizing', init=0, draws = 16000, keep_every = 4)
toc()
#print(fit3, digits=2)
#' ## Logistic regression n=100K, p=100
#'
#' #### Create fake data with 100\,000 observations and p=100
SEED <- 1655
set.seed(SEED)
n <- 1e5
k <- 100
x <- rnorm(n)
xn <- matrix(rnorm(n*(k-1)),nrow=n)
a <- 2
b <- 3
sigma <- 1
y <- as.numeric(a + b*x + sigma*rnorm(n) > 0)
fake <- data.frame(x, xn, y)
#' #### Fit using glm
tic()
fit1 <- glm(y ~ ., data=fake, family=binomial())
toc()
#display(fit1)
#' #### Fit using stan_glm and optimization
#'
#' Using optimization with normal approximation and Pareto smoothed
#' importance resampling provides coefficient standard errors, but
#' also diagnostic whether normal approximation at the mode is
#' appropriate.
tic()
fit3 <- stan_glm(y ~ ., data=fake, family=binomial(),
algorithm='optimizing', init=0)
toc()
summary(fit3, digits=2)