Calculate Mean Deviation: R code

How to calculate mean deviation?

Mean deviation is nothing but a measure of closeness of the data from the mean. There are three following steps to find out mean deviation.

Step 1: Calculate the mean of the data given.
Step 2: Find the absolute difference of each data points from calculated mean.
Step 3: Calculate the mean of the absolute difference vector you got from step 2

Mean:

If you have a data set of n elements (i.e., you have a vector of n real numbers). Now you want to represent your data with a single element. So the obvious choice would be the average value. So mean value is nothing but the average value.
Let,      be the data elements, then the mean is defined as the following.
 
Inbuilt R function for mean:  mean(data);


Formula for finding mean deviation:

Mean deviation = 
                                                         
Let's take an example to find mean deviation:


Mean deviation

Follow the R code to calculate mean deviation:
 ---------------------------------------------------------------------------------------

data= c(2,5,7,5,0,9,3) # Enter your data
data # Show your data you have entered
n= length(data) # calculating data size
n # data size
miu=mean(data)
miu
d1= abs(data-miu) #calculating absolute value of (data-miu) i.e, |data-miu|
d1
deviation=mean(d1)# calculating mean of |data-miu|
deviation #Print absolute deviation from mean

-----------------------------------------------------------------------------------------
One can improve the code by taking the data as an input. I would advise to make a code for mean value instead using the inbuilt function to get better understanding. R studio is an open source platform for data analysis.
Click to get  RStudio: Download

Click the following to participate interesting quizzes on mathematics:



Post a Comment

0 Comments