Open In App

Calculate Standard Error in R

Last Updated : 07 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to calculate standard error in R Programming Language. 

Mathematically we can calculate standard error by using the formula:

standard deviation/squareroot(n)

In R Language, we can calculate in these ways:

  • Using sd() function with length function
  • By using the standard error formula.
  • Using plotrix package.

Method 1 : Using sd() function with length function

Here we are going to use sd() function which will calculate the standard deviation and then the length() function to find the total number of observation.

Syntax: sd(data)/sqrt(length((data)))

Example: R program to calculate a standard error from a set of 10 values in a vector

R




# consider a vector with 10 elements
a < - c(179, 160, 136, 227, 123, 23,
        45, 67, 1, 234)
 
# calculate standard error
print(sd(a)/sqrt(length((a))))


Output:

[1] 26.20274

Method 2: By using standard error formula

Here we will use the standard error formula for getting the observations.

Syntax: sqrt(sum((a-mean(a))^2/(length(a)-1)))/sqrt(length(a))

where

  • data is the input data
  • sqrt function is to find the square root
  • sum is used to find the sum of elements in the data
  • mean is the function used to find the mean of the data
  • length is the function used to return the length of the data

Example: R program to calculate the standard error using formula

R




# consider a vector with 10 elements
a <- c(179, 160, 136, 227, 123, 23,
       45, 67, 1, 234)
 
# calculate standard error
print(sqrt(sum((a - mean(a)) ^ 2/(length(a) - 1)))
      /sqrt(length(a)))


Output:

[1] 26.20274

Method 3 : Using std.error() function( plotrix package)

This is the built-in function that directly calculated the standard error. It is available in plotrix package 

Syntax: std.error(data)

Example: R program to calculate the standard error using std.error()

R




# import plotrix package
library("plotrix")
 
# consider a vector with 10 elements
a <- c(179,160,136,227,123,
       23,45,67,1,234)
 
# calculate standard error using in built
# function
print(std.error(a))


Output:

[1] 26.20274


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads