library(ape)
library(MASS)
library(phytools)
## Loading required package: maps
CompareRates.multTrait<-function(phy,x,TraitCov=T,ms.err=NULL,ms.cov=NULL){
  build.chol<-function(b){
    c.mat<-matrix(0,nrow = p,ncol = p)
    c.mat[lower.tri(c.mat)]<-b[-1]
    c.mat[p,p]<-exp(b[1])
    c.mat[1,1]<-sqrt(sum((c.mat[p,])^2))
    if(p>2){
      for(i in 2:(p-1)){
        c.mat[i,i]<-ifelse((c.mat[1,1]^2-sum((c.mat[i,])^2))>0,sqrt(c.mat[1,1]^2-sum((c.mat[i,])^2)),0)
      }
    }
    return(c.mat)
  }
  
  x<-as.matrix(x)
  N<-nrow(x)
  p<-ncol(x)
  C<-vcv.phylo(phy)
  C<-C[rownames(x),rownames(x)]
  
  I<-diag(1,N)
  
  if (is.matrix(ms.err)){
    ms.err<-as.matrix(ms.err[rownames(x),])}
  if (is.matrix(ms.cov)){
    ms.cov<-as.matrix(ms.cov[rownames(x),])}
  
  a.obs<-colSums(solve(C)) %*% x / sum(solve(C))
  one<-matrix(1,N,1)
  
  R.obs<-t(x-one%*%a.obs)%*%solve(C)%*%(x-one%*%a.obs)/N
  
  IIDcon<-function(trait=trait,mu=mu,sigma=sigma,C=C){
    z<- (trait-mu)/sigma
    eC<-eigen(C)
    D.n5<-diag(1/sqrt(eC$values))
    C.neg.5<-eC$vectors%*%D.n5%*%t(eC$vectors)
    return(C.neg.5%*%trait)
  }
  
  a.obs.pmm<-array(NA,p)    
  R.obs.pmm<-array(NA,c(p,p))
  IIDtrait<-array(NA,dim(x))
  h.array<-array(NA,p)
  for(Index in 1:p){
    h<-phylosig(tree=phy,x=x[,Index],method="lambda")$lambda    
    h.array[Index]<-h
    Sh<-h^2*C + (1-h^2)*I
    assign(paste("Sh",Index,sep=""),Sh)
    a.obs.pmm[Index]<-colSums(solve(Sh)) %*% x[,Index] / sum(solve(Sh))
    R.obs.pmm[Index,Index]<-t(x[,Index]-one%*%a.obs.pmm[Index])%*%solve(Sh)%*%(x[,Index]-one%*%a.obs.pmm[Index])/N
    IIDtrait[,Index]<-IIDcon(trait=x[,Index],mu=a.obs.pmm[Index],sigma=R.obs.pmm[Index,Index],C=Sh)  
  }
  
  
  for(i in 1: (p-1)){
    for( j in (i+1):p){
      R.obs.pmm[i,j]<-R.obs.pmm[j,i]<-  cov(IIDtrait[,i],IIDtrait[,j])
    }
  }
  
  dim(a.obs.pmm)
  a.obs.pmm<-matrix(a.obs.pmm,nrow=1,ncol=p)
  a.obs.pmm
  dim(a.obs.pmm)
  R.obs.pmm
  
  D<-matrix(0,N*p,p)
  for(i in 1:(N*p)){
    for(j in 1:p){
      if((j-1)*N < i && i<=j*N){
        D[i,j]=1.0
      }
    }
  }
  y<-as.matrix(as.vector(x))
  
  if (TraitCov==F){R.obs.pmm<-diag(diag(R.obs.pmm),p)}
  
  Sh1
  Sh2
  h1<-h.array[1]
  h2<-h.array[2]
  h12<-(h1+h2)/2
  Sh12 <- h12^2*C + (1-h12^2)*I
  RkronSh<-rbind(cbind(R.obs.pmm[1,1]*Sh1,R.obs.pmm[1,2]*Sh12),
                 cbind(R.obs.pmm[1,2]*Sh12,R.obs.pmm[2,2]*Sh2))
  RkronSh

  
  LLik.obs.pmm<-ifelse(is.matrix(ms.err)==TRUE, 
                       -t(y-D%*%t(a.obs.pmm))%*%ginv((RkronSh+diag(as.vector(ms.err))))%*%(y-D%*%t(a.obs.pmm))/2-N*p*log(2*pi)/2-determinant((RkronSh+ diag(as.vector(ms.err))))$modulus[1]/2,
                       -t(y-D%*%t(a.obs.pmm))%*%ginv(RkronSh)%*%(y-D%*%t(a.obs.pmm))/2-N*p*log(2*pi)/2-determinant(RkronSh)$modulus[1]/2)
  
  
  sigma.mn.pmm<-mean(diag(R.obs.pmm))
  if(is.matrix(ms.err) && is.matrix(ms.cov)){
    within.spp<-cbind(ms.err,ms.cov)
    rc.label<-NULL
    for(i in 1:p){
      rc.label<-rbind(rc.label,c(i,i))
    }
    for(j in 2:p){
      if(i!=j&&i<j){
        rc.label<-rbind(rc.label,c(i,j))
      }
    }
    m.e<-NULL
    for(i in 1:p){
      temp<-NULL
      for(j in 1:p){
        for(k in 1:nrow(rc.label)){
          if(setequal(c(i,j),rc.label[k,])==T)
          {tmp<-cbind(tmp,diag(within.spp[,k]))}
        }
      }
      m.e<-rbind(m.e,tmp)
    }
  }
  

  R<-R.obs.pmm
  param<-c(sigma.mn.pmm,0.5,0.5)
  names(param)<-c("sigma","h1","h2")
  lik.covF.pmm<-function(param){
    h1<-param["h1"]
    h2<-param["h2"]
    sigma<-param["sigma"]
    h12<-(h1+h2)/2
    Sh12 <- h12^2*C + (1-h12^2)*I
    diag(R)<-sigma.mn.pmm
    RkronSh<-rbind(cbind(R[1,1]*Sh1,R[1,2]*Sh12),
                   cbind(R[1,2]*Sh12,R[2,2]*Sh2))
    LLik<-ifelse(is.matrix(ms.err)==TRUE,
                 -t(y-D%*%t(a.obs.pmm))%*%ginv((RkronSh+m.e))%*%(y-D%*%t(a.obs.pmm))/2-N*p*log(2*pi)/2-determinant((RkronSh+ m.e))$modulus[1]/2,
                 -t(y-D%*%t(a.obs.pmm))%*%ginv(RkronSh)%*%(y-D%*%t(a.obs.pmm))/2-N*p*log(2*pi)/2-determinant(RkronSh)$modulus[1]/2)
    if(LLik==-Inf){LLik<--1e+10}
    return(-LLik)
  }
  

  lik.covT.pmm<-function(param){
    h1<-param["h1"]
    h2<-param["h2"]
    sigma<-param["sigma"]
    R.offd<-param["R.offd"]
    
    h12<-(h1+h2)/2
    Sh12 <- h12^2*C + (1-h12^2)*I
    low.chol<-build.chol(c(sigma,R.offd))
    R<-low.chol%*%t(low.chol)
    RkronSh<-rbind(cbind(R[1,1]*Sh1,R[1,2]*Sh12),
                   cbind(R[1,2]*Sh12,R[2,2]*Sh2))
    LLik <- ifelse(is.matrix(ms.err)==TRUE,
                   -t(y-D%*%t(a.obs.pmm))%*%ginv(RkronSh+m.e)%*%(y-D%*%t(a.obs.pmm))/2-N*p*log(2*pi)/2-determinant(RkronSh+m.e)$modulus[1]/2,
                   -t(y-D%*%t(a.obs.pmm))%*%ginv(RkronSh)%*%(y-D%*%t(a.obs.pmm))/2-N*p*log(2*pi)/2-determinant(RkronSh)$modulus[1]/2
    )
    if(LLik==-Inf){LLik<--le+10}
    return(-LLik)
  }
  
  sigma.upper<-2*max(apply(x,2,sd))
  p0<-c(0.5,0.5,sigma.mn.pmm)
  names(p0)<-c("h1","h2","sigma")
  if(TraitCov==F){model.pmm<-optim(p0,fn=lik.covF.pmm,method
                                   ="L-BFGS-B",lower = c(0,0,0),upper=c(1,1,sigma.upper))}
  
  R.offd<-rep(0,(p*(p-1)/2))
  p0<-c(0.5,0.5,sigma.mn.pmm,0)
  names(p0)<-c("h1","h2","sigma","R.offd")
  if(TraitCov==T){model1.pmm<-
    optim(par=p0,fn=lik.covT.pmm,method="L-BFGS-B",lower = c(0,0,0,0),upper=c(1,1,sigma.upper,sigma.upper))
  }
  
  if(TraitCov==F){R.constr.pmm<-diag(model.pmm$par["sigma"],p)}
  if(TraitCov==T){
    chol.mat<-build.chol(model1.pmm$par[c("sigma","R.offd")])
    R.constr.pmm<-chol.mat%*%t(chol.mat)
  }
  
  if(model1.pmm$convergence==0){
    message.pmm<-"Optimization has converged."}else{
      message.pmm<-"Optim may not have converrged.
  Consideer changing startt value or lower/upper limits."}
  
  LRT.pmm<-(-2*((-model1.pmm$value-LLik.obs.pmm)))
  
  LRT.prob.pmm<-pchisq(LRT.pmm, (p-1),lower.tail = FALSE)
  
  AIC.obs.pmm<- -2*LLik.obs.pmm+2*p+2*p+2*p #(2p twice: 1x for rates, 1x for anc.states,1x for h)
  AIC.common.pmm<--2*(-model1.pmm$value)+2+2*p+2*p #(2*1:for 1 rate 2p for anc.states)
  
  return(
    
    list(
      Robs.pmm=R.obs.pmm,
      Rconstrained.pmm=R.constr.pmm,
      Lobs.pmm=LLik.obs.pmm,
      Lconstrained.pmm=(-model1.pmm$value),
      LRTest.pmm=LRT.pmm,
      Prob.pmm=LRT.prob.pmm,
      AICc.obs.pmm=AIC.obs.pmm,
      AICc.constrained.pmm=AIC.common.pmm,
      optimmessage.pmm=message.pmm
    )
  )
}

### Sample code
phy<-rcoal(5)
plot(phy)

x<- matrix(c(rnorm(5,2,1),rnorm(5,0,0.5)),ncol=2)
rownames(x)<-phy$tip.label#LETTERS[1:N]

CompareRates.multTrait(phy=phy,x=x,TraitCov=T,ms.err=NULL,ms.cov=NULL)
## $Robs.pmm
##            [,1]       [,2]
## [1,]  0.5396813 -0.1249293
## [2,] -0.1249293  0.2374261
## 
## $Rconstrained.pmm
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
## 
## $Lobs.pmm
## [1] -8.124351
## 
## $Lconstrained.pmm
## [1] -10.08756
## 
## $LRTest.pmm
## [1] 3.926419
## 
## $Prob.pmm
## [1] 0.04753307
## 
## $AICc.obs.pmm
## [1] 28.2487
## 
## $AICc.constrained.pmm
## [1] 30.17512
## 
## $optimmessage.pmm
## [1] "Optimization has converged."
#####

tree<-read.tree("http://tonyjhwueng.info/phyrates/ple.nwk")
plot(tree)

tree$tip.label
##  [1] "Plethodon_dorsalis"       "Plethodon_ventralis"     
##  [3] "Plethodon_angusticlavius" "Plethodon_welleri"       
##  [5] "Plethodon_punctatus"      "Plethodon_wehrlei"       
##  [7] "Plethodon_websteri"       "Plethodon_teyahalee"     
##  [9] "Plethodon_cylindraceus"   "Plethodon_variolatus"    
## [11] "Plethodon_chlorobryonis"  "Plethodon_chattahoochee" 
## [13] "Plethodon_cheoah"         "Plethodon_shermani"      
## [15] "Plethodon_amplus"         "Plethodon_meridianus"    
## [17] "Plethodon_montanus"       "Plethodon_albagula"      
## [19] "Plethodon_sequoyah"       "Plethodon_ocmulgee"      
## [21] "Plethodon_savannah"       "Plethodon_grobmani"      
## [23] "Plethodon_kisatchie"      "Plethodon_mississippi"   
## [25] "Plethodon_kiamichi"       "Plethodon_aureolus"      
## [27] "Plethodon_glutinosus"     "Plethodon_jordani"       
## [29] "Plethodon_metcalfi"       "Plethodon_ouachitae"     
## [31] "Plethodon_fourchensis"    "Plethodon_caddoensis"    
## [33] "Plethodon_kentucki"       "Plethodon_petraeus"      
## [35] "Plethodon_yonahlossee"    "Plethodon_hubrichti"     
## [37] "Plethodon_nettingi"       "Plethodon_richmondi"     
## [39] "Plethodon_electromorphus" "Plethodon_cinereus"      
## [41] "Plethodon_shenandoah"     "Plethodon_hoffmani"      
## [43] "Plethodon_virginia"       "Plethodon_serratus"
df<-read.csv("http://tonyjhwueng.info/phyrates/Adams2012-SystBiolData.csv")
spX<-strsplit(as.character(df$X),"_")
phy<-tree
spX<-strsplit(as.character(df$X),"_")
spname<-array(NA,length(phy$tip.label))
for(Index in 1:length(phy$tip.label)){
  spname[Index]<-paste("Plethodon_", spX[[Index]][2],sep="")
}
spname
##  [1] "Plethodon_albagula"       "Plethodon_amplus"        
##  [3] "Plethodon_angusticlavius" "Plethodon_aureolus"      
##  [5] "Plethodon_caddoensis"     "Plethodon_chattahoochee" 
##  [7] "Plethodon_cheoah"         "Plethodon_chlorobryonis" 
##  [9] "Plethodon_cinereus"       "Plethodon_cylindraceus"  
## [11] "Plethodon_dorsalis"       "Plethodon_electromorphus"
## [13] "Plethodon_fourchensis"    "Plethodon_glutinosus"    
## [15] "Plethodon_grobmani"       "Plethodon_hoffmani"      
## [17] "Plethodon_hubrichti"      "Plethodon_jordani"       
## [19] "Plethodon_kentucki"       "Plethodon_kiamichi"      
## [21] "Plethodon_kisatchie"      "Plethodon_meridianus"    
## [23] "Plethodon_metcalfi"       "Plethodon_mississippi"   
## [25] "Plethodon_montanus"       "Plethodon_nettingi"      
## [27] "Plethodon_ocmulgee"       "Plethodon_ouachitae"     
## [29] "Plethodon_petraeus"       "Plethodon_punctatus"     
## [31] "Plethodon_richmondi"      "Plethodon_savannah"      
## [33] "Plethodon_sequoyah"       "Plethodon_serratus"      
## [35] "Plethodon_shenandoah"     "Plethodon_shermani"      
## [37] "Plethodon_teyahalee"      "Plethodon_variolatus"    
## [39] "Plethodon_ventralis"      "Plethodon_virginia"      
## [41] "Plethodon_websteri"       "Plethodon_wehrlei"       
## [43] "Plethodon_welleri"        "Plethodon_yonahlossee"
df$X<-spname

HeadLength<-df$HeadLength
names(HeadLength)<-spname
BodyWidth<-df$BodyWidth
names(BodyWidth)<-spname

HeadLength<-HeadLength[phy$tip.label]
BodyWidth<-BodyWidth[phy$tip.label]
x<-cbind(HeadLength,BodyWidth)
CompareRates.multTrait(phy=phy,x=x,TraitCov=T,ms.err=NULL,ms.cov=NULL)
## $Robs.pmm
##           [,1]      [,2]
## [1,] 0.2222583 0.1252084
## [2,] 0.1252084 0.1251008
## 
## $Rconstrained.pmm
##           [,1]      [,2]
## [1,] 1.0166341 0.1300416
## [2,] 0.1300416 1.0166341
## 
## $Lobs.pmm
## [1] -126.824
## 
## $Lconstrained.pmm
## [1] -187.8177
## 
## $LRTest.pmm
## [1] 121.9874
## 
## $Prob.pmm
## [1] 2.323066e-28
## 
## $AICc.obs.pmm
## [1] 265.648
## 
## $AICc.constrained.pmm
## [1] 385.6354
## 
## $optimmessage.pmm
## [1] "Optimization has converged."