Site icon R-bloggers

How to perform Eta Squared in R

[This article was first published on Methods – finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to perform Eta Squared in R appeared first on finnstats.

If you want to read the original article, click here How to perform Eta Squared in R.

Eta Squared in R, Eta squared is a commonly-used effect size metric in ANOVA models.

It is calculated as follows: It measures the proportion of variance associated with each main effect and interaction effect in an ANOVA model.

Eta squared = SSeffect / SStotal

where:

SSeffect: For a single variable, the sum of squares of an effect.

SStotal: In the ANOVA model, the total sum of squares.

The value of Eta squared varies from 0 to 1, with values closer to 1 indicating that a specific variable in the model can explain a greater fraction of the variation.

The following are some general guidelines for interpreting Eta squared values:

0.01: Effect size is small.

0.06: Effect size is medium.

Large effect size if the number is 0.14 or above.

This tutorial shows you how to calculate Eta squared for variables in an ANOVA model in R step by step.

Step 1: Dataset Creation

Let’s say we want to see if duty intensity and gender have an effect on weight loss.

To investigate this, we recruited 30 men and 30 women to take part in a one-month trial in which 10 of each were randomly assigned to either no duty, light duty, or intensive duty.

The code below demonstrates how to build a data frame to hold the data we’re dealing with:

Make this example repeatable.

set.seed(123)

Let’s create a data frame

data <- data.frame(gender = rep(c("Male", "Female"), each = 30),
                   duty = rep(c("NO", "LIGHT", "HEAVY"), each = 10, times = 2),
                   weight_loss = c(runif(10, -5, 5), runif(10, 2, 5), runif(10, 5, 9),
                   runif(10, -5, 5), runif(10, 0, 3), runif(10, 3, 8)))

Let’s view the first six rows

head(data)
  gender duty weight_loss
1   Male       NO -3.03455710
2   Male       NO  2.95565356
3   Male       NO -0.08550515
4   Male       NO -4.07388111
5   Male       NO  2.63758055
6   Male       NO  0.36200633

Count how many people are in each category.

table(data$gender, data$duty)
          HEAVY LIGHT NO
  Female    10    10 10
  Male      10    10 10

Step 2: Fit the ANOVA Model

The following code demonstrates how to fit a two-way ANOVA with weight reduction as the response variable and activity and gender as factors:

fit the ANOVA model with two variables

model <- aov(weight_loss ~ gender + duty, data = data)
model
Call:
   aov(formula = weight_loss ~ gender + duty, data = data)
Terms:
                  gender     duty Residuals
Sum of Squares   29.7734 350.8557  168.8440
Deg. of Freedom        1        2        56
Residual standard error: 1.736396

Estimated effects may be unbalanced

Now we can view the model summary

summary(model)
        Df Sum Sq Mean Sq F value   Pr(>F)   
gender       1   29.8   29.77   9.875  0.00268 **
duty         2  350.9  175.43  58.184 2.13e-14 ***
Residuals   56  168.8    3.02                    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Step 3: Calculate Eta Squared in R

Using the etaSquared() function from the lsr package, we can determine the effect size Eta squared for each variable in our model:

install and load the lsr package

library(lsr)

Now we can calculate the Eta Squared

etaSquared(model)
eta.sq eta.sq.part
gender 0.05418539   0.1499034
duty   0.63853122   0.6751124

For gender and duty, the Eta squared is as follows.

For gender, eta squared is 0 05418539

duty eta squared: 0. 63853122

We may conclude that the effect size for duty is large, but the effect size for gender is minimal.

The p-values in the ANOVA table’s output match to these results. Duty has a significantly lower p-value (2.13e-14) than gender (0.00268), indicating that duty has a far stronger impact on weight loss prediction.

To read more visit How to perform Eta Squared in R.

If you are interested to learn more about data science, you can find more articles here finnstats.

The post How to perform Eta Squared in R appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Methods – finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.