Original link: http://tecdat.cn/?p=3138
Original source: Tuoduan Data Tribe Public Account
Using Generalized Linear Mixed Models (GLMM) and Linear Mixed Models (LMM) has become easier as software packages have advanced. As we find ourselves using these models more and more in our work, we have developed a suite of R shiny tools to simplify and speed up the common lme4 tasks of interacting with objects.
Related video: Linear Mixed Effects Models (LMM,Linear Mixed Models) and R language implementation
Linear Mixed Effects Model (LMM,Linear Mixed Models) and R language implementation case
Duration 12:13
shiny apps and demos
The easiest way to demonstrate the capabilities of this app is to use the Shiny app, start some metrics here to help explore the model.
On the first tab, the function displays prediction intervals for the data selected by the user. The function quickly computes prediction intervals by sampling from simulated distributions of fixed-effect and random-effects terms and combining these simulated estimates to produce a predicted distribution for each observation.
On the next tab, the distributions of fixed effects and group-level effects are displayed on a confidence interval plot. These are useful for diagnostics and provide a way to examine the relative magnitudes of various parameters.
There are some convenience methods on the third tab that show the effect's impact or extent predictInterval. For each case, up to 12, in the selected data type, the user can view the effect of changing the fixed effects. This allows users to compare effect sizes between variables, as well as effect sizes between models on the same data.
predict
Predictions like this.
predict(m1, newdata #> 1 2 3 4 5 6 7 8 #> 3.146336 3.165211 3.398499 3.114248 3.320686 3.252670 4.180896 3.845218 #> 9 10 #> 3.779336 3.331012
Predict lm and glm:
predInte(m1, newdata = Eval\[1:10, \], n.sims = 500, level = 0.9, #> fit lwr upr #> 1 3.074148 1.112255 4.903116 #> 2 3.243587 1.271725 5.200187 #> 3 3.529055 1.409372 5.304214 #> 4 3.072788 1.079944 5.142912 #> 5 3.395598 1.268169 5.327549 #> 6 3.262092 1.333713 5.304931
The prediction interval is slower because it is a simulation calculation.
visualization
Visually inspect the functionality of an object. The easiest is to get the posterior distributions for the fixed and random effects parameters.
head(Sim) #> term mean median sd #> 1 (Intercept) 3.22673524 3.22793168 0.01798444 #> 2 service1 -0.07331857 -0.07482390 0.01304097 #> 3 lectage.L -0.18419526 -0.18451731 0.01726253 #> 4 lectage.Q 0.02287717 0.02187172 0.01328641 #> 5 lectage.C -0.02282755 -0.02117014 0.01324410
We can draw like this:
pltsim(sim(m1, n.sims = 100), level = 0.9, stat = 'median'
We can also quickly make a plot of random effects:
head(Sims) #> groupFctr groupID term mean median sd #> 1 s 1 (Intercept) 0.15317316 0.11665654 0.3255914 #> 2 s 2 (Intercept) -0.08744824 -0.03964493 0.2940082 #> 3 s 3 (Intercept) 0.29063126 0.30065450 0.2882751 #> 4 s 4 (Intercept) 0.26176515 0.26428522 0.2972536 #> 5 s 5 (Intercept) 0.06069458 0.06518977 0.3105805
plotR((m1, n.sims = 100), stat = 'median', sd = TRUE
Sometimes random effects can be difficult to interpret
Rank(m1, groupFctr = "d") head(ranks) #> d (Intercept) (Intercept)_var ER pctER #> 1 1866 1.2553613 0.012755634 1123.806 100 #> 2 1258 1.1674852 0.034291228 1115.766 99 #> 3 240 1.0933372 0.008761218 1115.090 99 #> 4 79 1.0998653 0.023095979 1112.315 99 #> 5 676 1.0169070 0.026562174 1101.553 98 #> 6 66 0.9568607 0.008602823 1098.049 97
effect simulation
Interpreting the results of LMM and GLMM models is difficult, especially the relative effects of different parameters on the predicted results.
impact(m1, Eval\[7, \], groupFctr = "d", breaks = 5, n.sims = 300, level = 0.9) #> case bin AvgFit AvgFitSE nobs #> 1 1 1 2.787033 2.801368e-04 193 #> 2 1 2 3.260565 5.389196e-05 240 #> 3 1 3 3.561137 5.976653e-05 254 #> 4 1 4 3.840941 6.266748e-05 265 #> 5 1 5 4.235376 1.881360e-04 176
The results show the change in yhat from the first to the fifth quantile in terms of the magnitude of the group factor coefficients according to the newdata we provided.
ggplot(impSim, aes(x = factor(bin), y = AvgFit, ymin = AvgFit - 1.96*AvgFitSE, ymax = AvgFit + 1.96*AvgFitSE)) +
Thank you very much for reading this article, please leave a comment below if you have any questions!
Most Popular Insights
1.lmer mixed linear regression model based on R language
3.R language linear mixed effects model practical case
4.R language linear mixed effects model practical case2
5.R language linear mixed effects model practical case
6.Partially folded Gibbs sampling for Linear Mixed-Effects Models
7.R language LME4 mixed effects model to study the popularity of teachers
8.HAR-RV Model Based on Mixed Data Sampling (MIDAS) Regression in R Language to Predict GDP Growth The regression HAR-RV model predicts GDP growth")
9.Hierarchical Linear Model HLM using SAS, Stata, HLM, R, SPSS and Mplus