# function is simplified version of evol.vcv # written by Liam J. Revell 2011 evolvcv.lite<-function(tree,X,maxit=2000,tol=1e-10){ # model 1: common variances & correlation lik1<-function(theta,C,D,y){ v<-theta[1:2]; r<-theta[3] R<-matrix(c(v[1],r*sqrt(v[1]*v[2]),r*sqrt(v[1]*v[2]),v[2]),2,2) V<-kronecker(R,C) a<-solve(t(D)%*%solve(V)%*%D)%*%(t(D)%*%solve(V)%*%y) logL<--t(y-D%*%a)%*%solve(V)%*%(y-D%*%a)/2-n*m*log(2*pi)/2-determinant(V)$modulus[1]/2 return(-logL) } # model 2: different variances, same correlation lik2<-function(theta,C,D,y){ v1<-theta[1:2]; v2<-theta[3:4]; r<-theta[5] R1<-matrix(c(v1[1],r*sqrt(v1[1]*v1[2]),r*sqrt(v1[1]*v1[2]),v1[2]),2,2) R2<-matrix(c(v2[1],r*sqrt(v2[1]*v2[2]),r*sqrt(v2[1]*v2[2]),v2[2]),2,2) V<-kronecker(R1,C[[1]])+kronecker(R2,C[[2]]) a<-solve(t(D)%*%solve(V)%*%D)%*%(t(D)%*%solve(V)%*%y) logL<--t(y-D%*%a)%*%solve(V)%*%(y-D%*%a)/2-n*m*log(2*pi)/2-determinant(V)$modulus[1]/2 return(-logL) } # model 3: same variances, different correlation lik3<-function(theta,C,D,y){ v<-theta[1:2]; r1<-theta[3]; r2<-theta[4] R1<-matrix(c(v[1],r1*sqrt(v[1]*v[2]),r1*sqrt(v[1]*v[2]),v[2]),2,2) R2<-matrix(c(v[1],r2*sqrt(v[1]*v[2]),r2*sqrt(v[1]*v[2]),v[2]),2,2) V<-kronecker(R1,C[[1]])+kronecker(R2,C[[2]]) a<-solve(t(D)%*%solve(V)%*%D)%*%(t(D)%*%solve(V)%*%y) logL<--t(y-D%*%a)%*%solve(V)%*%(y-D%*%a)/2-n*m*log(2*pi)/2-determinant(V)$modulus[1]/2 return(-logL) } # model 4: everything different lik4<-function(theta,C,D,y){ v1<-theta[1:2]; v2<-theta[3:4]; r1<-theta[5]; r2<-theta[6] R1<-matrix(c(v1[1],r1*sqrt(v1[1]*v1[2]),r1*sqrt(v1[1]*v1[2]),v1[2]),2,2) R2<-matrix(c(v2[1],r2*sqrt(v2[1]*v2[2]),r2*sqrt(v2[1]*v2[2]),v2[2]),2,2) V<-kronecker(R1,C[[1]])+kronecker(R2,C[[2]]) a<-solve(t(D)%*%solve(V)%*%D)%*%(t(D)%*%solve(V)%*%y) logL<--t(y-D%*%a)%*%solve(V)%*%(y-D%*%a)/2-n*m*log(2*pi)/2-determinant(V)$modulus[1]/2 return(-logL) } # done internal functions # bookkeeping X<-as.matrix(X) X<-X[tree$tip.label,] n<-nrow(X) # number of species m<-ncol(X) # number of traits if(m!=2) stop("number of traits must equal 2") p<-ncol(tree$mapped.edge) # number of states if(p!=2) stop("number of states must equal 2") D<-matrix(0,n*m,m) for(i in 1:(n*m)) for(j in 1:m) if((j-1)*n