DF<-read.table("http://www.stat.sc.edu/~habing/courses/data/Dunn28Full.txt",head=T)
head(DF)
## no SK_L SK_B CR_I FEM TIB HUM ULN TOT AGE CODE EXC
## 1 45 37.8 29.2 77.3 74.7 112.0 73.7 66.5 364.7 2167 A .
## 2 105 40.5 31.2 77.0 81.5 124.0 79.2 72.3 397.5 2513 A .
## 3 277 40.3 31.0 76.9 80.3 116.5 78.6 73.8 389.5 2158 A .
## 4 313 38.9 30.5 78.4 74.8 111.0 72.2 66.6 363.5 1819 A .
## 5 348 37.6 29.2 77.7 73.2 100.5 73.6 67.3 352.2 342 A .
## 6 371 39.7 30.8 77.6 78.2 118.0 76.3 71.2 383.4 1467 A .
DF2<-DF[DF$CODE=="A",1:10]
#DF2[,2]+1
DF2[,2]
## [1] "37.8" "40.5" "40.3" "38.9" "37.6" "39.7" "39.1" "38.3" "39.0" "39.8"
## [11] "39.7" "40.2" "39.7" "38.8" "38.9" "39.0" "35.6" "39.5" "38.8" "39.3"
## [21] "37.7" "37.8" "39.6" "40.0" "40.3" "39.3" "37.4" "38.5" "40.2" "35.9"
## [31] "36.8" "42.4" "39.3" "38.3" "40.2" "40.5" "39.4" "40.5" "37.2" "36.3"
## [41] "39.3" "37.8" "38.0" "37.8" "40.6" "39.2" "38.8" "36.9" "38.7" "40.2"
## [51] "39.6" "38.4" "40.1" "40.5" "40.1" "41.2" "39.8" "39.5" "36.7" "37.9"
## [61] "36.2" "37.4" "37.6" "37.7" "37.3" "37.8" "38.2" "38.3" "37.2" "39.8"
## [71] "38.2" "37.5" "39.8" "37.4" "38.5" "39.6" "37.8" "37.4" "40.4" "39.4"
## [81] "39.6" "39.4" "39.0" "39.3" "36.2" "39.6" "39.1" "38.0" "36.8" "36.6"
## [91] "38.1" "39.6" "39.6" "39.2" "39.3" "37.3" "37.8" "37.5" "36.5" "36.7"
## [101] "36.3" "37.0" "39.2" "38.2" "36.3" "38.3" "37.7" "37.3" "37.3" "38.3"
## [111] "41.4" "40.9" "37.6" "39.3" "37.9" "38.3" "40.3" "40.3" "37.9" "35.9"
## [121] "38.5" "38.6" "40.0" "39.9" "39.7" "38.5" "37.3" "37.8" "39.1" "39.9"
## [131] "39.5" "38.3" "40.3" "39.4" "40.2" "38.7" "39.6" "38.4" "39.1" "38.8"
## [141] "37.4" "38.6" "39.4" "38.6" "37.9" "39.2" "40.4" "38.3" "38.3" "37.2"
## [151] "39.8" "38.5" "37.5" "39.7" "36.4" "37.8" "37.6" "39.9" "37.8" "38.9"
## [161] "38.3" "37.5" "37.4" "38.2" "37.8" "38.7" "40.5" "40.1" "38.9" "38.3"
## [171] "40.0" "36.6" "38.6" "39.4" "35.8" "37.2" "38.3" "37.3" "36.8" "37.7"
## [181] "40.6" "40.8" "39.4" "39.2" "38.0" "40.6" "40.1" "39.8" "40.6" "38.1"
## [191] "40.3" "39.5" "38.4" "39.7" "39.5" "39.1" "39.7" "39.2" "38.9" "39.0"
## [201] "38.0" "37.6" "40.7" "41.0" "40.5" "38.8" "39.6" "39.3" "38.4" "39.5"
## [211] "38.6" "40.2" "38.6" "39.8" "40.1" "41.6" "35.9" "38.5" "38.7" "37.7"
## [221] "36.3" "37.2" "40.1" "38.4" "40.5" "39.7" "40.4" "39.2" "37.4" "40.0"
## [231] "41.1" "38.7" "39.4" "37.3" "39.3" "37.8" "37.6" "38.8" "40.1" "37.3"
## [241] "39.9" "39.6" "38.9" "40.9" "37.5" "39.4" "39.5" "38.6" "38.2" "40.7"
## [251] "37.3" "39.0" "40.3" "36.6" "39.4" "38.9" "39.1" "39.7" "38.1" "39.4"
DF3<-matrix(0,nrow=nrow(DF2),ncol=ncol(DF2))
for (i in 1:ncol(DF2)){
DF3[,i]<-as.numeric(DF2[,i])
}
colnames(DF3)<-colnames(DF2)
DM<-DF3[,c(2:3,5:8)]
The second line is creating a new object, DF2, and using it to store a subset of the data from DF. From every row in DF for which the code is A, it is taking the first 10 columns of data and putting them in DF2.
Although this column looks like numbers, the computer is not treating it as numeric data. The output from the line fourth line shows that each number is in quotation marks, so they were probably read in as strings. You can’t do math on a word, which is why the third line causes problems.
par(mfrow=c(2,3))
for(i in 1:6){
qqnorm(DM[,i], main=paste("Normal Q-Q Plot:",colnames(DM)[i]))
qqline(DM[,i])
}
Skull length (SK_L) appears to be light-tailed. Skull width (SK_B) is close to normal but maybe slightly heavy tailed. Femur (FEM) is very close to normal besides 2-3 outliers. Tibia (TIB) is slightly heavy tailed. Humerus (HUM) looks slightly heavy tailed. Ulna (ULN) is very close to normal with 1-2 outliers.
I think the three closest to normal are Ulna, Femur, and Skull Width.
par(mfrow=c(1,1))
bvbox(cbind(DM[,2], DM[,3]), main="bvbox: Skull Width vs Femur")
bvbox(cbind(DM[,3], DM[,6]), main="bvbox: Femur vs Ulna")
bvbox(cbind(DM[,6], DM[,2]), main="bvbox: Ulna vs Skull Width")
head(DM)
## SK_L SK_B FEM TIB HUM ULN
## [1,] 37.8 29.2 74.7 112.0 73.7 66.5
## [2,] 40.5 31.2 81.5 124.0 79.2 72.3
## [3,] 40.3 31.0 80.3 116.5 78.6 73.8
## [4,] 38.9 30.5 74.8 111.0 72.2 66.6
## [5,] 37.6 29.2 73.2 100.5 73.6 67.3
## [6,] 39.7 30.8 78.2 118.0 76.3 71.2
In a bivariate box plots, we want to see most of the points concentrated in the inner ellipse, and a relatively even spread of points throughout the outer ellipse. A few points outside the outer ellipse are ok, as long as there is not a pattern to them. I think the first and third boxplots look pretty reasonable, as there are no egregious clusters or trailing outliers and the points are most concentrated in the inner ellipse. The ellipse in the second plot is quite narrow, and although the distribution of points within the ellipses is ok, I’m somewhat suspicious of this pairing.
Now to construct the chi-squared plot:
chi2<-function(x,nouts=0){
x<-as.data.frame(x)
row.names(x)<-as.character(row.names(x))
cm<-colMeans(x)
S<-cov(x)
d<-apply(x,1,function(x){t(x-cm)%*%solve(S)%*%(x-cm)})
qc=qchisq((1:nrow(x)-1/2)/nrow(x),df=ncol(x))
sod=sort(d)
plot(qc,sod,
xlab=expression(paste(chi^2,"Quantile")),
ylab="Ordered distances",xlim=range(qc)*c(1,1.1))
if (nouts>0){
oups<-which(rank(sod,ties="random")>nrow(x)-nouts)
text(qc[oups]-.1,sod[oups],names(oups),pos=2)}
abline(a=0,b=1)
}
chi2(cbind(DM[,2], DM[,3], DM[,6]))
This doesn’t look great. The points start missing the line at around 5 on the chi-squared quartile axis, and the points at the end are very far off. I don’t think that these three variables together are multivariate normal.
nsims<-10000
xmat<-matrix(rt(20*nsims,df=5)*sqrt(3/5), byrow=T, nrow=nsims)
ymat<-matrix(runif(20*nsims,-sqrt(3),sqrt(3)),byrow=T,nrow=nsims)
pvals<-rep(0,nsims)
for (i in 1:nsims){
pvals[i]<-t.test(xmat[i,],ymat[i,])$p.value}
hist(pvals, main="P Values for T-Test")
sum(pvals<=0.05)/nsims
## [1] 0.0506
The t-test is robust to these mis-matched distributions. As we dicussed in class, procedures for means are typically robust to non-normal distributions. Despite the pairing of a heavy-tailed and light-tailed distribution together, we still see a relatively uniform distribution of p-values across the 10,000 tests, and very close to 5% of those p-values are below an alpha of 0.05, as expected.
nsims<-10000
xmat<-matrix(rt(20*nsims,df=5)*sqrt(3/5), byrow=T, nrow=nsims)
ymat<-matrix(runif(20*nsims,-sqrt(3),sqrt(3)),byrow=T,nrow=nsims)
pvals<-rep(0,nsims)
for (i in 1:nsims){
pvals[i]<-var.test(xmat[i,],ymat[i,])$p.value}
hist(pvals, main="P Values for F-Test")
sum(pvals<=0.05)/nsims
## [1] 0.0782
However, the F-test does not work the way it’s supposed to for our mis-matched distributions. Smaller p-values are more frequent than larger ones, suggesting that the test will reject the null hypothesis more times than it should. Rather than 5%, over 7% of p-values are below an alpha of 0.05. This test of variances is not robust to non-normal distributions.