Skip to content

Commit 3288f4a

Browse files
committed
Merge branch 'luca_updates'
2 parents 3b5762a + 923d537 commit 3288f4a

4 files changed

Lines changed: 190 additions & 224 deletions

File tree

vignettes/figure/hist10-1.png

82.5 KB
Loading
108 KB
Loading

vignettes/vignette_start.Rmd

Lines changed: 66 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,9 @@ which produces an object `learnt10` (just a string, in this case) which can be u
373373

374374
## Examples of analysis
375375

376-
### Estimating the relative frequencies of the three species
376+
### Estimating the relative frequencies of the three penguin species
377377

378-
Consider our first research question **Q1**: what's the overall statistical occurrence of the three species in the whole population? The relative frequencies of the three species in the whole population are unknown to us, so we cannot give a simple answer such as "Adélie: 0.10, Chinstrap: 0.34, Gentoo: 0.56".
378+
Consider our first research question **Q1**: "what's the overall statistical occurrence of the three species in the whole population?" The relative frequencies of the three species in the whole population are unknown to us, so we cannot give a simple answer such as "Adélie: 0.10, Chinstrap: 0.34, Gentoo: 0.56".
379379

380380
Bayesian nonparametrics and ***inferno*** give a first answer to this question in the form of:
381381

@@ -413,7 +413,10 @@ The answer to our question is now contained in the `Fspecies10` object (we chose
413413
plot(Fspecies10)
414414
```
415415

416-
<img src="figure/vis10-1.png" width="100%" />
416+
<div class="figure">
417+
<img src="figure/vis10-1.png" alt="**Estimates and uncertainty of relative frequencies of the three penguin species**" width="100%" />
418+
<p class="caption">**Estimates and uncertainty of relative frequencies of the three penguin species**</p>
419+
</div>
417420

418421
The x-axis of this plot shows the three possible values of the `species` variate. The y-axis reports fractions which may be read as *frequencies* or *probabilities*.
419422

@@ -424,135 +427,95 @@ The three estimated frequencies are Adélie: 0.32, Chinstrap: 0.31, Gentoo: 0
424427

425428
``` r
426429
Fspecies10$values
427-
## X
428-
## Y [,1]
429-
## [1,] 0.315265
430-
## [2,] 0.310161
431-
## [3,] 0.374574
430+
## X
431+
## Y [,1]
432+
## Adelie 0.315265
433+
## Chinstrap 0.310161
434+
## Gentoo 0.374574
432435
```
433436

434-
What about the uncertainty of these estimates? In Bayesian theory, a compact way of expressing the uncertainty in a quantity is by means of a **credibility interval** with a given probability. For instance, if we say that a particular quantity has a 80%-credibility interval equal to $(0.41, 0.69)$, what we mean is that there's an 80% probability that the true value of that quantity is between $0.41$ and $0.69$ -- pretty straightforward! (*Please be careful not to confuse the Bayesian credibility interval with a "confidence interval"*: the latter has a much more involved and less straightforward meaning.)
437+
What about the uncertainty of these estimates?
435438

436-
The `Pr()` function by default calculates two credibility intervals for each frequency estimate: a 50% one, and an 89% one. In our present inference, the 50%-credibility intervals are shown in the plot above as the darker grey band, and the 89%-credibility interval as the lighter grey band. Note that this interval contains the 50% one.
439+
### Uncertainty of the estimates: credibility intervals and probabilities
437440

438-
For instance, the plot indicates that there's a 50% probability that the relative frequency of all Adélie penguins is roughly between 0.25 and 0.4; and an 89% probability that their relative frequency is roughly between 0.3 and 0.45.
441+
In Bayesian theory, a compact way of expressing the uncertainty in a quantity is by means of a **credibility interval** with a given probability. For instance, if we say that a particular quantity has a 80%-credibility interval equal to $(0.41, 0.69)$, what we mean is that there's an 80% probability that the true value of that quantity is between $0.41$ and $0.69$ -- pretty straightforward! (*Please be careful not to confuse the Bayesian credibility interval with a "confidence interval"*: the latter has a much more involved and less straightforward meaning.)
439442

440-
For the Gentoo species, there's a 50% probability that its relative frequency is roughly between 0.3 and 0.45; and an 89% probability that their relative frequency is roughly between 0.2 and 0.6.
443+
The `Pr()` function by default calculates two credibility intervals for each frequency estimate: a 50% one, and an 89% one. In our present inference, the 50%-credibility intervals are shown in the plot above as the darker grey band, and the 89%-credibility intervals as the lighter grey band. Note that these intervals contain the 50% ones.
441444

442-
The credibility intervals can be read from the `quantiles` element of the `Fspecies10` object:
445+
For instance, the plot indicates that there's a 50% probability that the relative frequency of all `Adelie` penguins is roughly between 0.25 and 0.4; and an 89% probability that their relative frequency is roughly between 0.3 and 0.45.
443446

447+
For the `Gentoo` species, there's a 50% probability that its relative frequency is roughly between 0.3 and 0.45; and an 89% probability that their relative frequency is roughly between 0.2 and 0.6.
444448

445-
``` r
446-
Fspecies10$quantiles
447-
## , , = 5.5%
448-
##
449-
## X
450-
## Y [,1]
451-
## [1,] 0.134878
452-
## [2,] 0.132087
453-
## [3,] 0.180867
454-
##
455-
## , , = 25%
456-
##
457-
## X
458-
## Y [,1]
459-
## [1,] 0.227634
460-
## [2,] 0.219339
461-
## [3,] 0.284369
462-
##
463-
## , , = 75%
464-
##
465-
## X
466-
## Y [,1]
467-
## [1,] 0.395804
468-
## [2,] 0.391340
469-
## [3,] 0.457973
470-
##
471-
## , , = 94.5%
472-
##
473-
## X
474-
## Y [,1]
475-
## [1,] 0.525858
476-
## [2,] 0.521080
477-
## [3,] 0.587441
478-
```
449+
We can Actually do more: we can plot the probabilities of all possible frequencies. To understand this idea, let's ask: what is the relative frequency of Adélie penguins in the whole population? Possible values could be anything between 0 and 1. But some of these values may be more probable than others. If we look at our 10 samples, we see that 3 out of 10 are `Adelie`. So a relative frequency around 0.3 is a little more probable, although there's still a lot of uncertainty because this is just a small sample.
479450

480-
As the name tells, this element actually report the *quantiles* of the probability. For example, look at the `= 25%` output: among other things it says `Adelie 0.227634`. This means that there's a 25% probability that the frequency of Adélie penguins is below $0.227634$, and thus a 75% probability that it's above $0.227634$.
451+
The `Pr()` function has actually calculated, in an approximate way, the probabilities for all possible frequencies of each species. We can plot the probabilities of the frequencies of the `Adelie` species as follows:
481452

482453

483454
``` r
455+
# # select only Adelie
484456
onlyAdelie <- subset(Fspecies10, list(species = 'Adelie'))
485-
hist(onlyAdelie, legend = 'topright')
486-
```
487457

488-
<img src="figure/hist10-1.png" width="100%" />
458+
hist(onlyAdelie, xlim = c(0, 1), legend = 'topright', col = 2)
459+
```
489460

461+
<div class="figure">
462+
<img src="figure/hist10adelie-1.png" alt="**Probability distribution for the frequency of Adelie penguins**" width="100%" />
463+
<p class="caption">**Probability distribution for the frequency of Adelie penguins**</p>
464+
</div>
490465

491-
by giving the probabilities of all possible relative-frequency distributions.
466+
With the `subset()` function we first extract from the `Fspecies10` object only the information pertaining to the `Adelie` species, saving it into the `onlyAdelie` object. Then the `hist()` function plots the probability distribution for all the frequency values. We see that frequencies between 0.2 and 0.4 have highest probability. But the probability distribution is quite wide; even a frequency of 0.6 has some probability. The vertical line indicates the mean of this probability distribution, which we take as our frequency estimate.
492467

468+
The width of this probability distribution reflects our large uncertainty, which in turn comes from the fact that we have only seen a small sample of penguins. We shall see that this uncertainty decreases and we gather more samples.
493469

470+
\
471+
The credibility intervals are calculated from the probability distribution above. They can be quickly obtained from the `quantiles` element of the `Fspecies10` object. For instance, we can read the 89%-credibility intervals as follows:
494472

495473

496474
``` r
497-
prQ1 <- Pr(
498-
Y = data.frame(species = c('Adelie', 'Chinstrap', 'Gentoo')),
499-
X = NULL,
500-
learnt = learnt10,
501-
parallel = parallel
502-
)
503-
##
504-
## Registered doParallelSNOW with 8 workers
505-
##
506-
## Closing connections to cores.
507-
508-
plot(prQ1, ylim = c(0, 1))
475+
Fspecies10$quantiles[, , c('5.5%', '94.5%')]
476+
##
477+
## Y 5.5% 94.5%
478+
## Adelie 0.134878 0.525858
479+
## Chinstrap 0.132087 0.521080
480+
## Gentoo 0.180867 0.587441
509481
```
510482

511-
<div class="figure">
512-
<img src="figure/freqs10-1.png" alt="plot of chunk freqs10" width="100%" />
513-
<p class="caption">plot of chunk freqs10</p>
514-
</div>
483+
<!-- As the name tells, this element actually report the *quantiles* of the probability. For example, look at the `= 25%` output: among other things it says `Adelie 0.227634`. This means that there's a 25% probability that the frequency of Adélie penguins is below $0.227634$, and thus a 75% probability that it's above $0.227634$.
484+
-->
515485

516-
``` r
486+
### A preliminary report
517487

518-
hist(prQ1, xlim = c(0, 1))
519-
```
488+
We can now give a preliminary answer to our question **Q1**, "what's the overall statistical occurrence of the three species in the whole population?".
520489

521-
<div class="figure">
522-
<img src="figure/freqs10-2.png" alt="plot of chunk freqs10" width="100%" />
523-
<p class="caption">plot of chunk freqs10</p>
524-
</div>
490+
A summary answer could be as follows:
525491

492+
> From a sample of 10 penguins, the inference about the relative frequencies of the three species in the full population is as follows:
493+
>
494+
> Adélie
495+
> : rel. frequency between 0.143 and 0.53, with 89% probability
496+
>
497+
> Chinstrap
498+
> : rel. frequency between 0.13 and 0.52, with 89% probability
499+
>
500+
> Gentoo
501+
> : rel. frequency between 0.18 and 0.59, with 89% probability
502+
503+
But we can also give a fuller answer by displaying the probability distributions for all three frequencies:
526504

527-
``` r
528-
prQ1 <- Pr(
529-
Y = data.frame(species = c('Adelie', 'Chinstrap', 'Gentoo')),
530-
X = NULL,
531-
learnt = 'learnt_all.rds',
532-
parallel = parallel
533-
)
534-
##
535-
## Registered doParallelSNOW with 8 workers
536-
##
537-
## Closing connections to cores.
538505

539-
plot(prQ1, ylim = c(0, 1))
506+
``` r
507+
# # select only Adelie
508+
hist(Fspecies10, legend = 'topright', col = 2:4)
540509
```
541510

542511
<div class="figure">
543-
<img src="figure/freqsall-1.png" alt="plot of chunk freqsall" width="100%" />
544-
<p class="caption">plot of chunk freqsall</p>
512+
<img src="figure/hist10-1.png" alt="**Probability distribution for the rel. requencies of the three species**" width="100%" />
513+
<p class="caption">**Probability distribution for the rel. requencies of the three species**</p>
545514
</div>
546515

547-
``` r
516+
It's important to keep in mind that the three frequencies must add up to 1, so if our future estimates of some of them increase, then the estimate of some others must decrease.
548517

549-
hist(prQ1, xlim = c(0, 1))
550-
```
551518

552-
<div class="figure">
553-
<img src="figure/freqsall-2.png" alt="plot of chunk freqsall" width="100%" />
554-
<p class="caption">plot of chunk freqsall</p>
555-
</div>
556519

557520

558521

@@ -845,13 +808,13 @@ dataset <- penguins
845808
Here are the values for two subjects:
846809
847810
848-
| |species |island | bill_len| bill_dep| flipper_len| body_mass|sex | year|
849-
|:---|:-------|:------|--------:|--------:|-----------:|---------:|:------|----:|
850-
|173 |Gentoo |Biscoe | 50.2| 14.3| 218| 5700|male | 2007|
851-
|210 |Gentoo |Biscoe | 45.5| 15.0| 220| 5000|male | 2008|
852-
|45 |Adelie |Dream | 37.0| 16.9| 185| 3000|female | 2007|
853-
|52 |Adelie |Biscoe | 40.1| 18.9| 188| 4300|male | 2008|
854-
|247 |Gentoo |Biscoe | 44.5| 14.7| 214| 4850|female | 2009|
811+
| |species |island | bill_len| bill_dep| flipper_len| body_mass|sex | year|
812+
|:---|:-------|:---------|--------:|--------:|-----------:|---------:|:------|----:|
813+
|30 |Adelie |Biscoe | 40.5| 18.9| 180| 3950|male | 2007|
814+
|81 |Adelie |Torgersen | 34.6| 17.2| 189| 3200|female | 2008|
815+
|108 |Adelie |Biscoe | 38.2| 20.0| 190| 3900|male | 2009|
816+
|28 |Adelie |Biscoe | 40.5| 17.9| 187| 3200|female | 2007|
817+
|107 |Adelie |Biscoe | 38.6| 17.2| 199| 3750|female | 2009|
855818
856819
857820

vignettes/vignette_start.htm

Lines changed: 124 additions & 121 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)