error in gamma link specification for lmer - r

I'm trying to fit a mixed-effects model with a gamma distribution. The most basic model has one fixed predictor and 1 random effect. No matter which link I specify (I've tried log, identity and inverse), I obtain the following error. My real data has zeros in Y, but even when I apply simulated data with only positive Y as below, it throws the same error.
mockdf = data.frame(y = rnorm(100,77,6.5), x1 = sample(letters,100,replace = T), x2 = seq(1900,1999,1))
mod = lmer(y ~ (1|x1) + x2, family = gamma(link = 'identity'), na.action = na.exclude, data = mockdf)
Error in gamma(link = "identity") :
supplied argument name 'link' does not match 'x'
I searched through SO and couldn't find another person who ran into this error. Is my syntax incorrect?
Thanks for your help.

Related

Trouble in GAM model in R software

I am trying to run the following code on R:
m <- gam(Flp_pop ~ s(Flp_CO, bs = "cr", k = 30), data = data, family = poisson, method = "REML")
My dataset is like this:
enter image description here
But when I try to execute, I get this error message:
"Error in if (abs(old.score - score) > score.scale * conv.tol) { :
missing value where TRUE/FALSE needed
In addition: There were 50 or more warnings (use warnings() to see the first 50)"
I am very new to R, maybe it is a very basic question. But does anyone know why this is happening?
Thanks!
The Poisson distribution has support on the non-negative integers and you are passing a continuous variable as the response. Here's an example with simulated data
library("mgcv")
library("gratia")
library("dplyr")
df <- data_sim("eg1", seed = 2) %>% # simulate Gaussian response
mutate(yabs = abs(y)) # make y non negative
mp <- gam(yabs ~ s(x2, bs = "cr"), data = df,
family = poisson, method = "REML")
# fails
which reproduces the error you saw
Error in if (abs(old.score - score) > score.scale * conv.tol) { :
missing value where TRUE/FALSE needed
In addition: There were 50 or more warnings (use warnings() to see the first 50)
The warnings are of the form:
$> warnings()[1]
Warning message:
In dpois(y, y, log = TRUE) : non-integer x = 7.384012
Indicating the problem; the model is evaluating the probability mass for your response data given the estimated model and you're evaluating this at the indicated non-integer value, which returns a 0 mass plus the warning.
If we'd passed the original Gaussian variable as the response, which includes negative values, the function would have errored out earlier:
mp <- gam(y ~ s(x2, bs = "cr"), data = df,
family = poisson, method = "REML")
which raises this error:
r$> mp <- gam(y ~ s(x2, bs = "cr"), data = df,
family = poisson, method = "REML")
Error in eval(family$initialize) :
negative values not allowed for the 'Poisson' family
An immediate but not necessarily advisable solution is just to use the quasipoisson family
mq <- gam(yabs ~ s(x2, bs = "cr"), data = df,
family = quasipoisson, method = "REML")
which uses the same mean variance relationship as the Poisson distribution but not the actual distribution so we can get away with abusing it.
Better would be to ask yourself why you were trying to fit a model that is ostensibly for counts to a response that is a continuous (non-negative) variable?
If the answer is you had a count but then normalised it in some way (say by dividing by some measure of effort like area surveyed or length of observation time) then you should use an offset of the form + offset(log(effort_var)) added to the model formula, and use the original non-normalised integer variable as the response.
If you really have a continuous response and the poisson was an over sight, try fitting with family = Gamma(link = "log")) or family = tw().
If it's something else, you should edit your question to include that info and perhaps we here can help or the question could be migrated to CrossValidated if the issue is more statistical in nature.

Error when adjusting a GLM: Error in eval(family$initialize)

I am trying to adjust a generalized linear model defined below:
It must be noted that the response variable Var1, as well as the regressor variable Var2, have zero values, for which a constant has been added to avoid problems when applying the log.
model = glm(Var1+2 ~ log(Var2+2) + offset(log(Var3/Var4)),
family = gaussian(link = "log"), data = data2)
However, I am facing an error when performing the graph for the diagnostic analysis using the hnp function, which is expressed by:
library(hnp)
hnp(model)
Gaussian model (glm object)
Error in eval(family$initialize) :
cannot find valid starting values: please specify some
In order to get around the situation, I tried to perform the manual implementation to then carry out the construction of the graph, however, the error message is still present.
dfun <- function(obj) resid(obj)
sfun <- function(n, obj) simulate(obj)[[1]]
ffun <- function(resp) glm(resp ~ log(Var2+2) + offset(log(Var3/Var4)),
family = gaussian(link = "log"), data = data2)
hnp(model, newclass = TRUE, diagfun = dfun, simfun = sfun, fitfun = ffun)
Error in eval(family$initialize) :
cannot find valid starting values: please specify some
Some guidelines in which I found information to try to solve the problem were used, such as considering initial values to initialize the estimation algorithm both in the linear predictor, as well as for the means, however, these were not enough to solve the problem, see below the computational routine:
fit = lm(Var1+2 ~ log(Var2+2) + offset(log(Var3/Var4)), data=data2)
coefficients(fit)
(Intercept) log(Var2+2)
32.961103 -8.283306
model = glm(Var1+2 ~ log(Var2+2) + offset(log(Var3/Var4)),
family = gaussian(link = "log"), start = c(32.96, -8.28), data = data2)
hnp(model)
Error in eval(family$initialize) :
cannot find valid starting values: please specify some
See that the error persists even when trying to manually implement the half-normal plot.
dfun <- function(obj) resid(obj)
sfun <- function(n, obj) simulate(obj)[[1]]
ffun <- function(resp) glm(resp ~ log(Var2+2) + offset(log(Var3/Var4)),
family = gaussian(link = "log"), data = data2, start = c(32.96, -8.28))
hnp(model, newclass = TRUE, diagfun = dfun, simfun = sfun, fitfun = ffun)
Error in eval(family$initialize) :
cannot find valid starting values: please specify some
I also tried to readjust the model by removing the zeros from the database, however, I didn't get any solution to the problem, that is, it still persists.
I suspect what you meant to fit is a log transformed response variable against your predictors. You can more detail about the difference between a log link glm and a log transformed response variable. Essentially when you use a log link, you are assuming the errors are on the exponential scale. I am not so familiar with hnp but my guess it there are problems simulating the response variable.
If I run your regression like this using the data provided, it looks ok
data2$Y = with(data2, log( (Var1+2)/Var3/Var4))
model = glm(Y ~ log(Var2+2), data = data2)
hnp(model)

How to Fit Conway–Maxwell-Poisson regression in R?

I want to fit Conway–Maxwell-Poisson regression with one response and two randomly generated covariates in R, How I Fit?
library(COMPoissonReg)
n1=200
x1 = rnorm(n1,0,1)
x2 = rnorm(n1,0,1)
b0=0.05; b1=0.0025;b2=0.005;b7=0.0001
y=b0+(b1*x1)+(b2*x2)
y1=exp(y)
nu=exp(y)
y2=rcmp(n1, y1,nu)
model = glm.cmp(y1 ~ x1+x2,formula.nu=x1+x2)
I found the following error
Error in formula.default(object, env = baseenv()) : invalid formula
so guide me I Fit this model?
For your data above, you did not simulate the overdispersion parameter so you can don't specify the formula for nu. This will work, but your dependent variable is not an integer, so it throws an error:
glm.cmp(y1 ~ x1+x2)
You can simulate data like this, below comes with an over dispersion so you can see that in the estimate for nu :
n1=200
x1 = rnorm(n1,0,1)
x2 = rnorm(n1,0,1)
b0=0.05; b1=3;b2=2
mu=exp(b0+(b1*x1)+(b2*x2))
y = rnbinom(length(mu),mu=mu,size=1)
glm.cmp(y ~ x1+x2,data=df)

Issues with logit regression in r

I am trying to run a logit regression and I tried two approaches:
m.logit <- glm(p4 ~ scale(log(gdp,orthodox,swb)),
data = happiness,
family = binomial("logit"))
summary(m.logit)
Throws: Error in summary(m.logit) : object 'm.logit' not found
While
m1.logit <- glm(p4 ~ gdp + orthodox + swb, family = binomial(link = "logit"), data = happiness)
Throws: Error in eval(family$initialize) : y values must be 0 <= y <= 1
I kind of understood the errors (in the former case m.logit is not found, and in the latter, I need to transform the variables I think...) but don't know how to solve it...
Any help?

glmulti with a fractional logit model

I want to fit a fractional logit model with the command:
glmfit <- glm(tr1 ~ period + male + stib+ income,
family = quasibinomial(link = "logit"), data=mydata)
Where tr1 is a variable that lies between zero and one (including some zeros).
I now want to choose the model with the smallest QAIC value (i.e. testing possible combinations of the independent variables and checking the resulting QAIC values). To do that, I tried to apply the glmulti command in R:
require("glmulti")
glmulti.out <- glmulti(tr1 ~ period + male + stib+ income,
data = mydata,crit = "qaic",
confsetsize = 5, fitfunction = "glm",
family = quasibinomial(link = "logit"))
However, I constantly get the following error and I can't see why:
Error in lesCrit[sel] = cricri : replacement has length zero
Does anyone know how I could overcome this problem?
For me this worked:
library(bbmle)
qaicmod = function (fit) qAIC(fit, dispersion=with(fit,sum((weights * residuals^2)[weights > 0])/df.residual) )
glmulti.out <- glmulti(tr1 ~ period + male + stib+ income,
data = mydata,crit = "qaicmod",
confsetsize = 5, fitfunction = "glm",
family = binomial(link = "logit"))
This uses a regular binomial GLM but calculates the QAIC based on the estimated dispersion coefficient.
In the dispersion argument of the qaicmod function you could also put the estimated dispersion coefficient of a full quasibinomial GLM with all variables included (some statisticions I have seen recommend this), i.e. to use instead
disp <<- summary(fullmodel)$dispersion
qaicmod = function (fit) qAIC(fit, dispersion=disp)
Finally, I also tried using
library(MuMIn)
x.quasibinomial <<- function(...) {
res <- quasibinomial(...)
res$aic <- binomial(...)$aic
res
}
qaicmod <<- function (fit) QAIC(update(fit, family = x.quasibinomial), chat = deviance(fit) / df.residual(fit))
glmulti.out <- glmulti(tr1 ~ period + male + stib+ income,
data = mydata,crit = "qaicmod",
confsetsize = 5, fitfunction = "glm",
family = binomial(link = "logit"))
but that returns the error "Error in eval(expr, envir, enclos) : could not find function "fitfunc"" - not sure how I can fix that...
(The idea would be that this solution would properly refit the model as a quasibinomial GLM and then return the QAIC from that)
First solution above should be OK though I think...

Resources