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
Related
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
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
I am trying to run a R program on cluster. In the R program,jags function is called from the package R2jags. If I don't use the cluster and simply use R, then the program works fine. However, when I try to submit a job, then I get the following error. If I don't call the function jags and use the cluster then it works just fine.
Loading required package: rjags
Loading required package: coda
Linked to JAGS 4.0.0
Loaded modules: basemod,bugs
Attaching package: ‘R2jags’
The following object is masked from ‘package:coda’:
traceplot
*** caught illegal operation ***
address 0x7fe566be8917, cause 'illegal operand'
Traceback:
1: dyn.load(file)
2: load.module(jags.module[m])
3: jags(model.file = "myfile.txt", data = model.data, inits = model.initial.values, parameters = model.parameters, n.chains = 1, n.iter = 500, n.burnin = 5, n.thin = 5)
An irrecoverable exception occurred. R is aborting now ...
line 15: 34161 Illegal instruction (core dumped) Rscript test1.R
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
I am using the "BMA" package in R 3.1.0, and get an error when running one of the functions in the package, iBMA.glm. When running the example in the package documentation:
## Not run:
############ iBMA.glm
library("MASS")
library("BMA")
data(birthwt)
y<- birthwt$lo
x<- data.frame(birthwt[,-1])
x$race<- as.factor(x$race)
x$ht<- (x$ht>=1)+0
x<- x[,-9]
x$smoke <- as.factor(x$smoke)
x$ptl<- as.factor(x$ptl)
x$ht <- as.factor(x$ht)
x$ui <- as.factor(x$ui)
### add 41 columns of noise
noise<- matrix(rnorm(41*nrow(x)), ncol=41)
colnames(noise)<- paste('noise', 1:41, sep='')
x<- cbind(x, noise)
iBMA.glm.out<- iBMA.glm( x, y, glm.family="binomial",
factor.type=FALSE, verbose = TRUE,
thresProbne0 = 5 )
summary(iBMA.glm.out)
I get the error:
Error in registerNames(names, package, ".__global__", add) :
The namespace for package "BMA" is locked; no changes in the global variables list may be made.
I get the error in RStudio running R 3.1.0 on Ubuntu.
on Windows 7, from RStudio and the R console I get a similar error:
Error in utils::globalVariables(c("nastyHack_glm.family", "nastyHack_x.df")) :
The namespace for package "BMA" is locked; no changes in the global variables list may be made.
I also get the same error when running my own data in the function. I'm not clear on what this error means and how to work around the error to be actually able to use the function. Any advice would be appreciated!