How to solve glmmTMB object not found error? - r

It seems a bit silly to post this as a question, but I haven't been able to find an answer for this.
I want to use the glmmTMB package for modelling data with zero-inflation, but when I try to fit the model I want, the function glmmTMB() returns the following error:
Error in glmmTMB(y ~ x + (1 | z), data = df, ziformula = x) : object 'x' not found
This only happens after I introduce the term for ziformula. The model without that term works just fine. For some reason, it seems that the ziformula overrides the data command in the function.
My glmmTMB package version is 1.1.3 and my R version is 4.1.1.
Here's a little sample data to reproduce the problem:
df <- data.frame(y=c(2,2,5,7,3,8,3,2,4,6),
x = c(23,21.7,22.5,18,20,19.9,21,20.3,20,20.5),
z = c(rep("a",5),rep("b",5)))
glmmTMB(y ~ x + (1|z), data = df, ziformula = x)
If anyone has had any similar issue, I would be grateful for any tips on how to solve it!

Related

Huber-White robust standard errors for a GLMM - R

I have discovered some heteroscedasticity in my model that I would like to compensate for with more robust standard errors. I have tried to use the Huber-White robust standard errors from the merDeriv package in R but I beleive these only work for a GLMM with a binomial distribution. Is there a way I could achieve the same thing for a Negative Binomial distribition?
Model:
library(lme4)
model <- glmer.nb(Jobs ~ 1 + Month + Year + (1|Region), data = df)
Huber-White robust standard errors:
library(merDeriv)
bread.glmerMod(model)
Error:
Error in vcov.lmerMod(object, full = full) : estfun.lmerMod() only works for lmer() models.
Thank you for any help!
This looks like a bug in the package, as far as I can tell (the bread.glmerMod function was calling estfun.lmerMod rather than estfun.glmerMod; there's a broader question here about the design of the generic functions, but never mind ...)
You should be able to install a fixed version from my fork via remotes::install_github("bbolker/merDeriv"), then reload the package and try again.
Alternately, download the tarball, change vcov.lmerMod to vcov.glmerMod in the last line of R/bread.glmerMod.R, and re-install the package ...
Try something like this:
library(lme4)
model <- glmer.nb(Jobs ~ 1 + Month + Year + (1|Region), data = df)
cov <- vcovHC(model, type = "HC1", sandwich = T)
se <- sqrt(diag(cov_m1))
(Can't confirm if it works since this there isn't a reproducible example)

Extracting path coefficients of piecewise SEM (structural equation model)

I'm constructing a piecewise structural equation model using the piecewiseSEM package in R (Lefcheck - https://cran.r-project.org/web/packages/piecewiseSEM/vignettes/piecewiseSEM.html)
I already created the model set and I could evaluate the model fit, so the model itself works. Also, the data fits the model (p = 0.528).
But I do not succeed in extracting the path coefficients.
This is the error i get: Error in cbind(Xlarge, Xsmall) : number of rows of matrices must match (see arg 2)
I already tried (but this did not work):
standardising my data because of the warning: Some predictor variables are on very different scales: consider rescaling
adapted my data (threw some NA values away)
This is my modellist:
predatielijst = list(
lmer(plantgrootte ~ gapfraction + olsen_P + (1|plot_ID), data = d),
glmer(piek1 ~ gapfraction + olsen_P + plantgrootte + (1|plot_ID),
family = poisson, data = d),
glmer(predatie ~ piek1 + (1|plot_ID), family = binomial, data = d)
)
with "predatie" being a binary variable (yes or no) and all the rest continuous variables (gapfraction, plantgrootte, olsen_P & piek1)
Thanks in advance!
Try installing the development version:
library(devtools)
install_github("jslefche/piecewiseSEM#2.0")
Replace list with psem and run the coefs or summary function. It will likely get rid of your error. If not, open a bug on Github!
WARNING: this will overwrite your current version from CRAN. You will need to reinstall from CRAN to get version 1.4 back.
try to use lme (out of the nlme library) ilstead of glmer. As far as I understand, the fact that lmer does not provide p-values (while lme does) seems to be the problem here.
Hope this works.

Error in using by in R

I'm new to R and started using it to implement a gam model. I've been following this tutorial examples and R keeps throwing an error when I try to use by. I really have no idea what's wrong and would appreciate it if anybody can suggest something.
Thanks a lot in advance.
>require(mgcv)
>set.seed(10)
## simulate date from y = f(x2)*x1 + error
>dat <- gamSim(3,n=400)
Continuous `by' variable example
>b<-gam(y ~ s(x2,by=x1),data=dat)
Error in s(x2, by = x1) : unused argument (by = x1)
Additional information: R version = 3.3.1 (2013-06-21)
OS = Windows 10
Code
require(mgcv)
set.seed(10)
## simulate date from y = f(x2)*x1 + error
dat <- gamSim(3,n=400)
b <- gam(y ~ s(x2,by=x1),data=dat)
b
Result
Continuous `by' variable example
Family: gaussian Link function: identity
Formula: y ~ s(x2, by = x1)
Estimated degrees of freedom:
9.2 total = 10.2
GCV score: 4.518245
Please start a fresh R session and see if you get the same error. Also, please check your version of mgcv and make sure it's up to date.

Resolving symmetry in gnls model

I'm trying to fit a logistic growth model in R, using gnls in the nlme package.
I have previously successfully fit a model:
mod1 <- gnls(Weight ~ I(A/(1+exp(b + v0*Age + v1*Sum.T))),
data = df,
start = c(A= 13.157132, b= 3, v0= 0.16, v1= -0.0059),
na.action=na.omit)
However, I now wish to constrain b so that it is not fitted by the model, so have tried fitting a second model:
mod2 <- gnls(Weight ~ I(A/(1+exp(log((A/1.022)-1) + v0*Age + v1*Sum.T))),
data = df,
start = c(A= 13.157132, v0= 0.16, v1= -0.0059),
na.action=na.omit)
This model returned the error:
Error in gnls(Weight ~ A/(1 + exp(log((A/1.022) - 1) + v0 * Age + :
approximate covariance matrix for parameter estimates not of full rank
Warning messages:
1: In log((A/1.022) - 1) : NaNs produced
Searching the error suggests that the problem is caused by symmetry in the model, and solutions to specific questions involve adapting the formula with different parameters. Unfortunately, my statistical knowledge is not good enough to a) fully understand the problem or b) adapt the formula myself.
As for the warning messages (there were 15 in all, all the same) I can't see why they arise because this section of the model works alone (with example numbers).
Any help with any of these queries would be greatly appreciated.
It may be informative for users to know that I finally solved this with what turned out to be a simple solution (with help from a friend).
Since exp(a+b) = exp(a)*exp(b), the equation can be rewritten:
Weight ~ I(A/(1+((A/1.022)-1) * exp(v0*Age + v1*Sum.T))
Which fits without any problems. In general, writing the equation in a different form would seem to be the answer.

coding multivariate response in R Party Package

I am looking to do multivariate prediction using the party package in R (Party package documentation below)
http://cran.r-project.org/web/packages/party/party.pdf
I however, cannot figure out how to do multivariate prediction (multiple response variables). It says that it can do it, and I try this:
f <-cbind(A,B,C~shopping_pt+n_A_0)
model_1 <- ctree(f, data= train)
But that produces the following error:
Error in [<-(tmp, nas, drop = FALSE, value = 0) : (subscript) logical subscript too long
The documentation says it supports multivariate... but doesn't suggest how one can write the syntax correctly, any ideas?
Use the following syntax:
ctree(A + B + C ~ shopping_pt + n_A_0, data=train)
Try the package partykit
Check the sample below where it shows how you can code the multivariate response for the Conditional Inference Tree
### multivariate responses
airq2 <- ctree(Ozone + Temp ~ ., data = airq)
airq2
plot(airq2)

Resources