Home » How to Calculate Standard Error of the Mean in R

How to Calculate Standard Error of the Mean in R

by Tutor Aspire

The standard error of the mean is a way to measure how spread out values are in a dataset. It is calculated as:

Standard error = s / √n

where:

  • s: sample standard deviation
  • n: sample size

This tutorial explains two methods you can use to calculate the standard error of a dataset in R.

Method 1: Use the Plotrix Library

The first way to calculate the standard error of the mean is to use the built-in std.error() function from the Plotrix library.

The following code shows how to use this function:

library(plotrix)

#define dataset
data #calculate standard error of the mean 
std.error(data)

2.001447

The standard error of the mean turns out to be 2.001447.

Method 2: Define Your Own Function

Another way to calculate the standard error of the mean for a dataset is to simply define your own function.

The following code shows how to do so:

#define standard error of mean function
std.error function(x) sd(x)/sqrt(length(x))

#define dataset
data #calculate standard error of the mean 
std.error(data)

2.001447

Once again, the standard error of the mean turns out to be 2.0014.

How to Interpret the Standard Error of the Mean

The standard error of the mean is simply a measure of how spread out values are around the mean.

There are two things to keep in mind when interpreting the standard error of the mean:

1. The larger the standard error of the mean, the more spread out values are around the mean in a dataset.

To illustrate this, consider if we change the last value in the previous dataset to a much larger number:

#define dataset
data  28, 150)

#calculate standard error of the mean 
std.error(data)

6.978265

Notice how the standard error jumps from 2.001447 to 6.978265.

This is an indication that the values in this dataset are more spread out around the mean compared to the previous dataset.

2. As the sample size increases, the standard error of the mean tends to decrease.

To illustrate this, consider the standard error of the mean for the following two datasets:

#define first dataset and find SEM
data1 #define second dataset and find SEM
data2 

The second dataset is simply the first dataset repeated twice.

Thus, the two datasets have the same mean but the second dataset has a larger sample size so it has a smaller standard error.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Calculate Sample & Population Variance in R
How to Calculate Pooled Variance in R
How to Calculate the Coefficient of Variation in R

You may also like