stargazer() with Zelig regression output - r

I'm working with the R package stargazer. I have a Zelig model that I can't get stargazer to create Latex code for.
logit9.1 <- zelig(winner ~ treatment + count_parties + resp_OECD24 + DirExp + IndExp,
data = outcome, model = "logit" ,robust = TRUE)
stargazer(logit9.1)
Error in if (zelig.object$family$family == "gaussian") { :
argument is of length zero
If I change model="normal", I get the same error message. model="gaussian" is not a supported model in zelig(). The stargazer documentation says that it works with zelig objects.

stargazer v. 2.0 is now available on CRAN. The package has been updated to reflect changes made to Zelig objects, and has a bunch of other new features. Zelig has not always been perfect about backward compatibility of the object it creates, so the old version of stargazer() might have trouble. Perhaps you should try running your code with the latest version, and see if it works.

Related

Issue with GLMM in glmmTMB

I am trying to run the following model:
MP1 <- glmmTMB(Abundance ~ All_predator + Year + Location + Depth +
(1 | Site/Site.Transect),
data = plandat,
family = "poisson")
but am getting this error:
Error in .Call("getParameterOrder", data, parameters, new.env(),
PACKAGE = DLL) :
Incorrect number of arguments (3), expecting 4 for
'getParameterOrder'
Does anyone know what this issue might be?
This is a binary-incompatibility problem as documented here. You should probably have seen a "Package version inconsistency detected" message telling you to re-install glmmTMB from source. Depending on the release sequence of TMB and glmmTMB on CRAN, updating from CRAN as usual might work; otherwise you need to re-install from source (see ?glmmTMB::reinstalling).

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.

Functions can't be found, but the packages are certainly installed and loaded

I'm trying to predict values with a linear mixed model and new data, but I keep getting errors that both functions I'm trying (predict.lme from nlme, and predict.merMod from lme4) do not exist. The packages are installed and loaded.
I first tried using predict.lme (nlme). I note that I've installed and loaded the package, since obviously this is a reason the function may not be found.
But I get this error:
Error in predict.lme(object = PlotModel.best, newdata = PlotInvData_predict,:
could not find function "predict.lme"
I've had random problems with nlme before, so I decide to try predict.merMod (lme4), but I get the same error that that function can't be found. I thought maybe those functions can't handle the interval argument that the regular predict function can. I get rid of that, and it still doesn't work. I tested other functions in those packages, and they work just fine. So something is wrong with my workflow, but I can't figure out what. It's the same workflow as with the regular predict function, which I've used just fine.
What am I doing wrong?
Here's the new data:
PlotInvData_predict <- read.csv(file="D:/ThesisPart2/Data/PlotInvData_predict.csv", header=TRUE, sep=",")
Heres the model:
PlotModel.best <- lmer(d_InvCov ~ TimeSinceDist + UnitArea_ha + (1|MgmtSame)+ (1|LandMgmt.1), PlotInvData)
Then I first tried using predict.lme (nlme). I note that I've installed and loaded the package, since obviously this is a reason the function may not be found.
install.packages("nlme")
library(nlme)
p_bd <- predict.lme(object=PlotModel.best, newdata=PlotInvData_predict, interval="confidence")
Here's the error, again:
Error in predict.lme(object = PlotModel.best, newdata = PlotInvData_predict, :
could not find function "predict.lme"
When using the regular predict function with a different model (linear model), I would get something like this as a result:
fit lwr upr
1 1.098959 0.5803632 1.617556
2 1.156005 0.6627035 1.649306
3 1.213050 0.7408797 1.685220
4 1.270095 0.8143122 1.725879
5 1.327141 0.8824762 1.771805
6 1.384186 0.9449715 1.823401
7 1.441231 1.0015871 1.880875

Mixed Logit Model in Zelig (R) -- not running -- not available anymore?

I am interested in getting first differences from a mixed logit model using the Zelig package. However, I am not able to run a mixed logit model in Zelig. I updated the Zelig package as instructed by the Zelig website.
I ran the mixed logit mode but I got the following error: Error: logit.mixed is not a supported model type.
I tried to run the model from the vignette and got the same error:
library(Zelig)
data(voteincome)
z.out1 <- zelig(vote ~ education + age + female +tag(1 | state), data=voteincome, model="logit.mixed")
I get the following error:
Error: logit.mixed is not a supported model type.
Is "logit.mixed" no longer available on Zelig? I am currently using R version 3.5.1 if that makes a difference.
Replying to this older post in case anyone else is doing this as well. Looks like the instructions here to use the remotes package to download ZeligMultilevel currently work better than devtools::install_github(); I ran into the same dependency problem as the previous user.
Instead, users can use this code, available here:
install.packages("remotes")
remotes::install_github("IQSS/ZeligMultilevel")
Just remember to accept the remotes package's suggestion to update all packages. Then everything works fine.
It seems like logit.mixed is no longer supported by the zelig-package.
It was implemented in Zelig 4: http://zeligdev.github.io/ , but I can not find it in the currently supported models.
There is, however, a deprecated version of a package called ZeligMultilevel up on github available here.
You can try:
devtools::install_github("IQSS/ZeligMultilevel")
and then, followed by the demo, you can:
library(ZeligMultilevel)
data(voteincome)
z5 <- zlogitmixed$new()
z5
z5$zelig(vote ~ education + age + female + (1 | state),
data = voteincome)
z5

R caret: rfe nnet "undefined columns selected"

I am running into problems when applying recursive feature selection to nnet models with caret::rfe; I get the following error message:
Error in { : task 1 failed - "undefined columns selected"
The actual task is more complex than the following example, but I am confident that this is a similar problem:
library(caret)
rfe(x = iris[,1:3],
y = iris[,4]/max(iris[,4]),
sizes = c(2),
method="nnet",
rfeControl = rfeControl(functions = caretFuncs)
)
I know this error can occur when trying to select more features than there are available in x (e.g. see https://stats.stackexchange.com/questions/18362/odd-error-with-caret-function-rfe), but this does not seem to be the problem here. I also ran very similar calls in earlier versions of caret, without this problem occurring.
I use R 3.3.1 and caret 6.0.71.
Thank you very much for your help.
EDIT: I went through the archived versions of caret and found that the example code is working in caret versions <= 6.0.62.
I went through the archived versions of caret and found that the example code is working in caret versions <= 6.0.62. This also solves the problems my original code had. I reported this issue on the caret github.
EDIT: The problem is now fixed : https://github.com/topepo/caret/issues/485

Resources