Statplot and Related Functions – copy and paste the functions into R

advertisement
Statplot and Related Functions – copy and paste the functions into R
Statplot = function(x, xname = deparse(substitute(x)))
{
par(err = -1)
par(mfrow = c(2, 2))
par(cex = 0.7)
skewness <- mean((x - mean(x))^3)/sd(x)^3
dens <- density(x, width = bandwidth.nrd(x))
ylim <- range(dens$y)
xlim <- range(dens$x)
hist(x, nclass.FD(x), prob = T, xlab = xname, xlim = xlim, ylim
= ylim,main=paste("Distribution of",xname),col="sky blue")
lines(dens)
boxplot(x, xlab = xname,main=paste("Boxplot of",xname))
symplot(x,xname)
qqnorm.env(x, ylab = xname)
abline(mean(x),sqrt(var(x)),col=2)
par(mfrow=c(1,1))
invisible()
}
bandwidth.nrd = function (x)
{
r <- quantile(x, c(0.25, 0.75))
h <- (r[2] - r[1])/1.34
4 * 1.06 * min(sqrt(var(x)), h) * length(x)^(-1/5)
}
Symplot = function (x, xname=xname,positive = F,
addline = F, xlab = "", ylab = "", ...)
{
mdx <- mad(x)
skewness <- mean((x - mean(x))^3)/sd(x)^3
x <- sort(x)/ifelse(mdx <= 0, 1, mdx)
positive <- positive && x[1] > 0
lx <- length(x)
mid <- floor(lx/2)
medx <- quantile(x, 0.5)
low <- x[1:mid]
up <- x[lx:(lx-mid+1)]
list(low, up)
symx <- if (positive) ((up-medx)^2 + (medx-low)^2)/4/medx
else mid:1
symy <- (up+low)/2 - medx
slim <- 4/sqrt(lx)
plot(symx, symy, ylim = range(symy, -slim, slim), xaxt = "n",
xlab = xlab, ylab = ylab,main=paste("Symmetry Plot of",xname),...)
abline(h = c(-slim, slim), lty = 2, col = 3)
if (positive)
axis(1)
else axis(1, at = c(1, mid/2, mid), labels = c("center",
"quartiles", "tails"), tck = 0)
if (positive && addline)
abline(0, 1, col = 3)
text(mean(symx)-sd(symx),slim-2*slim/10,paste("Skewness
=",signif(skewness,3)))
invisible()
}
qqnorm.env = function (x, ylab = deparse(substitute(x)),
main = "", pch = 1)
{
n <- length(x)
xs <- scale(x)
ident <- diag(n)
eps <- matrix(0, n, 100)
e <- matrix(0, n, 100)
e1 <- numeric(n)
e2 <- numeric(n)
for (i in 1:100) {
eps[, i] <- rnorm(n, 0, 1)
e[, i] <- sort(eps[, i])
}
for (i in 1:n) {
eo <- sort(e[i,])
e1[i] <- eo[5]
e2[i] <- eo[95]
}
e1 <- sqrt(var(x)) * e1 + mean(x)
e2 <- sqrt(var(x)) * e2 + mean(x)
ylim <- range(x, e1, e2)
if (n > 200) {pch = "."}
qqnorm(x, ylab = ylab, ylim = ylim, pch = pch, col = 1, main = main,
cex = 0.75)
par(new = T)
qqnorm(e1, axes = F, xlab = "", ylab = "", type = "l", ylim = ylim,
col = 4)
par(new = T)
qqnorm(e2, axes = F, xlab = "", ylab = "", type = "l", ylim = ylim,
col = 4)
abline(mean(x),sd(x))
sw <- shapiro.test(x)
text(-1,max(x)-(sd(x)/10),paste("Shapiro Test
(p=",signif(sw$p.value,4),")"))
invisible()
}
Statplot2 = function(x, xname = deparse(substitute(x)))
{
par(err = -1)
par(mfrow = c(2, 2))
par(cex = 0.7)
skewness <- mean((x - mean(x))^3)/sd(x)^3
dens <- density(x, width = bandwidth.nrd(x))
ylim <- range(dens$y)
xlim <- range(dens$x)
hist(x, nclass.FD(x), prob = T, xlab = xname, xlim = xlim, ylim
= ylim,main=paste("Distribution of",xname),col="sky blue")
lines(dens)
boxplot(x, xlab = xname,main=paste("Boxplot of",xname))
symplot(x,xname)
qqnorm(x, ylab = xname)
abline(mean(x),sqrt(var(x)),col=2)
par(mfrow=c(1,1))
invisible()
}
The Statplot2 does not computer Shapiro-Wilks test for normality and does not give
simulated envelope for the normal quantile plot. It is used when the sample size is large.
Related documents
Download