Woony
Woony

Reputation: 33

How to determine the fixed and random effect using linear mixed effect model?

I would like to investigate the variation of eye size among species and among type using linear mixed model. However, I am a little bit confuse to determine whether to be random effect and fixed effect. I tried to use this model: lmer(Eyesize ~ Species + (1|Type)

this is the data : data

this is my code:

library(lme4)
library(lmerTest)
library(multcomp)

example_data <-read.csv("EXAMPLE 1.csv", h= TRUE)
head(example_data)
result = lmer(Eyesize ~ Species + (1|Type), data=example_data)
summary(result)

I got the result of an analysis like this

result

I cannot get the analysis result for species A. I think it is because I used the wrong random or fixed effect in this mode. I also want to indicate the variation with the letter using Tukey analysis. Could anyone help me to solve this?

Thank you so much.

Upvotes: 0

Views: 124

Answers (1)

SushiChef
SushiChef

Reputation: 633

The 3rd level of Species doesnt show up because it's made into the reference level.

It sounds like you are after a post-hoc comparison, which can be done on the lme4 object using emmeans::emmeans.

In the future, please make the data easier to access, like this:

library(lme4)
library(emmeans)
# df <- read.csv("stack_q_lme4.csv")
# df$Eyesize <- as.numeric(df$Eyesize)
# df$factor <- as.factor(df$Species)
# df$Type <- as.factor(df$Type)
#dput(df)
df <- structure(list(Eyesize = c(0.428278862, 0.400995265, 0.39874335, 
                           0.393709024, 0.425950648, 0.422276073, 0.423718381, 0.436375912, 
                           0.434729533, 0.44292607, 0.474962122, 0.480422148, 0.446239189, 
                           0.456237883, 0.479825541, 0.319118225, 0.34803004, 0.392926567, 
                           0.355179776, 0.327142563, 0.292814474, 0.295765514, 0.268828342, 
                           0.269960081, 0.2834768, 0.356604986, 0.348001735, 0.360895441, 
                           0.342982001, 0.366047801, 0.317237392, 0.293341926, 0.308761142, 
                           0.30039708, 0.308900879, 0.326311003, 0.33954796, 0.316756444, 
                           0.317607287, 0.33562927), Species = c("Species A", "Species A", 
                                                                 "Species A", "Species A", "Species A", "Species A", "Species A", 
                                                                 "Species A", "Species A", "Species A", "Species A", "Species A", 
                                                                 "Species A", "Species A", "Species A", "Species B", "Species B", 
                                                                 "Species B", "Species B", "Species B", "Species B", "Species B", 
                                                                 "Species B", "Species B", "Species B", "Species B", "Species B", 
                                                                 "Species B", "Species B", "Species B", "Species B", "Species B", 
                                                                 "Species B", "Species B", "Species B", "Species C", "Species C", 
                                                                 "Species C", "Species C", "Species C"), Type = structure(c(6L, 
                                                                                                                            6L, 6L, 6L, 6L, 8L, 8L, 8L, 8L, 8L, 3L, 3L, 3L, 3L, 3L, 5L, 5L, 
                                                                                                                            5L, 5L, 5L, 7L, 7L, 7L, 7L, 7L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 
                                                                                                                            1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Type B", "Type K", "Type L", 
                                                                                                                                                                    "Type P", "Type Q", "type R", "Type T", "Type W"), class = "factor"), 
               factor = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                    1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
                                    3L), .Label = c("Species A", "Species B", "Species C"), class = "factor")), row.names = c(NA, 
                                                                                                                              -40L), class = "data.frame")

Here is what I think you're after, marginal means and pairwise comparisons of species.


m1 <- lmer(Eyesize ~ Species + (1|Type), data=df)

emmeans(m1, pairwise ~ Species)
# $emmeans
# Species   emmean     SE df lower.CL upper.CL
# Species A  0.436 0.0189  5    0.388    0.485
# Species B  0.323 0.0163  5    0.281    0.365
# Species C  0.327 0.0327  5    0.243    0.411
# 
# Degrees-of-freedom method: kenward-roger 
# Confidence level used: 0.95 
# 
# $contrasts
# contrast              estimate     SE df t.ratio p.value
# Species A - Species B  0.11354 0.0250  5   4.547  0.0140
# Species A - Species C  0.10919 0.0377  5   2.893  0.0742
# Species B - Species C -0.00435 0.0365  5  -0.119  0.9922
# 
# Degrees-of-freedom method: kenward-roger 
# P value adjustment: tukey method for comparing a family of 3 estimates

Upvotes: 1

Related Questions