# post_prob_states_matrix: Calculate posterior probability of each states/geographic... In BioGeoBEARS: BioGeography with Bayesian (and Likelihood) Evolutionary Analysis in R Scripts

## Description

This function calculates P(range|data,detection model), i.e. the probability of each possible range, given a prior probability of each range, and the likelihood of each range.

## Usage

 ```1 2``` ``` post_prob_states_matrix(prob_of_each_range, tip_condlikes_of_data_on_each_state) ```

## Arguments

 `prob_of_each_range` The probability of each range, given the prior probability of presence in each area. `tip_condlikes_of_data_on_each_state` The probability of the data, conditional on each range (i.e., the likelihood), as found in e.g. a row of the output from `tiplikes_wDetectionModel`.

## Details

The prior probability of each range should be considered by the user. Note that putting the same prior on the probability of occurrence in each individual range does NOT mean a flat prior on each state/geographic range. This fact is demonstrated in the function `prob_of_states_from_prior_prob_areas`.

## Value

`posterior_probs` The posterior probability of each range.

Go BEARS!

## Author(s)

Nicholas J. Matzke matzke@berkeley.edu

## References

Matzke_2012_IBS

Bottjer_Jablonski_1988

`prob_of_states_from_prior_prob_areas`, `tiplikes_wDetectionModel`, `rcpp_areas_list_to_states_list`, `Pdata_given_rangerow`, `calc_obs_like`, `mapply`, `read_detections`, `read_controls`

## Examples

 ``` 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``` ```testval=1 # soft-coded input files extdata_dir = np(system.file("extdata", package="BioGeoBEARS")) #extdata_dir = "/Dropbox/_njm/__packages/BioGeoBEARS_setup/inst/extdata/" detects_fn = np(paste(extdata_dir, "/Psychotria_detections_v1.txt", sep="")) controls_fn = np(paste(extdata_dir, "/Psychotria_controls_v1.txt", sep="")) detects_df = read_detections(detects_fn, OTUnames=NULL, areanames=NULL, tmpskip=0) controls_df = read_controls(controls_fn, OTUnames=NULL, areanames=NULL, tmpskip=0) # Setup prior_prob_presence = 0.01 areas = c("K", "O", "M", "H") numareas = length(areas) maxareas = length(areas) states_list_0based_index = rcpp_areas_list_to_states_list(areas=areas, maxareas=maxareas, include_null_range=TRUE) states_list_0based_index mean_frequency=0.1 dp=1 fdp=0 tip_condlikes_of_data_on_each_state = tiplikes_wDetectionModel(states_list_0based_index, numareas=numareas, detects_df, controls_df, mean_frequency=mean_frequency, dp=dp, fdp=fdp, null_range_gets_0_like=TRUE) tip_condlikes_of_data_on_each_state # To get denominator, just iterate over all the states # Prior probability prob_of_each_range = prob_of_states_from_prior_prob_areas(states_list_0based_index, numareas=numareas, prior_prob_presence=prior_prob_presence, null_range_gets_0_prob=TRUE, normalize_probs=TRUE) posterior_probs_matrix = post_prob_states_matrix(prob_of_each_range, tip_condlikes_of_data_on_each_state) posterior_probs_matrix # Should sum to 1 rowSums(posterior_probs_matrix) # How does posterior probability correlate with likelihood and prior probability? par(mfrow=c(1,2)) plot(x=jitter(log(tip_condlikes_of_data_on_each_state)), y=jitter(log(posterior_probs_matrix))) title("Correlation of data likelihoods\nand posterior probabilities") prob_of_each_range_matrix = matrix(data=prob_of_each_range, nrow=nrow(posterior_probs_matrix), ncol=length(prob_of_each_range)) plot(x=jitter(log(prob_of_each_range_matrix)), y=jitter(log(posterior_probs_matrix))) title("Correlation of prior probability\nand posterior probabilities") ```

### Example output

```Loading required package: rexpokit

Attaching package: 'SparseM'

The following object is masked from 'package:base':

backsolve

Attaching package: 'phylobase'

The following object is masked from 'package:ape':

edges

[[1]]
[1] NA

[[2]]
[1] 0

[[3]]
[1] 1

[[4]]
[1] 2

[[5]]
[1] 3

[[6]]
[1] 0 1

[[7]]
[1] 0 2

[[8]]
[1] 0 3

[[9]]
[1] 1 2

[[10]]
[1] 1 3

[[11]]
[1] 2 3

[[12]]
[1] 0 1 2

[[13]]
[1] 0 1 3

[[14]]
[1] 0 2 3

[[15]]
[1] 1 2 3

[[16]]
[1] 0 1 2 3

[,1]         [,2]         [,3]         [,4]         [,5]         [,6]
[1,]    0 3.414667e-14 8.805043e-32 3.449002e-34 8.813857e-34 3.073542e-12
[2,]    0 3.387445e-04 3.384058e-02 3.387445e-04 3.387445e-04 1.193137e-02
[3,]    0 3.387445e-10 3.387445e-10 3.377293e-04 3.387445e-10 1.194332e-10
[4,]    0 3.387445e-16 3.387445e-16 3.387445e-16 3.370542e-06 1.194332e-16
[5,]    0 3.384058e-02 3.387445e-04 3.387445e-04 3.387445e-04 1.193137e-02
[6,]    0 3.387445e-04 3.387445e-04 3.384058e-02 3.387445e-04 1.194332e-04
[7,]    0 3.387445e-04 3.387445e-04 3.384058e-02 3.387445e-04 1.194332e-04
[8,]    0 3.387445e-04 3.387445e-04 3.384058e-02 3.387445e-04 1.194332e-04
[9,]    0 3.387445e-04 3.387445e-04 3.384058e-02 3.387445e-04 1.194332e-04
[10,]    0 3.387445e-04 3.384058e-02 3.387445e-04 3.387445e-04 1.193137e-02
[11,]    0 3.387445e-04 3.384058e-02 3.387445e-04 3.387445e-04 1.193137e-02
[12,]    0 3.387445e-04 3.384058e-02 3.387445e-04 3.387445e-04 1.193137e-02
[13,]    0 3.384058e-02 3.387445e-04 3.387445e-04 3.387445e-04 1.193137e-02
[14,]    0 3.384058e-02 3.387445e-04 3.387445e-04 3.387445e-04 1.193137e-02
[15,]    0 3.384058e-02 3.387445e-04 3.387445e-04 3.387445e-04 1.193137e-02
[16,]    0 3.384058e-02 3.387445e-04 3.387445e-04 3.387445e-04 1.193137e-02
[17,]    0 3.384058e-02 3.387445e-04 3.387445e-04 3.387445e-04 1.193137e-02
[18,]    0 3.384058e-02 3.387445e-04 3.387445e-04 3.387445e-04 1.193137e-02
[19,]    0 3.387445e-04 3.384058e-02 3.387445e-04 3.387445e-04 1.193137e-02
[,7]         [,8]         [,9]        [,10]        [,11]
[1,] 1.203930e-14 3.076618e-14 3.104447e-32 7.933353e-32 3.107554e-34
[2,] 1.194332e-04 1.194332e-04 1.193137e-02 1.193137e-02 1.194332e-04
[3,] 1.190752e-04 1.194332e-10 1.190752e-04 1.194332e-10 1.190752e-04
[4,] 1.194332e-16 1.188372e-06 1.194332e-16 1.188372e-06 1.188372e-06
[5,] 1.193137e-02 1.193137e-02 1.194332e-04 1.194332e-04 1.194332e-04
[6,] 1.193137e-02 1.194332e-04 1.193137e-02 1.194332e-04 1.193137e-02
[7,] 1.193137e-02 1.194332e-04 1.193137e-02 1.194332e-04 1.193137e-02
[8,] 1.193137e-02 1.194332e-04 1.193137e-02 1.194332e-04 1.193137e-02
[9,] 1.193137e-02 1.194332e-04 1.193137e-02 1.194332e-04 1.193137e-02
[10,] 1.194332e-04 1.194332e-04 1.193137e-02 1.193137e-02 1.194332e-04
[11,] 1.194332e-04 1.194332e-04 1.193137e-02 1.193137e-02 1.194332e-04
[12,] 1.194332e-04 1.194332e-04 1.193137e-02 1.193137e-02 1.194332e-04
[13,] 1.193137e-02 1.193137e-02 1.194332e-04 1.194332e-04 1.194332e-04
[14,] 1.193137e-02 1.193137e-02 1.194332e-04 1.194332e-04 1.194332e-04
[15,] 1.193137e-02 1.193137e-02 1.194332e-04 1.194332e-04 1.194332e-04
[16,] 1.193137e-02 1.193137e-02 1.194332e-04 1.194332e-04 1.194332e-04
[17,] 1.193137e-02 1.193137e-02 1.194332e-04 1.194332e-04 1.194332e-04
[18,] 1.193137e-02 1.193137e-02 1.194332e-04 1.194332e-04 1.194332e-04
[19,] 1.194332e-04 1.194332e-04 1.193137e-02 1.193137e-02 1.194332e-04
[,12]        [,13]        [,14]        [,15]        [,16]
[1,] 1.083657e-12 2.769264e-12 1.084742e-14 2.797110e-32 9.763761e-13
[2,] 4.206716e-03 4.206716e-03 4.210927e-05 4.206716e-03 1.483187e-03
[3,] 4.198307e-05 4.210927e-11 4.198307e-05 4.198307e-05 1.480222e-05
[4,] 4.210927e-17 4.189915e-07 4.189915e-07 4.189915e-07 1.477263e-07
[5,] 4.206716e-03 4.206716e-03 4.206716e-03 4.210927e-05 1.483187e-03
[6,] 4.206716e-03 4.210927e-05 4.206716e-03 4.206716e-03 1.483187e-03
[7,] 4.206716e-03 4.210927e-05 4.206716e-03 4.206716e-03 1.483187e-03
[8,] 4.206716e-03 4.210927e-05 4.206716e-03 4.206716e-03 1.483187e-03
[9,] 4.206716e-03 4.210927e-05 4.206716e-03 4.206716e-03 1.483187e-03
[10,] 4.206716e-03 4.206716e-03 4.210927e-05 4.206716e-03 1.483187e-03
[11,] 4.206716e-03 4.206716e-03 4.210927e-05 4.206716e-03 1.483187e-03
[12,] 4.206716e-03 4.206716e-03 4.210927e-05 4.206716e-03 1.483187e-03
[13,] 4.206716e-03 4.206716e-03 4.206716e-03 4.210927e-05 1.483187e-03
[14,] 4.206716e-03 4.206716e-03 4.206716e-03 4.210927e-05 1.483187e-03
[15,] 4.206716e-03 4.206716e-03 4.206716e-03 4.210927e-05 1.483187e-03
[16,] 4.206716e-03 4.206716e-03 4.206716e-03 4.210927e-05 1.483187e-03
[17,] 4.206716e-03 4.206716e-03 4.206716e-03 4.210927e-05 1.483187e-03
[18,] 4.206716e-03 4.206716e-03 4.206716e-03 4.210927e-05 1.483187e-03
[19,] 4.206716e-03 4.206716e-03 4.210927e-05 4.206716e-03 1.483187e-03
[,1]         [,2]         [,3]         [,4]         [,5]         [,6]
[1,]    0 5.172159e-01 1.333690e-18 5.224166e-21 1.335025e-20 4.702485e-01
[2,]    0 9.617063e-03 9.607446e-01 9.617063e-03 9.617063e-03 3.421571e-03
[3,]    0 9.923627e-07 9.923627e-07 9.893886e-01 9.923627e-07 3.534174e-09
[4,]    0 9.943534e-11 9.943534e-11 9.943534e-11 9.893915e-01 3.541264e-13
[5,]    0 9.607446e-01 9.617063e-03 9.617063e-03 9.617063e-03 3.421571e-03
[6,]    0 9.617063e-03 9.617063e-03 9.607446e-01 9.617063e-03 3.424996e-05
[7,]    0 9.617063e-03 9.617063e-03 9.607446e-01 9.617063e-03 3.424996e-05
[8,]    0 9.617063e-03 9.617063e-03 9.607446e-01 9.617063e-03 3.424996e-05
[9,]    0 9.617063e-03 9.617063e-03 9.607446e-01 9.617063e-03 3.424996e-05
[10,]    0 9.617063e-03 9.607446e-01 9.617063e-03 9.617063e-03 3.421571e-03
[11,]    0 9.617063e-03 9.607446e-01 9.617063e-03 9.617063e-03 3.421571e-03
[12,]    0 9.617063e-03 9.607446e-01 9.617063e-03 9.617063e-03 3.421571e-03
[13,]    0 9.607446e-01 9.617063e-03 9.617063e-03 9.617063e-03 3.421571e-03
[14,]    0 9.607446e-01 9.617063e-03 9.617063e-03 9.617063e-03 3.421571e-03
[15,]    0 9.607446e-01 9.617063e-03 9.617063e-03 9.617063e-03 3.421571e-03
[16,]    0 9.607446e-01 9.617063e-03 9.617063e-03 9.617063e-03 3.421571e-03
[17,]    0 9.607446e-01 9.617063e-03 9.617063e-03 9.617063e-03 3.421571e-03
[18,]    0 9.607446e-01 9.617063e-03 9.617063e-03 9.617063e-03 3.421571e-03
[19,]    0 9.617063e-03 9.607446e-01 9.617063e-03 9.617063e-03 3.421571e-03
[,7]         [,8]         [,9]        [,10]        [,11]
[1,] 1.841999e-03 4.707192e-03 4.749769e-21 1.213794e-20 4.754524e-23
[2,] 3.424996e-05 3.424996e-05 3.421571e-03 3.421571e-03 3.424996e-05
[3,] 3.523582e-03 3.534174e-09 3.523582e-03 3.534174e-09 3.523582e-03
[4,] 3.541264e-13 3.523593e-03 3.541264e-13 3.523593e-03 3.523593e-03
[5,] 3.421571e-03 3.421571e-03 3.424996e-05 3.424996e-05 3.424996e-05
[6,] 3.421571e-03 3.424996e-05 3.421571e-03 3.424996e-05 3.421571e-03
[7,] 3.421571e-03 3.424996e-05 3.421571e-03 3.424996e-05 3.421571e-03
[8,] 3.421571e-03 3.424996e-05 3.421571e-03 3.424996e-05 3.421571e-03
[9,] 3.421571e-03 3.424996e-05 3.421571e-03 3.424996e-05 3.421571e-03
[10,] 3.424996e-05 3.424996e-05 3.421571e-03 3.421571e-03 3.424996e-05
[11,] 3.424996e-05 3.424996e-05 3.421571e-03 3.421571e-03 3.424996e-05
[12,] 3.424996e-05 3.424996e-05 3.421571e-03 3.421571e-03 3.424996e-05
[13,] 3.421571e-03 3.421571e-03 3.424996e-05 3.424996e-05 3.424996e-05
[14,] 3.421571e-03 3.421571e-03 3.424996e-05 3.424996e-05 3.424996e-05
[15,] 3.421571e-03 3.421571e-03 3.424996e-05 3.424996e-05 3.424996e-05
[16,] 3.421571e-03 3.421571e-03 3.424996e-05 3.424996e-05 3.424996e-05
[17,] 3.421571e-03 3.421571e-03 3.424996e-05 3.424996e-05 3.424996e-05
[18,] 3.421571e-03 3.421571e-03 3.424996e-05 3.424996e-05 3.424996e-05
[19,] 3.424996e-05 3.424996e-05 3.421571e-03 3.421571e-03 3.424996e-05
[,12]        [,13]        [,14]        [,15]        [,16]
[1,] 1.674731e-03 4.279741e-03 1.676407e-05 4.322775e-23 1.524176e-05
[2,] 1.218549e-05 1.218549e-05 1.219769e-07 1.218549e-05 4.339709e-08
[3,] 1.254879e-05 1.258652e-11 1.254879e-05 1.254879e-05 4.469094e-08
[4,] 1.261176e-15 1.254883e-05 1.254883e-05 1.254883e-05 4.469108e-08
[5,] 1.218549e-05 1.218549e-05 1.218549e-05 1.219769e-07 4.339709e-08
[6,] 1.218549e-05 1.219769e-07 1.218549e-05 1.218549e-05 4.339709e-08
[7,] 1.218549e-05 1.219769e-07 1.218549e-05 1.218549e-05 4.339709e-08
[8,] 1.218549e-05 1.219769e-07 1.218549e-05 1.218549e-05 4.339709e-08
[9,] 1.218549e-05 1.219769e-07 1.218549e-05 1.218549e-05 4.339709e-08
[10,] 1.218549e-05 1.218549e-05 1.219769e-07 1.218549e-05 4.339709e-08
[11,] 1.218549e-05 1.218549e-05 1.219769e-07 1.218549e-05 4.339709e-08
[12,] 1.218549e-05 1.218549e-05 1.219769e-07 1.218549e-05 4.339709e-08
[13,] 1.218549e-05 1.218549e-05 1.218549e-05 1.219769e-07 4.339709e-08
[14,] 1.218549e-05 1.218549e-05 1.218549e-05 1.219769e-07 4.339709e-08
[15,] 1.218549e-05 1.218549e-05 1.218549e-05 1.219769e-07 4.339709e-08
[16,] 1.218549e-05 1.218549e-05 1.218549e-05 1.219769e-07 4.339709e-08
[17,] 1.218549e-05 1.218549e-05 1.218549e-05 1.219769e-07 4.339709e-08
[18,] 1.218549e-05 1.218549e-05 1.218549e-05 1.219769e-07 4.339709e-08
[19,] 1.218549e-05 1.218549e-05 1.219769e-07 1.218549e-05 4.339709e-08
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
```

BioGeoBEARS documentation built on May 29, 2017, 8:36 p.m.