-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitEvolPar.R
57 lines (57 loc) · 2.01 KB
/
fitEvolPar.R
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
fitEvolPar<-function(dat,tre,mod) {
require(nlme)
require(ape)
require(evobiR)
if (identical(setdiff(tre$tip.label, rownames(dat)), character(0))==FALSE) {
stop("row names in the data and tips in the tree do not match")
}
if (ncol(dat) > 2) {
stop("data frame contains more than two columns")
}
dat<-ReorderData(tre,dat)
spp<<-rownames(dat)
Wcv<<-diag(vcv.phylo(tre))
colnames(dat)<-c("x","y")
switch (mod,
OU = {
alpha <- seq(0, 1, 0.1)
fit <- list()
for (i in seq_along(alpha)) {
cor <- corMartins(alpha[i], phy = tre, fixed = TRUE, form=~spp)
if (is.ultrametric(tre)==TRUE) {
fit[[i]] <- gls(y~x, correlation = cor, data=dat, na.action=na.exclude, method = "ML")
} else {
fit[[i]] <- gls(y~x, correlation = cor, data=dat, weights=varFixed(~Wcv), na.action=na.exclude, method = "ML")
}
}
print((which.max(sapply(fit, logLik))-1)*0.1)
},
lambda = {
lambda<- seq(0, 1, 0.1)
fit <- list()
for (i in seq_along(lambda)) {
cor <- corPagel(lambda[i], phy = tre, fixed = TRUE, form=~spp)
if (is.ultrametric(tre)==TRUE) {
fit[[i]] <- gls(y~x, correlation = cor, data=dat, na.action=na.exclude, method = "ML")
} else {
fit[[i]] <- gls(y~x, correlation = cor, data=dat, weights=varFixed(~Wcv), na.action=na.exclude, method = "ML")
}
}
print((which.max(sapply(fit, logLik))-1)*0.1)
},
EB = {
g <- seq(0.1, 1, 0.1)
fit <- list()
for (i in seq_along(g)) {
cor <- corBlomberg(g[i], phy = tre, fixed = TRUE, form=~spp)
if (is.ultrametric(tre)==TRUE) {
fit[[i]] <- gls(y~x, correlation = cor, data=dat, na.action=na.exclude, method = "ML")
} else {
fit[[i]] <- gls(y~x, correlation = cor, data=dat, weights=varFixed(~Wcv), na.action=na.exclude, method = "ML")
}
}
print(which.max(sapply(fit, logLik))*0.1)
},
stop("Please specify an accepted evolutionary model")
)
}