Problems based on measures of central tendency. Mean, Geometric mean, Harmonic mean, median, mode & Quartiles Experiment :1 For Ungrouped data. Experiment :2 For Ungrouped data with NA. Experiment :3 Forgrouped data without class intervals. Experiment :4 For grouped data with class intervals.
#Psych Package
install.packages("psych")
library("psych")
#UnGrouped Data
#Arithmetic_Mean
rainfall=c(10,10,10,10,10,560,640,520,320,90,20,10)
mean(rainfall)
#Geometric_Mean
rainfall=c(10,10,10,10,10,560,640,520,320,90,20,10)
geometric.mean(rainfall)
#Harmonic_Mean
rainfall=c(10,10,10,10,10,560,640,520,320,90,20,10)
harmonic.mean(rainfall)
#Median
rainfall=c(10,10,10,10,10,560,640,520,320,90,20,10)
median(rainfall)
#Mode
rainfall=c(10,10,10,10,10,560,640,520,320,90,20,10)
mode=function(x)
{
uniqx=unique (x)
uniqx[which.max(tabulate(match(x,uniqx)))]
}
mode(rainfall)
#Modeest
install.packages("modeest")
library(modeest)
rainfall=c(10,10,10,10,10,560,640,520,320,90,20,10)
mode=mfv(rainfall)
if(length(rainfall)==length(unique(rainfall))) print("Mode does not exist") else mode
#Quantile_25%
rainfall=c(10,10,10,10,10,560,640,520,320,90,20,10)
quantile(rainfall,0.25)
#Quantile_50%
rainfall=c(10,10,10,10,10,560,640,520,320,90,20,10)
quantile(rainfall,0.50)
#Program-2 Ungrouped Data
#Mean
x=c(NA,8,6,5.5,7,4.5,NA)
mean(x,na.rm=TRUE)
#G.M
x=c(NA,8,6,5.5,7,4.5,NA)
geometric.mean(x,na.rm=TRUE)
#H.M
x=c(NA,8,6,5.5,7,4.5,NA)
harmonic.mean(x,na.rm=TRUE)
#Median
x=c(NA,8,6,5.5,7,4.5,NA)
median(x,na.rm=TRUE)
#Mode
library(modeest)
x=c(NA,8,6,5.5,7,4.5,NA)
y=na.omit(x)
mode=mfv(y)
if(length(y)==length(unique(y))) print("Mode does not exist") else mode
#Quarile_75%
x=c(NA,8,6,5.5,7,4.5,NA)
y=na.omit(x)
quantile(y,0.75)
#Quarile_10%
x=c(NA,8,6,5.5,7,4.5,NA)
y=na.omit(x)
quantile(y,0.10)
#PROGRAM-03
#Mean
x=c(0,1,2,3,4,5,6)
f=c(3,5,4,6,4,5,3)
y=rep(x,f)
mean(y)
#G.M
x=c(0,1,2,3,4,5,6)
f=c(3,5,4,6,4,5,3)
y=rep(x,f)
geometric.mean(y)
#H.M
x=c(0,1,2,3,4,5,6)
f=c(3,5,4,6,4,5,3)
y=rep(x,f)
harmonic.mean(y)
#Median
x=c(0,1,2,3,4,5,6)
f=c(3,5,4,6,4,5,3)
y=rep(x,f)
median(y)
#Mode
library(modeest)
x=c(0,1,2,3,4,5,6)
f=c(3,5,4,6,4,5,3)
y=rep(x,f)
mode=mfv(y)
if(length(y)==length(unique(y))) print("Mode does not exist") else mode
#Quartile_25%
x=c(0,1,2,3,4,5,6)
f=c(3,5,4,6,4,5,3)
y=rep(x,f)
quantile(y,0.25)
#Quartile_21%
x=c(0,1,2,3,4,5,6)
f=c(3,5,4,6,4,5,3)
y=rep(x,f)
quantile(y,0.21)
#Grouped_Data
#Program 4
#A_Mean
ub=c(200,400,600,800,1000,1200,1400)
lb=c(0,200,400,600,800,1000,1200)
h=200
x=(ub+lb)/2
f=c(1,3,11,14,9,4,2)
n=sum(f)
am=sum(x*f)/n
print(am)
#G.M
ub=c(200,400,600,800,1000,1200,1400)
lb=c(0,200,400,600,800,1000,1200)
h=200
x=(ub+lb)/2
f=c(1,3,11,14,9,4,2)
n=sum(f)
gm=10^(sum(f*log10(x))/n)
print(gm)
#H.M
ub=c(200,400,600,800,1000,1200,1400)
lb=c(0,200,400,600,800,1000,1200)
h=200
x=(ub+lb)/2
f=c(1,3,11,14,9,4,2)
n=sum(f)
hm=n/sum(f/x)
print(hm)
#Median
ub=c(200,400,600,800,1000,1200,1400)
lb=c(0,200,400,600,800,1000,1200)
h=200
x=(ub+lb)/2
f=c(1,3,11,14,9,4,2)
n=sum(f)
lcf=cumsum(f)
medc=min(which(lcf>n/2))
med=lb[medc]+(n/2-lcf[medc-1])*h/f[medc]
med
#Mode
ub=c(200,400,600,800,1000,1200,1400)
lb=c(0,200,400,600,800,1000,1200)
h=200
x=(ub+lb)/2
f=c(1,3,11,14,9,4,2)
n=sum(f)
lcf=cumsum(f)
modc=which(f==max(f))
mode=lb[modc]+h*((f[modc]-f[modc-1])/(2*f[modc]-f[modc-1]-f[modc+1]))
mode
#Quartile_1
ub=c(200,400,600,800,1000,1200,1400)
lb=c(0,200,400,600,800,1000,1200)
h=200
x=(ub+lb)/2
f=c(1,3,11,14,9,4,2)
n=sum(f)
lcf=cumsum(f)
qlc=min(which(lcf>n/4))
q1=lb[qlc]+(n/4-lcf[qlc-1])*h/f[qlc]
q1
#Quartile_3
ub=c(200,400,600,800,1000,1200,1400)
lb=c(0,200,400,600,800,1000,1200)
h=200
x=(ub+lb)/2
f=c(1,3,11,14,9,4,2)
n=sum(f)
lcf=cumsum(f)
qlc=min(which(lcf>3*n/4))
q3=lb[qlc]+(3*n/4-lcf[qlc-1])*h/f[qlc]
q3
Comments
Post a Comment