Error "unused argument (env = environment(f)" in lme4 - r

I am trying to perform an lmer in R, from the lme4 package, but I keep getting the error :
Error in reformulate(paste0("(", vapply(findbars(f), deparse1, ""), ")"), :
unused argument (env = environment(f))".
Any ideas? Example with iris:
library(lme4)
lmer(iris$Sepal.Length ~ iris$Sepal.Width + (1|iris$Species))
(I know this doesn't make much sense intuitively, but as an example)

This is a bug/incompatibility between the current (CRAN) version of lme4, 1.1-28, and versions of R older than 3.6.0. From the r-sig-mixed-models mailing list:
This is a bug that was recently noted (and fixed in the development
version) here:
https://github.com/lme4/lme4/issues/664
Running this code snippet
assign('reformulate', envir = topenv(),
function(..., env = parent.env) {
f <- base::reformulate(...)
environment(f) <- env
return(f)
})
might fix the problem.
Alternately, if you have development tools (compilers etc.) installed on your computer, remotes::install_github("lme4/lme4") will install the development version (which should also fix the problem).
Or you can install a more recent version of R (which might be the best solution and save you future headaches) — see instructions here for installing an up-to-date version of R on Ubuntu.

Related

RStudio session aborts when I run gRbase::stepwise function (undirected graphs)

I'm doing an R project on RStudio (RStudio 2021.09.1+372 "Ghost Orchid" Release for Windows), (R version 4.1.2).
I'm working on Windows 10 x64.
I want to create an Undirected Graph from a dataset:
library(gRbase)
library(gRim)
library(gRain)
data(BodyFat)
BodyFat <- BodyFat[-c(31,42,48,76,86,96,159,169,175,182,206),]
BodyFat$Age <- sqrt(BodyFat$Age)
BodyFat$Weight <- sqrt(BodyFat$Weight)
names(BodyFat)[names(BodyFat) == 'BodyFat'] <- 'BodyFatPerc'
gRbodyfat <- BodyFat[,2:15]
graph.MyGraph <- cmod(~.^., data = gRbodyfat)
AIC.MyGraph <- gRbase::stepwise(graph.MyGraph)
I'm still exploring RStudio and graphical models, so I run these lines from the console, one by one.
When I run the last line of code, R session aborts and I get the following pop-up message:
I've also tried the following lines of code (I changed the last line):
library(gRbase)
library(gRim)
library(gRain)
data(BodyFat)
BodyFat <- BodyFat[-c(31,42,48,76,86,96,159,169,175,182,206),]
BodyFat$Age <- sqrt(BodyFat$Age)
BodyFat$Weight <- sqrt(BodyFat$Weight)
names(BodyFat)[names(BodyFat) == 'BodyFat'] <- 'BodyFatPerc'
gRbodyfat <- BodyFat[,2:15]
graph.MyGraph <- cmod(~.^., data = gRbodyfat)
AIC.MyGraph <- stepwise(graph.MyGraph)
but I get the same problem. I tried three or four times running those lines of code, R aborted everytime.
My gRbase, gRim, gRain, Rgraphviz and RBGL libraries are in the home folder:
C:/Users/MyUser/Documents/R/win-library/4.1
Any advice?
EDIT:
I've tried uninstalling/reinstalling R, Rtools, RStudio, reinstalling libraries, updating them, I've tried launching RStudio as Administrator, I've tried changing computer.
The last thing I tried is this:
uninstalling R, Rtools and RStudio
deleting .RData and .Renviron files in the Documents folder
deleting all libraries
downloading R (latest version) from CRAN webpage and installing it as Administrator
downloading Rtools (latest version from webpage) and installing it as Administrator
creating the .Renviron file as described here
downloading RStudio (latest version from webpage) and installing it as Administrator
installing BiocManager as described here (when asked to update Matrix library I said NO, I thought it could be the problem)
installing gRbase, gRim, gRain libraries as described here (when asked to update fansi library I said NO because it said "There are binary versions available but the source versions are later", I thought it could be the problem)
Tried again the lines of code
Still got the problem, R session aborted again.
A friend of mine doing a similar project used gRbase::stepwise function, so I inserted his code in the console and the function worked. So I think the problem might be the object of the function, graph.MyGraph. The cmod function works fine, so maybe there's a problem with gRbodyfat, which is used to build graph.MyGraph.
If I run these lines (I tried changing the dataset, I used BodyFat instead of gRbodyfat):
graph.MyGraph <- cmod(~.^., data = BodyFat)
AIC.MyGraph <- gRbase::stepwise(graph.MyGraph)
I get the following error:
Error in dim(res) <- c(NSEL, NSET) :
dims [product 210] do not match the length of object [14]
Don't know what to do.
EDIT:
I've tried debugging gRbase::stepwise function and I get this:
The problem appears to be the iModel function.
EDIT:
I've discovered that if I use graph.MyGraph <- cmod(~.^1, data = gRbodyfat) the gRbase::stepwise function works fine.

R - Alternatives to, or fix for, LMERConvenienceFunctions, for use with LME4?

I am crossposting this from https://stats.stackexchange.com/questions/488201/alternatives-to-or-fix-for-lmerconveniencefunctions-for-use-with-lme4 as it was suggested that people might have more relevant knowledge here.
Recently I updated from R version 3.6 to version 4.0 for my analyses, and noticed that LMERConvenienceFunctions stopped working. Specifically, I use it in conjunction with LME4.
Whenever I try to use the bfFixefLMER_F.fnc (backfitting of fixed effects in LMER models) or pamer.fnc (compute upper- and lower-bound p-values for the analysis of variance or deviance) for a LMER model fitted through LME4, regardless of dataset, I am met with the error "Error in pf(anova.table[term, "F value"], anova.table[term, "Df"], nrow(model#frame) - : Non-numeric argument to mathematical function". I have tried this on two separate computers with the same result. Now, as far as I can tell, LMERConvenienceFunctions hasn't been updated since 2015, so I'm not holding out hope that a fix is forthcoming.
I tried reverting to R 3.6.2, but found the same error using the versions of LME4 that were out shortly before R 4.0 came out. I have finally found the previous version I was using, so this will (hopefully) fix it for my current analysis, but doesn't help if I want to use the most recent version of R and LME4 going forwards.
Other functions of LMERConvenience (namely fitLMER.fnc and mcp.fnc) seem to be working properly, so it doesn't seem to be a systematic issue, but it is definitely one that significantly impedes my work.
Does anyone have any suggestions for alternative packages, or could anyone offer any advice on editing the LMERConvenienceFunctions package so that I can get the broken functions working again? I don't have any experience with changing the coding within packages, so would be starting from bare basics there.
I am also aware that there are some workarounds through adding extra code in my R script, as I did find in my searching for an answer that it had previously been a problem with the same package in 2014 (https://stat.ethz.ch/pipermail/r-sig-mixed-models/2014q2/022264.html), but I am not familiar with writing that kind of code, so would appreciate any guidance there as well.
I managed to get some help from the lovely people working with LME4 on github; for anyone who has this issue in the future, there are a few things of note:
I have emailed the listed maintainer of LMERConvenienceFunctions asking for them to update the CRAN version so that it abides by the rules of CRAN. Hopefully he does this.
For a fix for those using LME4 and LMERConvenienceFunctions, but who are NOT also using lmerTest, recently the anova table headers "Chi Df" and"Df" were updated to "Df" and "npar" respectively (reasons outlined here:https://github.com/lme4/lme4/issues/528). This is an issue for LMERConvenienceFunctions because pamer.fnc called to Df, so needs to be updated to call to npar instead. Further, bfFixefLMER_F.fnc calls to pamer.fnc, so is fixed when pamer.fnc is updated. For anyone unsure how, I found the code using getAnywhere() and modified it, so just copy the code below and paste it once right near the start of the file:
pamer.fnc <- function (model, ndigits = 4)
{
if (length(rownames(anova(model))) == 0) {
cat("nothing to evaluate: model has only an intercept.\n\n")
cat("printing model fixed effects:\n")
fixef(model)
}
else {
dims <- NULL
rank.X = qr(model#pp$X)$rank
anova.table = anova(model)
anova.table = cbind(anova.table, upper.den.df = nrow(model#frame) -
rank.X)
p.values.upper = as.numeric()
p.values.lower = as.numeric()
for (term in row.names(anova.table)) {
p.values.upper = c(p.values.upper, round(1 - pf(anova.table[term,
"F value"], anova.table[term, "npar"],
nrow(model#frame) - rank.X), ndigits))
model.ranef <- ranef(model)
lower.bound <- 0
for (i in 1:length(names(model.ranef))) {
dims <- dim(model.ranef[[i]])
lower.bound <- lower.bound + dims[1] * dims[2]
}
p.values.lower = c(p.values.lower, 1 - pf(anova.table[term,
"F value"], anova.table[term, "npar"],
nrow(model#frame) - rank.X - lower.bound))
}
dv <- gsub(" ", "", gsub("(.*)~.*",
"\\1", as.character(model#call)[2]))
ss.tot <- sum((model#frame[, dv] - mean(model#frame[,
dv]))^2)
aov.table <- as.data.frame(anova(model))
expl.dev <- vector("numeric")
for (i in rownames(aov.table)) {
expl.dev <- c(expl.dev, aov.table[i, 2]/ss.tot)
}
names(expl.dev) <- rownames(aov.table)
anova.table = round(cbind(anova.table, upper.p.val = p.values.upper,
lower.den.df = nrow(model#frame) - rank.X - lower.bound,
lower.p.val = p.values.lower, `expl.dev.(%)` = expl.dev *
100), ndigits)
return(anova.table)
}
}
(You may also need to run the function script for bfFixefLMER_F.fnc separately again, to let R know that bfFixefLMER_F.fnc should be calling from the updated version of pamer.fnc)
For a fix for those using LME4 and LMERConvenienceFunctions, but who ARE also using lmerTest, you will need to either 1) use numDF instead of npar, or 2) replace "anova.table = anova(model)" with "anova.table = anova(model, dff = "lme4") in versions of lmerTest 3.0 onwards. This appears to be due to the anova.lmerModLmerTest function (added in lmerTest 3.0) overwriting what the call to anova() does, with a Type III Satterthwaite analysis listed before lme4, leading to it being defaulted to when dff is not specified.

Geomorph not functioning after update

I recently updated my MacBook air (2017) to the Mojave OS and proceeded to update my version of R and Rstudio. Unfortunately, ever since the 'plotTangentSpace' command no longer functions appropriately even when attempting to run the example 'plethodon' code from the Geomorph vingette, I always receive the following error in the output (bolded below):
data(plethodon)
Y.gpa<-gpagen(plethodon$land) #GPA-alignment
|====================================================================================| 100%
gp <- interaction(plethodon$species, plethodon$site)
plotTangentSpace(Y.gpa$coords, groups = gp)
Error in plotTangentSpace(Y.gpa$coords, groups = gp) :
unused arguments (Y.gpa$coords, groups = gp)
I have tried uninstalling and reinstalling R for a MacOS many times including versions 4.0.2, 3.6.3, 3.6.1, and 3.2.1. I have also uninstalled and reinstalled XQuartz several times.
If anyone has encountered a similar issue or has any other ideas I can try I would really appreciate the help. Thank you!
If you execute:
?plotTangentSpace
You should see that this is a deprecated function. That means you should not be using it. It says:
Notes for geomorph 3.3.0 and subsequent versions
I suspect you have not updated your copy of the vignette. There are instructions on the help page for an alternate approach. The example in the help page for the suggested function is:
data(plethspecies)
Y.gpa <- gpagen(plethspecies$land) #GPA-alignment
### Traditional PCA
PCA <- gm.prcomp(Y.gpa$coords)
summary(PCA)
plot(PCA, main = "PCA")
For the plethodon data they suggest:
PCA.w.phylo <- gm.prcomp(Y.gpa$coords, phy = plethspecies$phy)
summary(PCA.w.phylo)
plot(PCA.w.phylo, phylo = TRUE, main = "PCA.w.phylo")
So start a new session (to unload the currently loaded geomorph namespace, and execute this at the r session command line:
install.packages("geomorph")
You should be getting version 3.3.1 of the geomorph package.
I think I found the problem! In my case, the error was due to a old version of the package RRPP, which is required by Geomorph. After updating it, Geomorph is working perfectly! Hope this can be useful for you too.
I came across the same error but after updating the RRPP and rgl packages required for geomorph, the gm.prcomp() function worked for me. I hope this helps if you haven't figured it out already.

How can I install and use (mice) function in R?

I want to use mice function to handle the missing data that I have in (data). I installed the package and I called the library. However, when I am trying to apply the function to my data it gives me error as below:
(Error in mice(data[, 5:9], m = 3, seed = 123) :
could not find function "mice")
I have a normal data frame that includes NAs
install.packages('mice')
library(mice)
library(VIM)
md.pattern(data)
md.pairs(data)
My_New_Data <- mice(data[,5:9], m=3, seed=123)
I am expecting the function to solve the problem and replace the NAs with reasonable values. It did not work at all!
Edit (incorporating comment suggestion)
In the comments the running mice::mice(data[, 5:9], m = 3, seed = 123). I ran this and the following error was returned.
Error in get(Info[i, 1], envir = env):
lazy-load database 'C:/Users/MUSTAFA KAMAL/Documents/R/win-library/3.5/broom/R/broom.rdb' is corrupt
In addition:
Warning message: In get(Info[i, 1], envir = env) : internal error -3 in R_decompress1
In order to incorporate an answer to this question, I will rewrite my comment which resolved the problem, in the form of a short answer.
From the comments executing mice::mice(data[, 5:9], m = 3, seed = 123) resulted in an error message, showing the directory ~/Documents/R/win-library/**3.5**/broom/R/broom.rdb being corrupt.
From the corrupted directory path, one can see that OP was running R-3.5.x, while the newest version is R-3.6.x. Some packages updated since the most recent R-update has experienced similar problems, as such a first step towards solving these types of issues is updating R. The installr contains the function updateR which can help smooth over such updates, while also updating any outdated packages.
As a side note, an update sometimes fails to update the actual packages or results in other packages being corrupted, as such if an error persists one solution is to simply delete and re-install the package (or the entire ~/Documents/R/win-library/3.z/ directory). In the question from OP the corrupt package is the broom package, as such one could re-install this package by running
remove.packages("broom")
install.packages("broom")
which should resolve any leftover issues. Note however multiple packages might be corrupt, and likely only one will be shown every time the function is executed. In such cases a full package clear will do the trick, but requires re-installing all packages. For this one can export all installed packages prior to removing them all, by noting that a full list of installed packages is contained in installed.packages(), which can then be exported to a file with for example write.table or write.csv.

How to correct for "Error in nullmodel(comm, method) : could not find function "list2env" in the vegan package

I'm busy exploring the package vegan for R, using it to calculate nestedness of community matrices and null models. I'm particularly interested in using the permat functions as well as Oecosimu.
However, when running my program I obtained the following errors:
Error in nullmodel(comm, method) : could not find function "list2env"
Error in nullmodel(m, ALGO) : could not find function "list2env"
I then even ran an example (given below) of how to use these functions given by the R help function, and even these examples gave the same error. Am I suppose to import something else in order to use these functions or how do I go about fixing this?
Examples:
m <- matrix(c(
1,3,2,0,3,1,
0,2,1,0,2,1,
0,0,1,2,0,3,
0,0,0,1,4,3
), 4, 6, byrow=TRUE)
x1 <- permatswap(m, "quasiswap")
summary(x1)
x2 <- permatfull(m)
summary(x2)
x3 <- permatfull(m, "none", mtype="prab")
x3$orig
summary(x3)
x4 <- permatfull(m, strata=c(1,1,2,2))
summary(x4)
Technically, this is a bug in the development version of Vegan on R-Forge. We were failing to declare a dependency on R versions >= 2.12 in DESCRIPTION. I have checked in the relevant change to the source tree to fix this but it will take a day or so before the tarball and binaries are rebuilt by R-Forge.
That said, you should probably update your R to something more recent. Or use the versions of those functions provided in Vegan 2.0-x on CRAN.
list2env is part of R base, which means it comes with the distribution, not in an add-on package. So if you don't have it you're probably either running an old version of R or have a broken installation. The example worked fine for me, with R 2.12.1 and vegan 2.1-0.
Your code works for me without an error message
The most probable cause of your error is your using old versions of R, vegan or permute
The R news for changes says
CHANGES IN R VERSION 2.12.0: NEW FEATURES:
o New list2env() utility function as an inverse of
as.list(<environment>) and for fast multi-assign() to existing
environment. as.environment() is now generic and uses list2env()
as list method.
CHANGES IN R VERSION 2.12.1: BUG FIXES:
o When list2env() created an environment it was missing a PROTECT
call and so was vulnerable to garbage collection.
CHANGES IN R VERSION 2.13.0: NEW FEATURES:
o list2env(envir = NULL) defaults to hashing (with a suitably sized
environment) for lists of more than 100 elements.
So update your version of R and the packages and try again.

Resources