Professional Documents
Culture Documents
Statisticalrethinking 2
Statisticalrethinking 2
ulam
map
quap
map2stan ulam
extract.prior
brms rstanarm
y ~ (1|x) + z
y<-7
y
200
= ( . )
= × ( . )
( log( 0.01^200 ) )
( 200 * log(0.01) )
[1] -Inf
[1] -921.034
#
PROC GLM
rethinking
rethinking
rethinking rstan
rstan mc-stan.org
rstan
mc-stan.org rethinking
install.packages(c("coda","mvtnorm","devtools","dagitty"))
library(devtools)
devtools::install_github("rmcelreath/rethinking")
rethinking
rethinking
github.com/rmcelreath/rethinking
Hypotheses Process models Statistical models
P0A
Neutral,
H0 equilibrium
MI
“Evolution
is neutral” P0B
Neutral,
non-equilibrium
MII
P1A
H1 Constant
selection
“Selection
matters” MIII
P1B
Fluctuating
selection
=
× × =
× × =
× × =
× × =
× × =
× =
× =
× =
× =
× =
×
= = =
= × =
× =
× =
× =
× =
× =
∝
= / = . =
∝ ×
×
=
.
.
.
ways <- c( 0 , 3 , 8 , 9 , 0 )
ways/sum(ways)
•
•
−
> .
W L W W W L W L W
W Ln W
= 0W W L W L W W L WWW L W L W W L WWW L W L W
n=1 n=2 n=3
plausibility
confidence
W L W W W L W L W
0 0.5 1 0 0.5 1 0 0.5 1
0 0.5 1
nW= 0W W L W L W W L W W W L W L W
W Lprobability W Lprobability
W W W ofL water
W L W
of waterwater
proportion probability of water
n=4 n=5 n=6
plausibility
confidence
W L W W W L W WL LW W W W L W WL LW W W W L W L W
0W L W0.5W W L 1 W 0 L W 0.5 1 0 0.5 1
n =00 0.5 n=0 1 n=0
nW
= 0W W ofL water
W Lprobability W L W W Lprobability
W W W ofL water
W L W W Lprobability
W W W ofL water
W L W
proportion water
n=7 n=8 n=9
plausibility
plausibility
plausibility
plausibility
confidence
( + )!
( , | )= ( − )
! !
[1] 0.1640625
= +
0.5
d dbinom
r
p ?dbinom
•
•
•
•
=
( )= = .
−
/( − )
/( − )
∼ ( , )
= +
∼ ( , )
( | , )
( , , )= ( , | ) ( )
, ( , | )
( )
( , , )= ( | , ) ( , )
( , , )
( , | ) ( )= ( | , ) ( , )
( | , )
( , | ) ( )
( | , )=
( , )
( , )
×
=
( , )
( , )
#
! "
( , )= ( , | ) = ( , | ) ( )
prior likelihood posterior
⇥ /
prior likelihood posterior
0 0.5 1 0 0.5 1 0 0.5 1
⇥ /
prior likelihood posterior
0 0.5 1 0 0.5 1 0 0.5 1
⇥ /
# define grid
p_grid <- seq( from=0 , to=1 , length.out=20 )
# define prior
prior <- rep( 1 , 20 )
0.12
posterior probability
posterior probability
0.04 0.08
0.00
0.0
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
probability of water probability of water
p_grid
prior
p_grid
=
rethinking quap quap
library(rethinking)
globe.qa <- quap(
alist(
W ~ dbinom( W+L ,p) , # binomial likelihood
p ~ dunif(0,1) # uniform prior
) ,
data=list(W=6,L=3) )
quap
5
2.5
4
1.0 1.5 2.0
Density
Density
Density
3
2
2
1
0.5
1
0.0
0
0.0 0.5 1.0 0.0 0.5 1.0 0.0 0.5 1.0
proportion water proportion water proportion water
=
=
= =
= =
precis
dbeta
# analytical calculation
W <- 6
L <- 3
curve( dbeta( x , W+1 , L+1 ) , from=0 , to=1 )
# quadratic approximation
curve( dnorm( x , 0.67 , 0.16 ) , lty=2 , add=TRUE )
=
n_samples <- 1000
p <- rep( NA , n_samples )
p[1] <- 0.5
W <- 6
L <- 3
for ( i in 2:n_samples ) {
p_new <- rnorm( 1 , p[i-1] , 0.1 )
if ( p_new < 0 ) p_new <- abs( p_new )
if ( p_new > 1 ) p_new <- 2 - p_new
q0 <- dbinom( W , W+L , p[i-1] )
q1 <- dbinom( W , W+L , p_new )
p[i] <- ifelse( runif(1) < q1/q0 , p_new , p[i-1] )
}
dens( p , xlim=c(0,1) )
curve( dbeta( x , W+1 , L+1 ) , lty=2 , add=TRUE )
( )
( | )
( | )
( , )/ ( )
( | )
( | )
( | )
( | ) ( )
( | ) ( )/ ( )
( | ) ( )/ ( )
< .
≥ .
( | )
•
•
L(θ| )
( |θ)
dbeta(p,w+1,n-w+1)
p w n
‒
‒
‒
‒
$
‒
‒
axis
complete.cases
data(Howell1)
data(milk)
GPL2
data(WaffleDivorce)
dbetabinom
extract.samples
Kline2
link
log_sum_exp
n_eff
mcreplicate
‒
pairs rlkjcorr
rugged
sim
sim.train.test
sim_train_test
quap
ulam