Which R package can I use to visualize mixed effect model coefficient - r

I am working on mixed effect model using lmer function in R. My response variable is Productivity which is continues variable and I try to find the effect of 5 predictors on productivity(SR, NRI, CWM_H, and CWM_Chl and FDispersion) all the predictors are continues variables. I want to visualize the model result using coefficient plot in the same way as you can see on the image. (using different colors for positive and negative predictors).The data collected in 2017 and 2018 from 32 plots (sampled from 32 plots) repeated measure, so that I used the four predictors mentioned above as fixed effect and the plot as random effect in the model. Which package I should use to visualize the coefficient? Any help would be greatly appreciated!
Formula I have used
mixed_model<- lmer(Productivity_log ~ SpR + NRI + CWM_Height + CWM_Chlorophyl +
FDispersion + (1 | Plot), Data= datsc,REML=TRUE)
<img src="http://example.com/img.jpg">
<img src="http://file:///C:/Users/Gossaye/Desktop/CWM/png.jpg">

sjPlot::plot_model() does exactly this. A quick demo:
library(lme4)
library(sjPlot)
data(msleep, package = "ggplot2")
mixed_model <- lmer(
sleep_total ~ brainwt + bodywt + factor(vore) + (1 | order),
data = msleep
)
plot_model(mixed_model)

Related

Plotting random effects with lmer and sjPlot

I am currently running a mixed effects model using lmer in which random slopes and correlated random intercepts are estimated. After fitting the model I would like to plot the result allowing from random slopes and intercepts as well as one overall fixed line. How I currently implement is this way:
library(lmer)
library(sjPlot)
df <- read_csv("anonymized_test.csv")
m1 <- lmer("DV ~ IV + (1 + IV| iso)", df)
plot_model(m1, ,type="pred",
terms=c("IV","iso"),
pred.type="re", ci.lvl = NA)
This is the result:
Which is not what is expected as we would expect some negative and positive slopes in addition to the random intercepts according to the extracted random effects of the model
The problem is that sjPlot seems to only plot the random intercepts. Looking at an older vignette of sjPlot this seems to have been implemented in a deprecated function (see here ). The question is how do I get this functionality back? Thanks for any insight.
This is actually straightforward, even without the sjPlot package. We may extract fixef and ranef as fe and re and combine them in a plot. Both, fe and re have intercept and slope and get added together.
library(lme4)
fm1 <- lmer("Reaction ~ Days + (Days | Subject)", sleepstudy)
fe <- fixef(fm1)
re <- ranef(fm1)$Subject
clr <- rainbow(nrow(re)) ## define n colors
par(mfrow=c(1, 2))
plot(Reaction ~ Days, sleepstudy, col=clr[as.numeric(Subject)], main='Pred w/ points')
lapply(seq_len(nrow(re)), \(x) abline(fe[1] + re[x, 1], fe[2] + re[x, 2], col=clr[x]))
plot(Reaction ~ Days, sleepstudy, col=clr[as.numeric(Subject)], main='Pred w/o points', type='n')
lapply(seq_len(nrow(re)), \(x) abline(fe[1] + re[x, 1], fe[2] + re[x, 2], col=clr[x]))
However, I also get the random slopes using sjPlot. Not sure what went wrong, maybe you are using outdated software?
sjPlot::plot_model(fm1, type="pred", terms=c("Days","Subject"), pred.type="re", ci.lvl=NA)
# Warning message:
# In RColorBrewer::brewer.pal(n, pal) :
# n too large, allowed maximum for palette Set1 is 9
# Returning the palette you asked for with that many colors

R + Multilevel Logistic Regression + Group Fixed Effects

In Stata, I know that if I use the following command, I can get the logits for each possible combination between my dependent variable (thkbins) and my two predictor variables (cc & tv):
melogit thkbins cc#tv || school:,
Is there a way to produce a similar output in R? I have been using the glmer command from the lme4 package, and while I can get the output with the interaction term, it isn't exactly what I can produce in Stata.
model1 <- glmer(thkbin ~ cc + tv + cc*tv + (1|school),
data=thkdata, family = binomial, nAGQ = 7)
summary(model1)
I would use clmm from the package ordinal (tutorial here):
model<-clmm(DepVar~IndVar+(1|WithinVar),data=df)
I hope this helps.

PPML package gravity with time fixed effects

I'm trying to include time fixed effects (dummies for years generated with model.matrix) into a PPML regression in R.
Without time fixed effect the regression is:
require(gravity)
my_model <- PPML(y="v", dist="dist",
x=c("land","contig","comlang_ethno",
"smctry","tech","exrate"),
vce_robust=T, data=database)
I've tried to add command fe=c("year") within the PPML function but it doesn't work.
I'd appreciate any help on this.
I would comment on the previous answer but don't have enough reputation. The gravity model in your PPML command specifies v = dist × exp(land + contig + comlang_ethno + smctry + tech + exrate + TimeFE) = exp(log(dist) + land + contig + comlang_ethno + smctry + tech + exrate + TimeFE).
The formula inside of glm should have as its RHS the variables inside the exponential, because it represents the linear predictor produced by the link function (the Poisson default for which is natural log). So in sum, your command should be
glm(v ~ log(dist) + land + contig + comlang_ethno + smctry + tech + exrate + factor(year),
family='quasipoisson')
and in particular, you need to have distance in logs on the RHS (unlike the previous answer).
Just make sure that year is a factor, than you can just use the plain-and-simple glm-function as
glm(y ~ dist + year, family = "quasipoisson")
which gives you the results with year as dummies/fixed effects. The robust SE are then calculated with
lmtest::coeftest(EstimationResults.PPML, vcov=sandwich::vcovHC(model.PPML, "HC1"))
The PPML function does nothing more, it just isn't very flexible.
Alternatively to PPML and glm, you can also solve your problem using the function femlm (from package FENmlm) which deals with fixed-effect estimation for maximum likelihood models.
The two main advantages of function femlm are:
you can add as many fixed-effects as you want, and they are dealt with separately leading to computing times without comparison to glm (especially when fixed-effects contain many categories)
standard-errors can be clustered with intuitive commands
Here's an example regarding your problem (with just two variables and the year fixed-effects):
library(FENmlm)
# (default family is Poisson, 'pipe' separates variables from fixed-effects)
res = femlm(v ~ log(dist) + land | year, base)
summary(res, se = "cluster")
This code estimates the coefficients of variables log(dist) and land with year fixed-effects; then it displays the coefficients table with clustered standard-errors (w.r.t. year) for the two variables.
Going beyond your initial question, now assume you have a more complex case with three fixed-effects: country_i, country_j and year. You'd write:
res = femlm(v ~ log(dist) + land | country_i + country_j + year, base)
You can then easily play around with clustered standard-errors:
# Cluster w.r.t. country_i (default is first cluster encountered):
summary(res, se = "cluster")
summary(res, se = "cluster", cluster = "year") # cluster w.r.t. year cluster
# Two-way clustering:
summary(res, se = "twoway") # two-way clustering w.r.t. country_i & country_j
# two way clustering w.r.t. country_i & year:
summary(res, se = "twoway", cluster = c("country_i", "year"))
For more information on the package, the vignette can be found at https://cran.r-project.org/web/packages/FENmlm/vignettes/FENmlm.html.

R lme4 Plot lmer residuals ~ fitted by Factors levels in ggplot

I would like to reproduce lmer diagnostic plots in ggplot2. Particularly, I know that for a lmer model DV ~ Factor1 * Factor2 + (1|SubjID) I can simply call plot(model, resid(.)~fitted(.)|Factor1+Factor2) to generate a lattice-based Residuals Vs. Fitted plot, faceted for each Factor1+Factor 2 combination.
I would like to generate the same plot, but using ggplot2. I tried using qplot(resid(model), fitted(model)) and different variations of that with different arguments, but the information about the factors needed for faceting is not coming through with this (and similar) calls.
I'd appreciate any advise to achieve this, thanks!
EDIT
The core of my question: given any lmer model, how can I create a data-frame including fitted and residual values AND the Factor information for each value? something like:
Factor1 Factor2 Fitted Resid
0 0 987 654
0 0 123 456
(...)
I could not figure that out from lmer documentation on the resid() and fitted() functions
Here is an example of what we want, a reproducible example
data(Orthodont, package="nlme")
Orthodont$age <- as.factor(Orthodont$age)
model <- lmer(distance ~ age * Sex + (1|Subject), Orthodont)
plot(model, resid(.) ~ fitted(.) | age + Sex )
answer
ggplot(model, aes(.fitted, .resid)) + geom_point() +
facet_wrap(~ Sex + age, ncol = 4) # (edited) I noticed fortify(model) isn't necessary.

Plot Effects of Variables in Interaction Terms

I would like to plot the effects of variables in interaction terms, using panel data and a FE model.
I have various interaction effects in my equation, for example this one here:
FIXED1 <- plm(GDPPCgrowth ~ FDI * PRIVCR, data = dfp)
I can only find solutions for lm, but not for plm.
So on the x-axis there should be PRIVCR and on the y-axis the effect of FDI on growth.
Thank you for your help!
Lisa
I am not aware of a package that supports plm objects directly. As you are asking for FE models, you can just take an LSDV approach for FE and do the estimation by lm to get an lm object which works with the effects package. Here is an example for the Grunfeld data:
library(plm)
library(effects)
data("Grunfeld", package = "plm")
mod_fe <- plm(inv ~ value + capital + value:capital, data = Grunfeld, model = "within")
Grunfeld[ , "firm"] <- factor(Grunfeld[ , "firm"]) # needs to be factor in the data NOT in the formula [required by package effects]
mod_lsdv <- lm(inv ~ value + capital + value:capital + firm, data = Grunfeld)
coefficients(mod_fe) # estimates are the same
coefficients(mod_lsdv) # estimates are the same
eff_obj <- effects::Effect(c("value", "capital"), mod_lsdv)
plot(eff_obj)

Resources