Skip to content

Commit

Permalink
emmean chapter write up
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidhuharp97 committed Nov 26, 2024
1 parent 823dde2 commit e9ee1e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
25 changes: 7 additions & 18 deletions chapters/means-and-contrasts.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ execute:

## Background

To start off with, we need to define estimated marginal means (EMM). Estimated marginal means are defined as marginal means of a variable across all levels of other variables in a model, essentially giving a "population-level" average.

Estimated marginal means are defined as marginal means of a variable across all levels of other variables in a model, essentially giving a "population-level" average.
model predictions over the grid comprising all factor combinations – called the reference grid
The emmeans package is one of the most commonly used package in R in determine EMMs. This package provides methods for obtaining EMMs (also known as **least-squares means**) for factor combinations in a variety of models. The emmeans package is one of several alternatives to facilitate post hoc methods application and contrast analysis. It is a relatively recent replacement for the lsmeans package that some R users may be familiar with. It is intended for use with a wide variety of ANOVA models, including repeated measures and nested designs(mixed models). This is a flexible package has a set of detailed [**vignettes**](https://cran.r-project.org/web/packages/emmeans/vignettes/AQuickStart.html) and works with a lot of different model objects.

In this chapter, we will demonstrate the extended use of the emmeans package to calculate estimated marginal means and contrasts.


To demonstrate the use of the emmeans() package. We will pull the model from split plot lesson ([**Chapter 6**](split-plot-design.qmd)), where we evaluated the effect of Nitrogen and Variety on Oat yield. This data contains 6 blocks, 3 main plots (Variety) and 4 subplots (Nitrogen). The primary outcome variable was oat yield.
To read more about the experiment layout details please read RCBD split-plot section in [**Chapter 6**](split-plot-design.qmd).

Expand All @@ -26,19 +25,17 @@ For demonstration of the `emmeans` package, we are fitting model with `nlme` pac
Let's start the analysis by loading the required libraries for fitting linear mixed models using `nlme` package.

## Analysis Examples

```{r, warning=FALSE, message=FALSE}
library(nlme); library(performance); library(emmeans)
library(dplyr); library(broom.mixed)
```


### Import data
Let's import oats data from the MASS package.
```{r}
data1 <- MASS::oats
```


### Model fitting
```{r}
model1 <- lme(Y ~ V + N + V:N ,
Expand All @@ -48,50 +45,42 @@ model1 <- lme(Y ~ V + N + V:N ,
tidy(model1)
```



### Check Model Assumptions

```{r, fig.height=3}
check_model(model1, check = c('normality', 'linearity'))
```

### Model Inference

```{r}
anova(model1, type = "marginal")
```

### Estimated Marginal Means

Now that we have a good model, let’s use the emmeans() function to obtain EMMs.

Now that we have fitted a linear mixed model (model1) and it meets the model assumption. Let's use the `emmeans()` function to obtain estimated marginal means for main (variety and nitrogen) and interaction (variety x nitrogen) effects.

#### Main effects

```{r}
m1 <- emmeans(model1, ~V, level = 0.95)
m1
```

```{r}
m2 <- emmeans(model1, ~N)
m2
```


#### Interaction effects

```{r}
m3 <- emmeans(model1, ~V*N)
m3
m4 <- emmeans(model1, ~V|N)
m4
```




```{r, eval=FALSE}
One-way estimated marginal means and plot
Expand Down
Loading

0 comments on commit e9ee1e2

Please sign in to comment.