Skip to content

Commit

Permalink
Merge pull request #9 from joshwlambert/rampal_update
Browse files Browse the repository at this point in the history
Cleaning up R code 4
  • Loading branch information
rsetienne authored Feb 18, 2024
2 parents 26dbae7 + 58a2cb8 commit ff65289
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions answers.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ author: Luis Valente
### Load required packages

```{r}
rm(list=ls())
rm(list = ls())
library(ape)
library(DAISIEprep)
library(DAISIE)
Expand All @@ -17,7 +17,7 @@ library(DAISIE)
### Load tree

```{r}
Insula_tree<-read.nexus("data/Insula.tre")
Insula_tree <- read.nexus("data/Insula.tre")
```

Visualise tree (easier to use Figtree!)
Expand Down Expand Up @@ -107,7 +107,7 @@ As you can see, the results of the 2 extractions (min and asr) are exactly the s
Add missing species "Spec_51", which is not closely related to any species

```{r}
island_tbl<-island_tbl_min
island_tbl <- island_tbl_min
island_tbl <- add_island_colonist(
island_tbl = island_tbl,
clade_name = "Spec_51",
Expand Down Expand Up @@ -153,7 +153,7 @@ Fit model with 5 parameters

```{r M1, message=FALSE, warning=FALSE, cache=TRUE}
M1<-DAISIE_ML(
M1 <- DAISIE_ML(
datalist = insula_data_list,
initparsopt = c(1.5,1.1,20,0.009,1.1),
ddmodel = 11,
Expand All @@ -168,7 +168,7 @@ Fit model with no carrying capacity

```{r M2, message=FALSE, warning=FALSE, cache=TRUE}
M2<-DAISIE_ML(
M2 <- DAISIE_ML(
datalist = insula_data_list,
initparsopt = c(1.5,1.1,0.009,1.1),
idparsopt = c(1,2,4,5),
Expand All @@ -182,7 +182,7 @@ M2
Fit model with no anagenesis (optional)

```{r M3, message=FALSE, warning=FALSE, cache=TRUE}
M3<-DAISIE_ML(
M3 <- DAISIE_ML(
datalist = insula_data_list,
initparsopt = c(1.5,1.1,0.009),
idparsopt = c(1,2,4),
Expand All @@ -196,25 +196,25 @@ M3
Save model results in a table

```{r}
model_results<-rbind(M1,M2,M3)
model_results <- rbind(M1,M2,M3)
model_results
```

Create AIC function for model comparison

```{r}
AIC_compare<- function(LogLik,k){
aic <- (2*k)-(2*LogLik)
AIC_compare <- function(LogLik,k){
aic <- (2 * k) - (2 * LogLik)
return(aic)
}
```

Compute AIC for all the models

```{r}
AICs<-AIC_compare(c(M1$loglik ,M2$loglik, M3$loglik),c(M1$df,M2$df,M3$df))
names(AICs)<-c('M1','M2','M3')
AICs <- AIC_compare(c(M1$loglik,M2$loglik,M3$loglik),c(M1$df,M2$df,M3$df))
names(AICs) <- c('M1','M2','M3')
AICs
```

Expand All @@ -225,11 +225,11 @@ In this case, the preferred model is M3.
Run simulations

```{r Simulations, message=FALSE, warning=FALSE, cache=TRUE}
Insula_sims<-DAISIE_sim(
time=4,
M=1000,
pars=as.numeric(M3[1:5]),
replicates= 100,
Insula_sims <- DAISIE_sim(
time = 4,
M = 1000,
pars = as.numeric(M3[1:5]),
replicates = 100,
verbose = 1,
plot_sims = FALSE)
```
Expand Down

0 comments on commit ff65289

Please sign in to comment.