4.1.6 Influence of the Shape of the Data Distribution

# Fig. 4.1.: R figures for DATA DISTRIBUTION:

#####################################################################
#####################################################################
# Examples of serveral distributions with location measures



# Lagemasse berechnen #
#######################

"lage" <- function(x,xrange,fx)
{
A=mean(x)
G=exp(mean(log(x)))
Med=median(x)
Mod=xrange[which.max(fx)]
c(A=A,G=G,Med=Med,Mod=Mod)
}

###############################################
library(StatDA)
pdf("fig-4-1.pdf",width=8,height=10)
par(mfrow=c(3,2))
par(mar=c(5,0.2,4,2))

# normal N(0,1) #
#################


n=100000
set.seed(110)
x=rnorm(n,mean=100)
xrange=seq(94,106,by=0.01)
fx=dnorm(xrange,mean=100)
val=lage(x,xrange,fx)

plot(xrange,fx,type="l",xlab="",ylab="",main="Normal distribution",
frame.plot=FALSE,yaxt="n",xaxt="n",lwd=2,cex.main=1.6,ylim=c(-0.02,0.42))
segments(min(xrange),0,max(xrange),0)

# Strich einzeichnen
lu=-0.001
lo=0.001+max(fx)
# Text positionieren
tl=0.3
# Textgroesse
tc=0.8
# y-Koord. von Pfeilspitze
pe=-0.005
# y-Koord. von Pfeilschaft
pt=-0.04
# Pfeilspitzenl�nge
pl=0.07

par(las=3)

segments(val['A'],lu,val['A'],lo)
mtext("MEAN",side=1,line=tl,at=val['A'],adj=1,padj=-2.5,cex=tc)
arrows(val['A']-1,pt,val['A']+0.1,pe,length=pl)

segments(val['G'],lu,val['G'],lo)
mtext("G",side=1,line=tl,at=val['G'],adj=1,padj=-1,cex=tc)
arrows(val['G']-0.45,pt,val['G'],pe,length=pl)

segments(val['Med'],lu,val['Med'],lo)
mtext("MEDIAN",side=1,line=tl,at=val['Med'],adj=1,padj=2,cex=tc)
arrows(val['Med']+0.5,pt,val['Med']+0.1,pe,length=pl)


segments(val['Mod'],lu,val['Mod'],lo)
mtext("MODE",side=1,line=tl,at=val['Mod'],adj=1,padj=0.5,cex=tc)
arrows(val['Mod']+0.05,pt,val['Mod'],pe,length=pl)

par(las=0)



# Log - normal #
################

n=100000
set.seed(110)
x=rlnorm(n)
xrange=seq(-1,10,by=0.01)
fx=dlnorm(xrange) # density function
val=lage(x,xrange,fx)

plot(xrange,fx,type="l",xlab="",ylab="",main="Lognormal distribution",
frame.plot=FALSE,yaxt="n",xaxt="n",lwd=2,cex.main=1.6,ylim=c(-0.02,0.67))
segments(min(xrange),0,max(xrange),0)

segments(0,-0.005,0,0.02)
mtext("0",side=1,line=-0.1,at=0,adj=0.5,padj=0,cex=1)

# Strich einzeichnen
lu=-0.001
lo=0.001+max(fx)
# Text positionieren
tl=0.35
# Textgroesse
tc=0.8
# y-Koord. von Pfeilspitze
pe=-0.005
# y-Koord. von Pfeilschaft
pt=-0.05
# Pfeilspitzenl�nge
pl=0.07

par(las=3)

segments(val['A'],lu,val['A'],lo)
mtext("MEAN",side=1,line=tl,at=val['A'],adj=1,padj=0.5,cex=tc)

segments(val['G'],lu,val['G'],lo)
mtext("G",side=1,line=tl,at=val['G'],adj=1,padj=-0.2,cex=tc)
arrows(val['G']-0.2,pt,val['G']-0.05,pe,length=pl)

segments(val['Med'],lu,val['Med'],lo)
mtext("MEDIAN",side=1,line=tl,at=val['Med'],adj=1,padj=1,cex=tc)
arrows(val['Med']+0.2,pt,val['Med']+0.05,pe,length=pl)

segments(val['Mod'],lu,val['Mod'],lo)
mtext("MODE",side=1,line=tl,at=val['Mod'],adj=1,padj=0.5,cex=tc)

par(las=0)



# t df=5 #
##########


n=100000
set.seed(110)
xzu=rt(n, df = 5)
x=xzu+100
xrange=seq(96,104,by=0.01)
fx=dt(xrange-100, df = 5) # density function
val=lage(x,xrange,fx)

plot(xrange,fx,type="l",xlab="",ylab="",main="Student t distribution, df=5",
frame.plot=FALSE,yaxt="n",xaxt="n",lwd=2,cex.main=1.6,ylim=c(-0.02,0.4))
segments(min(xrange),0,max(xrange),0)

# Strich einzeichnen
lu=-0.001
lo=0.001+max(fx)
# Text positionieren
tl=0.3
# Textgroesse
tc=0.8
# y-Koord. von Pfeilspitze
pe=-0.005
# y-Koord. von Pfeilschaft
pt=-0.04
# Pfeilspitzenl�nge
pl=0.07

par(las=3)

segments(val['A'],lu,val['A'],lo)
mtext("MEAN",side=1,line=tl,at=val['A'],adj=1,padj=-2,cex=tc)
arrows(val['A']-0.6,pt,val['A']-0.1,pe,length=pl)

segments(val['G'],lu,val['G'],lo)
mtext("G",side=1,line=tl,at=val['G'],adj=1,padj=-0.5,cex=tc)
arrows(val['G']-0.2,pt,val['G']-0.05,pe,length=pl)

segments(val['Med'],lu,val['Med'],lo)
mtext("MEDIAN",side=1,line=tl,at=val['Med'],adj=1,padj=2.5,cex=tc)
arrows(val['Med']+0.5,pt,val['Med']+0.1,pe,length=pl)

segments(val['Mod'],lu,val['Mod'],lo)
mtext("MODE",side=1,line=tl,at=val['Mod'],adj=1,padj=1,cex=tc)
arrows(val['Mod']+0.09,pt,val['Mod'],pe,length=pl)

par(las=0)



# chisq 5 #
###########

par(mar=c(6,0.2,4,2))

n=100000
set.seed(110)
x=rchisq(n,5)
xrange=seq(-1,14,by=0.01)
fx=dchisq(xrange,5) # density function
val=lage(x,xrange,fx)

plot(xrange,fx,type="l",xlab="",ylab="",main="Chi-square distribution, df=5",
frame.plot=FALSE,yaxt="n",xaxt="n",lwd=2,cex.main=1.6)

segments(min(xrange),0,max(xrange),0)
segments(0,-0.002,0,0.002)
mtext("0",side=1,line=0,at=-0.1,adj=0.5,padj=0,cex=1)

# Strich einzeichnen
lu=-0.001
lo=0.001+max(fx)
# Text positionieren
tl=0.3
# Textgroesse
tc=0.8
# y-Koord. von Pfeilspitze
pe=-0.01
# y-Koord. von Pfeilschaft
pt=-0.2
# Pfeilspitzenl�nge
pl=0.07

par(las=3)

segments(val['A'],lu,val['A'],lo)
mtext("MEAN",side=1,line=tl,at=val['A'],adj=1,padj=0.5,cex=tc)
#arrows(val['A']+0.4,pt,val['A']+0.2,pe,length=pl)

segments(val['G'],lu,val['G'],lo)
mtext("G",side=1,line=tl,at=val['G'],adj=1,padj=0.2,cex=tc)
#arrows(val['G']-0.4,pt,val['G']-0.2,pe,length=pl)

segments(val['Med'],lu,val['Med'],lo)
mtext("MEDIAN",side=1,line=tl,at=val['Med'],adj=1,padj=0.8,cex=tc)
#arrows(val['Med']+0.15,pt,val['Med']+0.05,pe,length=pl)

segments(val['Mod'],lu,val['Mod'],lo)
mtext("MODE",side=1,line=tl,at=val['Mod'],adj=1,padj=0.5,cex=tc)
arrows(val['Mod']+0.1,pt,val['Mod'],pe,length=pl)

par(las=0)



# exp #
#######


n=100000
set.seed(110)
x=rexp(n)
xrange=seq(-1,7,by=0.01)
fx=dexp(xrange)
val=lage(x,xrange,fx)

plot(xrange,fx,type="l",xlab="",ylab="",main="Exponential distribution",
frame.plot=FALSE,xaxt="n",yaxt="n",lwd=2,cex.main=1.6,ylim=c(-0.02,1.02))
segments(min(xrange),0,max(xrange),0)
segments(0,-0.002,0,0.002)
mtext("0",side=1,line=-0.1,at=-0.1,adj=0.5,padj=0,cex=1)

# Strich einzeichnen
lu=-0.001
lo=0.001+max(fx)
# Text positionieren
tl=0.5
# Textgroesse
tc=0.8
# y-Koord. von Pfeilspitze
pe=-0.015
# y-Koord. von Pfeilschaft
pt=-700
# Pfeilspitzenl�nge
pl=0.07

par(las=3)

segments(val['A'],lu,val['A'],lo)
mtext("MEAN",side=1,line=tl,at=val['A']+0.03,adj=1,padj=0.5,cex=tc)

segments(val['G'],lu,val['G'],lo)
mtext("G",side=1,line=tl,at=val['G']-0.05,adj=1,padj=0.3,cex=tc)

segments(val['Med'],lu,val['Med'],lo)
mtext("MEDIAN",side=1,line=tl,at=val['Med'],adj=1,padj=0.7,cex=tc)

segments(val['Mod'],lu,val['Mod'],lo)
mtext("MODE",side=1,line=tl,at=val['Mod'],adj=1,padj=1,cex=tc)
arrows(0.2,-0.1,-0.01,-0.02,length=0.07)

par(las=0)



# multimodal (3 normal) #
#########################

n=100
set.seed(110)
x1=rnorm(n, mean = 96, sd = 0.8)
set.seed(110)
x2=rnorm(n, mean = 98.5, sd = 1.2)
set.seed(110)
x3=rnorm(n, mean = 103, sd = 1.6)
x=cbind(x1,x2,x3)

xrange=seq(93,107,by=0.01)

fx1=dnorm(xrange, mean = 96, sd = 0.8)
fx2=dnorm(xrange, mean = 98.5, sd = 1.2)
fx3=dnorm(xrange, mean = 103, sd = 1.6)
fx=fx1+fx2+fx3
val=lage(x,xrange,fx)

plot(xrange,fx,type="l",xlab="",ylab="",main="Multimodal distribution",
frame.plot=FALSE,yaxt="n",xaxt="n",lwd=2,cex.main=1.6,ylim=c(-0.02,0.62))
segments(min(xrange),0,max(xrange),0)

# Strich einzeichnen
lu=-0.001
lo=0.001+max(fx)
# Text positionieren
tl=0.4
# Textgroesse
tc=0.8
# y-Koord. von Pfeilspitze
pe=-0.01
# y-Koord. von Pfeilschaft
pt=-0.07
# Pfeilspitzenl�nge
pl=0.07

par(las=3)

segments(val['A'],lu,val['A'],lo)
mtext("MEAN",side=1,line=tl,at=val['A']-0.1,adj=1,padj=0.7,cex=tc)
arrows(val['A'],pt,val['A']+0.1,pe,length=pl)

segments(val['G'],lu,val['G'],lo)
mtext("G",side=1,line=tl,at=val['G'],adj=1,padj=2,cex=tc)
arrows(val['G']+0.6,pt,val['G']+0.2,pe,length=pl)

segments(val['Med'],lu,val['Med'],lo)
mtext("MEDIAN",side=1,line=tl,at=val['Med']-0.1,adj=1,padj=0.5,cex=tc)

segments(val['Mod'],lu,val['Mod'],lo)
mtext("MODE",side=1,line=tl,at=val['Mod']-0.2,adj=1,padj=1,cex=tc)

par(las=0)


dev.off()