Original link:http://tecdat.cn/?p=23050

Original source:Extension end number according to the tribal public number

In this article, we will use R language to fit the data to a linear mixed effects model, and then visualize your results.

Linear mixed-effect models are used when there are random effects, which occur when multiple measurements are made of randomly sampled units. Measurements from the same natural group are not in themselves independent random samples. Therefore, these units or groups are assumed to be randomly selected from the “population” of a group. Example situations include

  • When you break it up and experiment with the parts individually (with the set).
  • When your sampling design is nested, such as a quadrant within a cross section; A cross section within a woodland; Woodland within the area (cross section, woodland and area are all associated with the unit).
  • When you measure the relative individuals (the family is the group).
  • When you repeatedly measure the subject (the subject is random).

The linear model of the mixed effects is implemented in the R commands lme4 and lmerTest packages. Another option is to use the LME method in the NMLE package. The method used in LME4 to calculate the approximate degree of freedom is more accurate than the method in NMLE package, especially when the sample size is small.


Measure patch length

The first data set was extracted from a paper by Griffith and Sheldon (Ethology 61:987-993, 2001), who measured the white forehead spots of 30 leading male flycatchers on the Swedish island of Gotland over a two-year period. The plaque is important in attracting a mate, but its size varies from year to year. Our goal here is to estimate plaque length (mm).

Read and check data

  1. Read data from a file.
  2. Look at the first few lines of data to see if they are read correctly.
  3. Create a pair of measurements showing each bird in the two-year study. You can try to make a lattice diagram. Is there any evidence of measurement variability between years?

A linear mixed effects model was constructed

  1. A linear mixed-effect model was applied to the data, and a single bird was regarded as a random group. Note: Two measurements of each bird were made in the consecutive years of the study. For simplicity, the year is not included in the model. Convert it to a character or factor in R so that it is not treated as a numeric variable. Reproducibility is recalculated using this model as described in steps (2) and (3) below. How does the repetitive interpretation change?
  2. Extract parameter estimates (coefficients) from the saved LMER object. Check the output of random effects. What are the two sources of random variation? What is a fixed effect?
  3. In the output, check the standard deviation of the random effect. There should be two standard deviations: one is “(intercept)” and one is “residual”. This is because the mixed-effect model has two sources of random variation: differences in repeated measurements within birds and true differences in frontal patch length between birds. Which of these two sources corresponds to the (intercept) and which corresponds to the residuals?
  4. The output of the fixed effect results is also examined. The only fixed effect in the model formula is the average of all length measurements. It is called “(intercept)”, but not to be confused with the intercept of random effects. The fixed effect output gives you an estimate of the mean and the standard error for that estimate. Note how the fixed effects output provides an estimate of the mean, while the random effects output provides an estimate of the variance (or standard deviation).
  5. Variance components were extracted from the fitting model to estimate the reproducibility of patch length for each year *.
  6. Explain the repeatability measurements obtained in the previous step. If you get a repeatability of less than 1.0, what is the source of variation between the measurements within the individual. Is it just measurement error?
  7. Generates a graph of residuals and fitted values. Notice what’s wrong with that? There seems to be a slight positive trend. This is not an error, but the result of a “contraction” of the best linear unbiased predictors (BLUPS).

Analysis steps

Read and check the data.

head(fly)

Chart (patch ~ bird)

# But a better way to display pairs of data is to display pairs of interaction plots (res=patch, x = year)

Plot (y = patch, x = factor(year), theme_classic)

Fit into a linear mixed effects model. The summary() output shows two sources of random variation: variation between a single bird (bird intercept) and variation between repeated measurements of the same bird (residuals). Each source has an estimated variance and standard deviation. The fixed effect is just an average of all birds – another “intercept”.

1. Mixed effect model 2. Parameter estimation summary(Z)

# 5. Variance component VarCorr(z)

# repeatability 1.11504^2/(1.11504^2 + 0.59833^2)
0.7764342 # # \ [1 \]
# 7. Relation plot(z) between residuals and fitted values


Goldfish visual

Cronly – Dillon and Muntz (1965; J. Exp. Biol 42:481-493) The visual motor response is used to measure the color vision of goldfish. Here, we will fit the data, including all the wavelengths tested. Each of the five fish was tested at all wavelengths in a random order. A high value of sensitivity indicates that the fish can detect low light intensity. An important feature of the visuomotor response is that fish are not used to the idea that a measurement of visual sensitivity at one wavelength is unlikely to have an effect on a subsequent measurement at another wavelength.

Read and check data

  1. Read the data in the file and look at the first few lines to make sure they read correctly.
  2. Interactive graphs were used to compare the responses of individual fish in experiments with different light wavelengths.
  3. What type of experimental design is used? * This will determine the linear hybrid model to be used when fitting the data.

A linear mixed effects model was constructed

  1. The data were fitted into a linear mixed effects model. This can be done with lmer(). Found “Deformity Fitting,” “Boundary (Singular) Fit: See? IsSingular”
  2. Draw the fit (predicted) value **. The difference between the predicted and observed values for each fish represents the residual.
  3. What assumptions do you make in (1)? Create a graph of residuals and fitting values to check one of these assumptions.
  4. Extract parameter estimates from the saved LMER object. Examine the results of the fixed effect. The coefficients given are the same as the interpretation of the classification variables analyzed using LM.
  5. Check the output of random effects. Again, two sources of random error appear in our mixed-effect model. What are they? Which of these corresponds to the “(intercept)” in the output, which corresponds to the “residual”? Note that in this data set, the estimated standard deviation of one of the sources of change is very small. This is the reason behind the malformed fitting information. It is unlikely that the variance between fish is truly zero, but this data set is very small and low variance estimates may occur due to sampling error.
  6. Generate model-based estimates of the average sensitivity of each wavelength.
  7. Are the differences between the wavelengths significant? Generate an analysis of variance table for the LMER object. What effect is being tested here, a random effect or a fixed effect? Interpret the ANOVA results.

* This is a “subject by subject” repeatable measurement design, because each fish is measured once per experiment. It is essentially the same as a random complete block design (think of each fish as a “block”).

* Visualization is preferred because both the data and the fit values are plotted. Note how similar the predicted values are from fish to fish. This suggests that the estimated differences between individual fish in this study were very small.

* In general, only fixed effects are tested in ANOVA tables. It is possible to test the null hypothesis that there is no variance in random effects.

Analysis steps

Read and check the data.

x <- read.csv("fish.csv", 
        stringsAsFactors = FALSE)
head(x)

Fit into a linear mixed effects model.

The model assumes that the residuals of all fitted values are normally distributed and the variances are equal. The method also assumes that the random intercepts between individual fish are normally distributed. The method also assumes that random sampling of groups (fish) has no effect between the same fish measurements.

# # 1. Fit the mixed effects model.
## boundary (singular) fit: see ? isSingular

# 2. This plots the fit value for each fish. vis(z)

 

# 3. Test hypothesis plot(z)

Summary (Z) Summary (Z)

# 6. Average sensitivity estimation based on model

# 7. ANOVA analysis


The concentration of phenols in yarrow

The project experimentally examines the effects of fertilization and grazing on boreal forest ecosystems in the National Park (Krebs, C.J., Boutin, S. & Boonstra, R., EDS (2001A) Ecosystem dynamics of the Boreal Forest.Kluane project. Oxford University Press, New York)), the current data came from a study of the effects of plant resources and herbivores on the defensive chemistry of bottom plant species.

Each of the 16 5×5 meter plots was randomly assigned to one of four experiments. 1) Fencing to exclude herbivores; 2) fertilizing with N-P-K fertilizer; 3) Fencing and fertilizing; 4) Control without experiment. Then, each of the 16 plots was divided into two. One side of each plot (selected at random) continued to be tested over the 20 years of the study. The other half of each field was tested for the first ten years and then returned to its untested state. The data to be analyzed here recorded the concentration of phenols (a rough measure of a plant defense compound) in Achillea millefolium, a common herbaceous plant in the plot. It is measured in milligrams of tannic acid equivalent per gram of dry weight.

Visual data

  1. Read data from a file.
  2. Check the data in the first few rows. The experiment was given as a single variable with four levels (rather than as a model with two variables, walls and fertilizer, designed with a 2×2 factor). Duration indicates whether half of the field has been tested for the full 20 years, or whether the experiment is stopped after 10 years. The variable “CH” is the concentration of phenols in yarrow.
  3. Draw a graph showing the concentration of phenols in yarrow for different experiments and duration categories. There are not many data points in each combination of experiment and duration level, so it is probably better to draw a bar chart as a group rather than a box chart as a group.
  4. Add line segments to join pairs of points.

Fit into a linear mixed effects model

  1. What type of experimental design was used? * This will determine the fit of the linear mixed model for the data.
  2. In the absence of interaction between experiment and duration, linear hybrid model fitting was performed on the data. The logarithm of phenols was used as the dependent variable because the logarithmic transformation improved the fitting of the data to the assumptions of the linear model.
  3. Data fitting by visual model. Split the panels by duration (if Xvar is an experiment) or experiment (if Xvar is a duration). Visreg () does not preserve the pairing, but it does allow you to check the residuals.
  4. The model fitting is now repeated, but this time includes the interaction between the experiment and duration. Visualize the fit between the model and the data. What is the most obvious difference between the two model fits, one with interaction and the other without? Describe what a model that includes interaction terms “allows”, while a model that has no interaction terms does not. Determine, which model is best for the data?
  5. Diagnostic diagrams are used to examine one of the key assumptions of a linear hybrid model that includes interaction items.
  6. The parameters of a linear model (including interactions) are estimated using a fitting model object. Notice that there are now a number of coefficients in the fixed effects table.
  7. In the output from the previous step, you will see two quantities of “std.dev” under the “Random Effects” TAB. Explain what these quantities refer to.
  8. To estimate the model-fitting mean for all fixed effect combinations.
  9. Generate analysis of variance tables for fixed effects. Which items are statistically significant?
  10. By default, lmerTest will test model items using the sum of squares of Type 3, rather than sequentially (Type 1). Repeat the ANOVA table with Type 1. Did the results make a difference? 台湾国

* The experiment used a block design, where the entire block was randomly assigned to different experiments, and then different levels of the second experiment (duration) were assigned to half of the block.

* There should be no difference because the design is perfectly balanced.

Analysis steps

Read and check the data.

A good strategy is to rank the categories of experiments, putting the control group first. This will make the output of the linear model more useful.

# 2. Check head(x)

# 3. Group band chart # First, Factor (TREAT, Levels = C (" CONT "," EXC "," FER "," BO ")) PLOT (DATA = X, Y = LOG (PHE), X = TREAT, FILL = DURA, color = dura)

Plot (data = x,y = log(ach, x = dur, fill = dur, col = dur)

Fit into a linear mixed effects model. The fixed effect is “experiment” and “duration”, while the “block” is the random effect. When fitting interactions, the magnitude of differences between experimental levels will vary between duration levels.

Since random effects also exist (blocks), the coefficient table will show the variance estimates for two sources of random variation. One is the variance of the residual of the fitting model. The second is the variance between the (random) block intercepts.

# 2. Fit mixed effect model - no interaction
# 3. Visualize Vis (Z)

# 4. Including interaction and visual again z.i nt < - lmer (log (phen. Ach) ~ * duration treatment + 1 | (plot), data = x) vis (z.i nt, overlay = TRUE)

# 5. Plot a chart to test for homogeneity of variance (and normality)

# 6. Summary (Z)

# 8. Means of model fitting (z, data = x)

# 9. ANOVA table ANOVA (Z) # LMERTEST default is the sum of 3 classes of squares.

# 10. Change to class 1 (Z, type = 1)


The most popular insight

1. LMER mixed linear regression model based on R language

Exploring LME4 Generalized Linear Mixed Model (GLMM) and Linear Mixed Model (LMM) with RSHINY in R Language

3. Practical case of linear mixed effects model in R language

4. Practical case of linear mixed effects model in R language II

5. Practical case of linear mixed effects model in R language

The partially folded Gibbs sampling of the Linear Mixed-Effects Models

7. LME4 mixed-effect model for R language to study teacher popularity

8. HAR-RV model based on mixed data sampling (MIDAS) regression in R language predicts GDP growth

9. Hierarchical linear model HLM using SAS, STATA, HLM, R, SPSS and MPLUS