-
Notifications
You must be signed in to change notification settings - Fork 11
/
likelihood_how-to.Rmd
147 lines (107 loc) · 3.78 KB
/
likelihood_how-to.Rmd
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
145
146
147
---
title: "Notes on likelihood"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
par(bg = 'black', fg = 'white', col.axis = 'white', col.lab = 'white',
col.main = 'white')
```
Here we document how to work with likelihoods.
First let's read in the data we generated from the multi-species competitive model.
```{r sad-data}
x <- readRDS('lvMultiSim.Rds')
plot(sort(x, decreasing = TRUE), log = 'y',
xlab = 'Species rank', ylab = 'Abundance')
```
We're going to fit the log-series distribution to the data. Here we make a *function* to compute the log-series.
```{r logseries-eq}
lseries <- function(b, n) {
1 / log(1 / (1 - exp(-b))) * exp(-b * n) / n
}
hist(x, probability = TRUE)
points(1:50, lseries(0.1, 1:50), col = 'red', type = 'b')
points(1:50, lseries(0.5, 1:50), col = 'orange', type = 'b')
points(1:50, lseries(0.01, 1:50), col = 'blue', type = 'b')
```
A likelihood is simply the probability of observing the data. Because these probabilities are typically very small, we most often work in log transformed probability, or *log likelihood*.
```{r likelihood-intro}
sum(log(lseries(0.001, x)))
sum(log(lseries(0.01, x)))
sum(log(lseries(0.5, x)))
```
```{r ll-curve, echo = FALSE}
bb <- seq(0.001, 0.1, length.out = 50)
ll <- sapply(bb, function(b) sum(log(lseries(b, x))))
pdf('fig_ll.pdf', width = 4, height = 4)
par(bg = 'black', fg = 'white', col.axis = 'white', col.lab = 'white')
plot(bb, ll, xlab = 'b parameter', ylab = 'log likelihood')
dev.off()
```
Fitting the model to the data precisely means finding the parameter value that maximizes the likelihood function.
```{r maxloglik}
llLSeries <- function(b, n) {
sum(log(lseries(b, n)))
}
optimize(llLSeries, interval = c(0, 10), n = x, maximum = TRUE)
```
We can did all that likelihood by hand, but we can also use the pika package
```{r pika}
# devtools::install_github('ajrominger/pika')
library(pika)
s <- sad(x, 'lseries')
s
logLik(s)
```
The pika package let's us do nicer plots, and calculate a z-statistic, which is a goodness of fit measure.
```{r pika-fun}
plot(s, ptype = 'rad')
logLikZ(s)
pchisq(3.33, df = 1, lower.tail = FALSE)
```
```{r llz, eval = FALSE, echo = FALSE}
pdf('fig_lseriesSAD.pdf', width = 4, height = 4)
par(bg = 'black', fg = 'white', col.axis = 'white', col.lab = 'white')
plot(s, ptype = 'rad', thr.col = 'magenta')
dev.off()
pdf('fig_lseriesPerf.pdf', width = 4, height = 4)
par(bg = 'black', fg = 'white', col.axis = 'white', col.lab = 'white')
plot(sad(rlseries(s$nobs, s$MLE), 'lseries'), ptype = 'rad', thr.col = 'white')
dev.off()
llDist <- replicate(1000, sad(rlseries(s$nobs, s$MLE), 'lseries')$ll)
pdf('fig_llDist.pdf', width = 6, height = 4)
par(bg = 'black', fg = 'white', col.axis = 'white', col.lab = 'white')
curve(dnorm(x, mean = mean(llDist), sd = sd(llDist)),
from = -230, to = -130, yaxt = 'n',
xlab = 'log likelihood', ylab = '', lwd = 3)
dev.off()
```
Model comparison necessitates that we penalize models for their complexity (i.e. number of parameters). We do this with AIC
```{r model-comp}
ls <- sad(x, 'lseries')
ln <- sad(x, 'plnorm')
nb <- sad(x, 'tnegb')
AIC(ls)
AIC(ln)
AIC(nb)
plot(ls, ptype = 'rad', log = 'y', main = 'logseries')
plot(ln, ptype = 'rad', log = 'y', main = 'lognormal')
plot(nb, ptype = 'rad', log = 'y', main = 'negbinom')
logLikZ(ls)
logLikZ(nb)
```
Let's see if output from a neutral model looks any different
```{r neutral}
library(roleR)
p <- untbParams(1000, 100000, 200, 0.01, 0.1, 'oceanic_island',
50000, 50000)
neutMod <- roleModel(p)
neutMod <- iterModel(neutMod)
neutModFin <- getFinalState(neutMod)
y <- getSumStats(neutModFin, list(abund = rawAbundance))
y <- y$abund$abund
y <- y[y > 0]
untbNB <- sad(y, 'tnegb')
plot(untbNB, ptype = 'rad', log = 'y')
logLikZ(untbNB)
```