I need help in implementing a glmm model using Laplace approximation - r

The model I am trying to implement using laplace approximation i s below:
model2<-lmer(ChickWeight$weight~Time + Diet + Time*Diet + (1+Time|Chick), data = ChickWeight)
summary(model2)
I was trying to use the code below to do this but i am not sure:
#Laplace approximation
library(glmmsr)
mod_Laplace <- glmm(ChickWeight$weight~Time + Diet + Time*Diet + (1+Time|Chick), data = ChickWeight,
family = gaussian, method = "Laplace")
I am getting an error as the output

Related

How can I calculate marginal effects from a binomial logistic model with clustered standard errors in R studio?

I would like to calculate marginal effects for this logistic model with clustered standard errors which I computed with miceadds::glm.cluster.
fullmodel3 <- miceadds::glm.cluster(data = SDdataset17,
formula = stigmatisation_dummy_num ~ gender + age +
agesquared + education_new + publicsector +
retired + socialisation_sd + selfplacement_num +
years_membership + voteshare,
cluster = "voteshare", family = "binomial")
Given that I am not using glm(), most functions I have seen around do not work.
Any suggestions?

Random effects specification in gamlss in R

I would like to use the gamlss package for fitting a model benefiting from more available distributions in that package. However, I am struggling to correctly specify my random effects or at least I think there is a mistake because if I compare the output of a lmer model with Gaussian distribution and the gamlss model with Gaussian distribution output differs. If comparing a lm model without the random effects and a gamlss model with Gaussian distribution and without random effects output is similar.
I unfortunately cannot share my data to reproduce it.
Here my code:
df <- subset.data.frame(GFW_food_agg, GFW_food_agg$fourC_area_perc < 200, select = c("ISO3", "Year", "Forest_loss_annual_perc_boxcox", "fourC_area_perc", "Pop_Dens_km2", "Pop_Growth_perc", "GDP_Capita_current_USD", "GDP_Capita_growth_perc",
"GDP_AgrForFis_percGDP", "Gini_2008_2018", "Arable_land_perc", "Forest_loss_annual_perc_previous_year", "Forest_extent_2000_perc"))
fourC <- lmer(Forest_loss_annual_perc_boxcox ~ fourC_area_perc + Pop_Dens_km2 + Pop_Growth_perc + GDP_Capita_current_USD +
GDP_Capita_growth_perc + GDP_AgrForFis_percGDP + Gini_2008_2018 + Arable_land_perc + Forest_extent_2000_perc + (1|ISO3) + (1|Year),
data = df)
summary(fourC)
resid_panel(fourC)
df <- subset.data.frame(GFW_food_agg, GFW_food_agg$fourC_area_perc < 200, select = c("ISO3", "Year", "Forest_loss_annual_perc_boxcox", "fourC_area_perc", "Pop_Dens_km2", "Pop_Growth_perc", "GDP_Capita_current_USD", "GDP_Capita_growth_perc",
"GDP_AgrForFis_percGDP", "Gini_2008_2018", "Arable_land_perc", "Forest_loss_annual_perc_previous_year", "Forest_extent_2000_perc"))
df <- na.omit(df)
df$ISO3 <- as.factor(df$ISO3)
df$Year <- as.factor(df$Year)
fourC <- gamlss(Forest_loss_annual_perc_boxcox ~ fourC_area_perc + Pop_Dens_km2 + Pop_Growth_perc + GDP_Capita_current_USD +
GDP_Capita_growth_perc + GDP_AgrForFis_percGDP + Gini_2008_2018 + Arable_land_perc + Forest_extent_2000_perc + random(ISO3) + random(Year),
data = df, family = NO, control = gamlss.control(n.cyc = 200))
summary(fourC)
plot(fourC)
How do the random effects need to be specified in gamlss to be similar to the random effects in lmer?
If I specify the random effects instead using
re(random = ~1|ISO3) + re(random = ~1|Year)
I get the following error:
Error in model.frame.default(formula = Forest_loss_annual_perc_boxcox ~ :
variable lengths differ (found for 're(random = ~1 | ISO3)')
I found the +re(random=~1|x) specification to work fairly well with my GAMLSS. Have you double check that the NA's are being removed from your dataset? Sometimes na.omit does not work properly.
Have a look at this thread that has the same error than yours, but in a GAM. You can try that code to remove your NA's
Error in model.frame.default: variable lengths differ

Confidence intervals for the predicted probabilities from glmer object, error with bootMer

I need to calculate 95% confidence intervals or predicted probabilities from a logistic mixed effects model, created using the glmer function from lme4 R package. The model includes a stabilized probability weighting to correct for the selecttion bias on the analized data.
I've read that bootMer function (lme4 package) perform a Model-based semi-parametric bootstraping that makes staighforward to get the CI's as the quantiles of the distribution (quantile approach).
Nevertheless, when I apply the function bootMer, the following error is generated:
"Error in sfun(object, nsim = 1, ftd = rep_len(musim, n * nsim), wts =
weights): cannot simulate from non-integer prior.weights"
I must use a non-integer weights, so my question is ¿How can I solve this problem using bootMer function? Or if it's impossible, ¿Are anny alternatives?
#The model
M1s = glmer(plab ~ 1 + edad2_c + I(edad2_c^2) + periodo_c + cohorte + nocu_c + tipoocu2 + sector + educ + benef + genero + ecivil + area + generojh + edadjh2_c + nhogar_c + nhogar05_c + nhogar0614_c + nhogar66_c + (1 | periodo_c), weights = ipw,
data = seriecasen,family = binomial(link=logit),nAGQ = 10,glmerControl(optimizer = "bobyqa", optCtrl = list(maxfun = 2e5)))
#Model-based semi-parametric bootstrap for mixed models - CI's predicted probabilites
merBoot <- bootMer(M1s, predict, nsim = 1000, use.u = TRUE, type = c("parametric"), seed = 1959)
CI.lower = apply(merBoot$t, 2, function(x) as.numeric(quantile(x, probs=.025, na.rm=TRUE)))
CI.upper = apply(merBoot$t, 2, function(x) as.numeric(quantile(x, probs=.975, na.rm=TRUE)))
Error in sfun(object, nsim = 1, ftd = rep_len(musim, n * nsim), wts =
weights): cannot simulate from non-integer prior.weights
An alternative is the std_beta() function from the sjstats package. It's difficult to test on your model without your data, but I've performed this function on my own logistic regression and it seems to provide your standardized beta, along with the confidence interval(s). The following code should likely work:
sjstats::std_beta(M1s)
Here is the link to the function: std_beta

Nonlinear mixed model without random effects structure specification

I would like to fit a nonlinear model just with the fixed structure specification using nlme R package.
model <- nlme(y ~ Asym/(1+exp((xmid-x)/scal)),
data = data,
fixed = list(Asym + xmid + scal ~ treatment))
#random = Asym ~ 1|subject)
However I am getting the following error:
Error in parse(text = paste("~", paste(nVal, collapse = "/"))) :
<text>:2:0: unexpected end of input
1: ~
^
Is there a way to circunvent this issue? Any advice is more than welcome.
I believe you want the gnls() function (also from the nlme package) with the params= argument rather than fixed=. Try this:
model <- gnls(y ~ Asym/(1+exp((xmid-x)/scal)),
data = data,
params = list(Asym + xmid + scal ~ treatment),
start= ...)
FWIW, if you're really fitting a logistic (and this isn't just a simplified example of what you want to do), fitting might be faster/more robust with the SSlogis() self-starting function in place of your explicit formula ...

Multilevel Imputation: glmer and HMI

Trying to fit a two-level imputation model with HMI (hierch. multiple imputation)...
The model I'm using is this (I want random intercept ONLY):
glmer(pica_yn ~ 1 + visit_c+visit_c2 + geo_child + hhloc + diar_c + hemo_c + (1|pid))
I keep getting this error:
Error in buildZ(rmodel.terms[r], data = data, nginverse =
names(ginverse)): object id not found
It seems as though HMI prefers the specified formula also has a random slope.
Has anyone fit a multilevel imputation model for a BINARY response?
Here is an example you can run that will get the same error:
data("sleepstudy", package="lme4")
sleepstudy[sample(1:nrow(sleepstudy), size = 20), "Reaction"] <- NA
sleep_formula<-Reaction ~ Days + (1|Subject)
hmi_imp <- hmi(data = sleepstudy, model_formula = sleep_formula, M = 5, maxit = 1)

Resources