Using MCMC on evolutionary traits with Evomap - r

Here is what I'm running. I'm wondering why I'm getting an error and what the error means/how I could fix it. Is it something to with getting a null value for CDdat? Anyone have an idea? thanks.
library(phytools)
Loading required package: ape
Loading required package: maps
library(adephylo)
Loading required package: ade4
library(geiger)
library(nlme)
library(evomap)
library(caper)
Loading required package: MASS
Loading required package: mvtnorm
library(scales)
Attaching package: ‘scales’
The following object is masked from ‘package:geiger’:
rescale
library(coda)
library(ggplot2); theme_set(theme_light())
library(HDInterval)
Frogtree<-read.nexus("C:/Users/WiensDell3/Desktop/TwilliamsRfiles_AdvertisementCalls/frog_contree")
Call.duration. <- read.csv("C:/Users/WiensDell3/Desktop/TwilliamsRfiles_AdvertisementCalls/Call duration .csv")
CD <- Call.duration.
mydata<-comparative.data(data= CD,phy= Frogtree, names.col= "Species")
FrogTree<- mydata$phy
mydata2 <-mydata$data
x <- mydata2$CD
BMsigma2 <- ace(x, FrogTree, method = "REML")$sigma2[1]
mvBMresults <- mvBM(x, FrogTree, BMsigma2)
tree_mvBMCD <- FrogTree; tree_mvBMCD$edge.length<-mvBMresults$rBL
CDdat<- mydata2$CD; names(CDdat <-rownames(mydata2)
+ )
NULL
Iterations <- 500000
Model_mvBMCD<-anc.Bayes(tree_mvBMCD,CDdat,ngen=Iterations)
Error in invC %*% X : requires numeric/complex matrix/vector arguments
Model_mvBMCD<-anc.Bayes(tree_mvBMCD,CDdat,ngen=Iterations)
Error in invC %*% X : requires numeric/complex matrix/vector arguments

Related

Loading {logistf} breaks MCMCglmm()

Loading the package logistf breaks MCMCglmm(). Unloading logistf before running the command doesn't remove the error.
Why is that? Is there a way to solve this?
Works
library(MCMCglmm)
#> Loading required package: Matrix
#> Loading required package: coda
#> Loading required package: ape
data(PlodiaPO)
MCMCglmm(PO ~ plate, data = PlodiaPO)
#>
#> MCMC iteration = 0
#>
#> MCMC iteration = 1000
#>
#> MCMC iteration = 2000
#>
#> MCMC iteration = 3000
#>
[...]
#> attr(,"class")
#> [1] "MCMCglmm"
Created on 2022-06-07 by the reprex package (v2.0.1)
Doesn't work
library(logistf)
library(MCMCglmm)
#> Loading required package: Matrix
#> Loading required package: coda
#> Loading required package: ape
data(PlodiaPO)
MCMCglmm(PO ~ plate, data = PlodiaPO)
#> Error in terms.formula(formula, data = data): invalid term in model formula
unloadNamespace("logistf")
MCMCglmm(PO ~ plate, data = PlodiaPO)
#> Error in terms.formula(formula, data = data): invalid term in model formula
Created on 2022-06-07 by the reprex package (v2.0.1)
After some research i found that the problem not from logistf but it comes from the imported package formula.tools to reproduce the error try :
library(formula.tools)
#>formula.tools-1.7.1 - Copyright © 2022 Decision Patterns
library(MCMCglmm)
#> Loading required package: Matrix
#> Loading required package: coda
#> Loading required package: ape
data(PlodiaPO)
MCMCglmm(PO ~ plate, data = PlodiaPO)
#> Error in terms.formula(formula, data = data) :
invalid term in model formula
and this issue known for formula.tools see Weird package dependency introduces error
The solution detailed in this issue is:
fork fomula.tools repo
(remove this line)[https://github.com/decisionpatterns/formula.tools/blob/45b6654e4d8570cbaf1e2fd527652471202d97ad/NAMESPACE#L3]
install_github from your repo
OR
run as.character.formula = function(x) as.character.default(x) right after loading formula.tools. That might break code using as.character.formula though (but not sure).
Thanks for this question

error in base backsolve with quantile regression, how do I solve?

If I run the library for 'quantreg', I get a warning that backsolve is masked from base. Then I try to run a quantile regression and I get an error involving backsolve. How can I solve this?
library(quantreg)
Loading required package: SparseM
Attaching package: ‘SparseM’
The following object is masked from ‘package:base’: backsolve
quantile_mod1<- rq(X ~ Y, tau=0.3,data=mydata)
Warning message:
In rq.fit.br(x, y, tau = tau, ...) : Solution may be nonunique
summary(quantile_mod1)
Error in base::backsolve(r, x, k = k, upper.tri = upper.tri, transpose = transpose, :
singular matrix in 'backsolve'. First zero in diagonal [41]
In addition: Warning message:
In summary.rq(quantile_mod1) : 12506 non-positive fis

How to use 2 different functions in 2 different libraries that have the same name

I'm trying to explore the difference in how the gam function works in the mgcv package versus the gam package. But I'm not able to run both gam functions in one R session. I thought if I preface with mgcv::gam or gam::gam it would be able to run the right function, but it looks like I have to detach mgcv in order to run the gam function in the gam package.
library(ISLR)
library(mgcv)
library(gam)
# I get an error message when it runs this
gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
# No error message when I detach mgcv
detach(package:mgcv)
gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
Is there a way I can run both gam functions in one session?
Below is the output:
> library(ISLR)
> library(mgcv)
> library(gam)
> #I get an error message when it runs this
> gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
Error in terms.formula(reformulate(term[i])) :
invalid model formula in ExtractVars
> #No error message when I detach mgcv
> detach(package:mgcv)
> gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
Warning message:
In model.matrix.default(mt, mf, contrasts) :
non-list contrasts argument ignored
Update: I re-ran this with a clean R session and the story is different. Before, I cleared the workspace but did not have a clear R session. Now, if I run with a clean session the gam.m3 model seems to work. BUT - if I change the order of the library load, and load gam before mgcv, I get the same error. When mgcv is loaded after gam is loaded, I do get this message:
The following objects are masked from ‘package:gam’:
gam, gam.control, gam.fit, s
So I guess just part of the deal of loading mgcv is that you can no longer use certain functions in gam? That is annoying. FYI I get the analogous warning message when loading gam after mgcv is loaded - that certain objects will be masked from package:mgcv.
As shown in my answer to your other question, you can't use gam::s.
However, you can tell R to evaluate the call in the gam package namespace:
library(ISLR)
library(gam)
fit1 <- gam(wage~s(year,4)+s(age,5)+education,data=Wage)
library(mgcv)
gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
#errors
fit2 <- eval(quote(gam(wage~s(year,4)+s(age,5)+education,data=Wage)),
envir = asNamespace("gam"))
#works
all.equal(coef(fit1), coef(fit2))
#[1] TRUE

hclust() - Error in hclust(d) : object 'C_hclust' not found in R

Trying to call the hclust() function in R, and I am getting the following error:
library(stats)
d <- dist(as.matrix(mtcars))
hc <- hclust(d)
Error in hclust(d) : object 'C_hclust' not found
Not sure what is the issue as other functions from the "stats" library work, such as dist (used above)

Why isn't broom::glance working with the gam package?

If I try to run an example from the broom package PDF, I'm getting an error. This is because mgcv is being loaded by the car package. However, even when I after I unload car and mgcv, broom::glance still fails.
How can I get the broom package work with gam::gam and have other packages loaded?
library(reshape2); library(plyr); library(stringr); library(lubridate);
library(knitr); library(sp); library(rgeos); library(lme4); library(formatR);
library(rgdal); library(ggmap); library(maptools); library(Hmisc); library(raster);
library(car); library(extrafont); library(openNLP); library(effects); library(psych);
library(MuMIn); library(GPArotation); library(tuneR); library(gdata); library(xlsx);
library(combinat); library(stargazer); library(grid); library(scales); library(dplyr);
library(english); library(repmis); library(pscl); library(testthat); library(broom);
library(ggplot2); library(geosphere); library(moult); library(GGally); library(pavo);
library(stringi); library(mgcv); library(gam)
detach(package:car, unload = TRUE)
detach(package:mgcv, unload = TRUE)
if (require("gam", quietly = TRUE)) {
data(kyphosis)
g <- gam::gam(Kyphosis ~ s(Age,4) + Number, family = binomial, data = kyphosis)
tidy(g)
augment(g)
glance(g)
}
Warning: namespace ‘mgcv’ is not available and has been replaced
by .GlobalEnv when processing object ‘kyphosis’
Warning: namespace ‘mgcv’ is not available and has been replaced
by .GlobalEnv when processing object ‘kyphosis’
Warning: namespace ‘mgcv’ is not available and has been replaced
by .GlobalEnv when processing object ‘kyphosis’
Warning: namespace ‘mgcv’ is not available and has been replaced
by .GlobalEnv when processing object ‘kyphosis’
Warning: namespace ‘mgcv’ is not available and has been replaced
by .GlobalEnv when processing object ‘kyphosis’
Warning: namespace ‘mgcv’ is not available and has been replaced
by .GlobalEnv when processing object ‘kyphosis’
Error in `$<-.data.frame`(`*tmp*`, "logLik", value = numeric(0)) :
replacement has 0 rows, data has 1

Resources