Loading {logistf} breaks MCMCglmm() - r

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

Related

Using MCMC on evolutionary traits with Evomap

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

Checkpoint package R not finding dependency

I'm trying to set up a checkpoint using the checkpoint package.
I'm doing a simple example in which I only load tidyverse and I run into an error for missing dependencies.
library(checkpoint)
#> Warning: package 'checkpoint' was built under R version 3.6.3
#>
#> checkpoint: Part of the Reproducible R Toolkit from Microsoft
#> https://mran.microsoft.com/documents/rro/reproducibility/
checkpoint(snapshotDate = "2020-06-12", forceInstall = TRUE)
#> Scanning for packages used in this project
#> No file at path 'C:\...'.
#> - Discovered 3 packages
#> Unable to parse 1 files:
#> - reprex_reprex.spin.Rmd
#> Removing packages to force re-install
#> Installing packages used in this project
#> - Installing 'tidyverse'
#> tidyverse
#> also installing the dependencies 'tibble', 'broom', 'dbplyr', 'forcats', 'haven', 'modelr'
#> - Installing 'knitr'
#> knitr
#> checkpoint process complete
#> ---
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.6.3
#> Error: package or namespace load failed for 'tidyverse' in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]):
#> namespace 'rlang' 0.4.4 is already loaded, but >= 0.4.6 is required
Created on 2020-06-13 by the reprex package (v0.3.0)
According to the documentation, I'd expect the dependencies to install automatically but this seems not to be the case. If I update the rlang package on using install.packages everything works fine:
library(checkpoint)
#> Warning: package 'checkpoint' was built under R version 3.6.3
#>
#> checkpoint: Part of the Reproducible R Toolkit from Microsoft
#> https://mran.microsoft.com/documents/rro/reproducibility/
checkpoint(snapshotDate = "2020-06-12", forceInstall = TRUE, auto.install.knitr = FALSE)
#> Scanning for packages used in this project
#> No file at path 'C:\...'.
#> - Discovered 2 packages
#> Unable to parse 1 files:
#> - reprex_reprex.spin.Rmd
#> Removing packages to force re-install
#> Installing packages used in this project
#> - Installing 'tidyverse'
#> tidyverse
#> also installing the dependencies 'tibble', 'broom', 'dbplyr', 'forcats', 'haven', 'modelr'
#> checkpoint process complete
#> ---
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.6.3
#> Warning: package 'ggplot2' was built under R version 3.6.2
#> Warning: package 'tibble' was built under R version 3.6.3
#> Warning: package 'tidyr' was built under R version 3.6.2
#> Warning: package 'readr' was built under R version 3.6.2
#> Warning: package 'purrr' was built under R version 3.6.2
#> Warning: package 'dplyr' was built under R version 3.6.3
#> Warning: package 'stringr' was built under R version 3.6.2
#> Warning: package 'forcats' was built under R version 3.6.3
Created on 2020-06-13 by the reprex package (v0.3.0)
I believe it might be related to having rlang loaded via a namespace (and not attached), but I thought checkpoint isolated these issues.

ETS from fable package in R (can I do it with out tsibble)

I am trying to use ETS function from fable package (following this tutorial link). Ideally I would like to do it without using tsibble functionality. In particular I am trying to generate forecast:
library(tsibble)
library(fable)
library(tidyverse)
fit <- ETS(1:63)
forecast(fit, h =2)
returns error:
Error in UseMethod("forecast") :
no applicable method for 'forecast' applied to an object of class "c('mdl_defn', 'R6')"
another try
summary(fit)
also returns error
Error in object[[i]] : wrong arguments for subsetting an environment
So can I used it without full tsibble functionality? It was so simple with ARIMA from forecast package.
If it is not possible without tsibble what would be the quickest way to cast it as tsibble data?
You need to use tsibbles, but it is very easy to do so.
library(tsibble)
library(fable)
library(tidyverse)
ts(1:63) %>%
as_tsibble() %>%
model(ETS(value)) %>%
forecast(h=2)
#> # A fable: 2 x 4 [1]
#> # Key: .model [1]
#> .model index value .distribution
#> <chr> <dbl> <dbl> <dist>
#> 1 ETS(value) 64 64 N(64, 0)
#> 2 ETS(value) 65 65 N(65, 0)
Created on 2020-02-19 by the reprex package (v0.3.0)

How to fix error when using studpermu.test with large dataset

I am using studpermu.test on a large hyperframe of ppp objects and it returns an error.
I am using studpermu.test on a large hyperframe of 250 ppp objects, each with thousands of points (~ 2000-5000), and a grouping factor with 5 levels, with equal group sizes of 50. The function runs successfully on a smaller subset of point patterns but returns an error when I try to run the function on the whole hyperframe.
Here is a smaller reproducible example which gives the same error.
X <- runifpoint(20, nsim = 250)
h <- hyperframe(ppp = X, group = rep(1:5, each=50))
studpermu.test(h, ppp ~ group)
Error in if (npossible < max(100, nperm)) warning("Don't expect exact results - group sizes are too small") : missing value where TRUE/FALSE needed
In addition: Warning message: In factorial(sum(m)) : value out of range in 'gammafn'
This is a bug in the current CRAN version of spatstat. Here is a small reproducible example (which it would have been great if you provided in your question):
library(spatstat)
#> Loading required package: spatstat.data
#> Loading required package: nlme
#> Loading required package: rpart
#>
#> spatstat 1.61-0 (nickname: 'Puppy zoomies')
#> For an introduction to spatstat, type 'beginner'
X <- runifpoint(20, nsim = 250)
h <- hyperframe(ppp = X, group = rep(1:5, each=50))
studpermu.test(h, ppp ~ group)
#> Error in if (npossible < max(100, nperm)) warning("Don't expect exact results - group sizes are too small"): missing value where TRUE/FALSE needed
This is now fixed in the development version of spatstat. For now you can install the development version from GitHub which should fix the issue:
library(remotes)
install_github("spatstat/spatstat")
With this version I get (didn't set a seed so results may vary, but no error should occur):
library(spatstat)
#> Loading required package: spatstat.data
#> Loading required package: nlme
#> Loading required package: rpart
#>
#> spatstat 1.61-0.021 (nickname: 'New improved formula')
#> For an introduction to spatstat, type 'beginner'
X <- runifpoint(20, nsim = 250)
h <- hyperframe(ppp = X, group = rep(1:5, each=50))
studpermu.test(h, ppp ~ group)
#>
#> Studentized permutation test for grouped point patterns
#> ppp ~ group
#> 5 groups: 1, 2, 3, 4, 5
#> summary function: Kest, evaluated on r in [0, 0.25]
#> test statistic: T, 999 random permutations
#>
#> data: h
#> T = 3.6133, p-value = 0.125
#> alternative hypothesis: not the same K-function

Performing multiclass PLS-DA with mlr package in R

I want to use partial least squares discriminant analysis (PLS-DA) to solve a classification problem where there are multiple classes to be predicted. I know PLS-DA is not limited to the two class problem, and I believe that using plsda from the Caret package can handle this ok, but when I try to build a PLS-DA model in the mlr package, I get an error telling me my task is a "multiclass-problem, but learner 'classif.plsdaCaret' does not support that!"
Is it possible to build a multiclass PLS-DA model using mlr and am I simply using the wrong learner? Here is a reproducible example:
# LOAD PACKAGES ----
#install.packages("BiocManager")
#BiocManager::install("mixOmics")
library(mlr)
library(tidyverse)
library(mixOmics)
# LOAD IN DATA ----
data(liver.toxicity)
liverTib <- as.tibble(cbind(liver.toxicity$treatment$Treatment.Group,
liver.toxicity$gene)
)
names(liverTib)[1] <- "Treatment"
liverTib
# MAKE TASK, LEARNER AND ATTEMPT TO BULD MODEL
liverTask <- makeClassifTask(data = liverTib, target = "Treatment")
plsda <- makeLearner("classif.plsdaCaret")
liverModel <- train(plsda, liverTask)
In the development version of mlr (v2.14.0.9000) multiclass classification via plsdaCaret model is enabled. You can download the package from GitHub using this code:
install.packages("remotes")
remotes::install_github("mlr-org/mlr")
A PLS-DA example with 3 classes:
library(mlr)
#> Loading required package: ParamHelpers
tsk <- makeClassifTask("iris", iris, target = "Species")
lrn1 <- makeLearner("classif.plsdaCaret")
mod1 <- train(lrn1, tsk)
prd <- predict(mod1, tsk)
calculateConfusionMatrix(prd)
#> predicted
#> true setosa versicolor virginica -err.-
#> setosa 50 0 0 0
#> versicolor 0 31 19 19
#> virginica 0 8 42 8
#> -err.- 0 8 19 27
Created on 2019-07-18 by the reprex package (v0.3.0)
(This pull request solved the issue.)
The current implementation does not support multiclass, see here: https://mlr.mlr-org.com/articles/tutorial/integrated_learners.html
You can change the code for the learner (https://github.com/mlr-org/mlr/blob/master/R/RLearner_classif_plsdaCaret.R) to make multiclass possible (see here for an instruction: https://mlr.mlr-org.com/articles/tutorial/create_learner.html).

Resources