Bootstrap command

advertisement
########################################################
#
#
#
These are the functions. They are written out
#
#
because I am not a good enough R programmer to
#
#
make a package! In general no changes should be
#
#
needed and the following can be loaded as is.
#
#
Simply cut and paste from the text file into R
#
#
#
########################################################
#### This is a function that counts dams and offspring
CntDandO <- function(DATA,VarNames)
{
offspcnt <<- c()
sire <- DATA[[VarNames[1]]][1]
dam <- DATA[[VarNames[2]]][1]
OffCount <- 1
for (i in 2:length(DATA[[VarNames[1]]]))
{
if ((DATA[[VarNames[1]]] [i] == sire) && (DATA[[VarNames[2]]] [i] == dam))
{ sire <- DATA[[VarNames[1]]] [i]
dam <- DATA[[VarNames[2]]] [i]
OffCount <- OffCount + 1;
}
else
{
offspcnt <- rbind(offspcnt, cbind(OffCount, dam, sire))
sire <- DATA[[VarNames[1]]] [i]
dam <- DATA[[VarNames[2]]] [i]
OffCount <- 1
}
}
offspcnt <- rbind(offspcnt, cbind(OffCount, dam, sire))
offspcnt <<- offspcnt
damcnt <- c()
sire <- offspcnt[1,3]
DamCount <- 1
for (i in 2:length(offspcnt[,3]))
{
if (offspcnt[i,3] == sire)
{ sire <- offspcnt[i,3]
DamCount <- DamCount + 1;
}
else
{
damcnt <- rbind(damcnt, cbind(DamCount, sire))
sire <- offspcnt[i,3]
DamCount <-1}
}
# DamCount <- DamCount + 1;
damcnt <<- rbind(damcnt, cbind(DamCount, sire))
}
#### This is the function that generates a boot strap data set.
GenBootstrap <- function(srceDATA, destDATA, FactNames)
{
########################################################
#### This program generates a hierarchically structured
#### bootstrap data frame. It takes three files as input.
#### The first is a fully balanced data frame that is used as
#### the source data. This should have no missing
#### values, and needs to be fully balanced. It is used
#### as the source of the data in the bootstrap data set
#### The second data frame is the target data set. The
#### bootstrap data frame will have exactly the same
#### structure as the target data frame, but will be organized
#### in a stratified random manner, with all of the missing
#### values of the original target data set intact.
#### The data sets must have a simple two level nested
#### structure. This will typically be sires and dams within
#### sires. The third data set will have the names of the
#### columns identifying sires and dams. These must be
#### in order.
####
#### An example call:
#### FileOfFactorNames <- c("sire", "dam")
#### GenBootstrap(SrceFile, DstFile, FileOfFactorNames)
####
####
The output data frame is called BootData
####
#### uses function: CntDandO(DATA,VarNames)
########################################################
boottemp <- c()
CntDandO (srceDATA, FactNames)
srceDam <- damcnt [1,1]
srcOff <- offspcnt[1,1]
StBalIndex <-array(1:length(srceDATA[,1]), dim=c(srcOff, srceDam,(length(srceDATA [,1])/( srcOff * srceDam))))
sire_size <- trunc(length(srceDATA [,1])/( srcOff * srceDam))-.000001
dam_size <- srceDam
off_size <- srcOff
CntDandO (destDATA, FactNames)
dest_sire_size <- length(damcnt[,1])
cntr <- 0
for (sireNum in 1:dest_sire_size)
{
SIRE <- runif(1) *( sire_size) +1
#
damorder <- sample(1:dam_size, damcnt[sireNum,1], replace=TRUE) #the replace = TRUE is disturbing!
#
for (damNum in 1: damcnt[sireNum,1])
{
cntr <- cntr + 1
DAM <- damorder[damNum] # runif(1)*( dam_size)+1
offorder <- sample(1:off_size, offspcnt[cntr,1])
for (offNum in 1: offspcnt[cntr,1])
{
OFF <- offorder[offNum]
#offNum #runif(1)*(off_size)+1
RandRow <- StBalIndex[as.integer(OFF), as.integer(DAM), as.integer(SIRE)]
boottemp <- rbind(boottemp, cbind(sireNum, damNum, srceDATA[RandRow,]))
}
}
}
for (Bti in 1:length(destDATA [,1]))
{
for (Btj in 1:length(destDATA [1,]))
{
if (is.na(destDATA [Bti, Btj])) {boottemp[Bti, Btj+2] <- NA}
}
}
BootData <<- data.frame(boottemp, row.names = 1:length(boottemp [,1]))
}
#### This does a two factor nested Manova and
#### extracts degrees of freedom and covariance matrices
SDManova <- function(DATA, FACTORS, TRAITS)
{
sire <- factor(DATA[[FACTORS[1]]]);
dam <- factor(DATA[[FACTORS[2]]]);
Y <- DATA[[TRAITS[1]]]
for (cntr in 2: length(TRAITS))
{
Y <- cbind (Y, DATA[[TRAITS[cntr]]]);
}
manovaout <- manova(Y ~ sire + dam:sire);
sireSS <- summary(manovaout)$SS$sire;
damSS <- summary(manovaout)$SS$'sire:dam';
ResidSS <- summary(manovaout)$SS$Residuals;
sireDF <<- summary(manovaout)$ stats[1,1];
damDF <<- summary(manovaout)$ stats[2,1];
ResidDF <<- summary(manovaout)$stats[3,1];
numsires <- sireDF + 1;
damspersire <- (numsires + damDF)/numsires;
offsperdam <- (ResidDF + numsires + damDF)/( numsires*damspersire);
CovSires <<- ((sireSS/sireDF) - (damSS/damDF))/(damspersire*offsperdam);
CovDams <<- ((damSS/damDF) - (ResidSS/ResidDF))/offsperdam;
CovResid <<- (ResidSS/ResidDF);
CovPhen <<- CovSires* VA_multiplier + (CovDams - CovSires)* VD_multiplier + CovResid;
VA <<- CovSires* VA_multiplier;
VD <<- (CovDams - CovSires)* VD_multiplier;
}
#### This does a one factor Manova and
#### extracts degrees of freedom and covariance matrices
SManova <- function(DATA, FACTORS, TRAITS)
{
sire <- factor(DATA[[FACTORS[1]]]);
Y <- DATA[[TRAITS[1]]]
for (cntr in 2: length(TRAITS))
{
Y <- cbind (Y, DATA[[TRAITS[cntr]]]);
}
manovaout <- manova(Y ~ sire);
sireSS <- summary(manovaout)$SS$sire;
ResidSS <- summary(manovaout)$SS$Residuals;
sireDF <<- summary(manovaout)$ stats[1,1];
ResidDF <<- summary(manovaout)$stats[2,1];
numsires <- sireDF + 1;
offspersire <- (ResidDF + numsires)/( numsires);
CovSires <<- ((sireSS/sireDF) - (ResidSS/ResidDF))/(offspersire);
CovResid <<- (ResidSS/ResidDF);
CovPhen <<- CovSires* VA_multiplier + CovResid;
VA <<- CovSires* VA_multiplier;
}
#### This function compares two matrices and finds a subset of the
#### matrices such that both have non-singular matrices
MatrixSize <- function(Df1, P1, Df2, P2)
{
# first test for negative variances
BadVar <- c()
BadVarFlag <- F
for (scntr in 1:length(P1[,1]))
{
if ((P1[scntr,scntr]<=0)|| (P2[scntr,scntr]<=0))
{
BadVar <- cbind(BadVar, scntr)
BadVarFlag <-T
}
}
if (BadVarFlag)
{
P1<- P1[-BadVar,- BadVar]
P2<- P2[-BadVar,- BadVar]
}
#second test for negative determinants
if (length(P1) > 1)
{
Det1 <- det(P1)
Det2 <- det(P2)
temprank <- length(P1[,1])
}
if (length(P1) == 1)
{
Det1 <- P1
Det2 <- P2
temprank <- 1
}
if (length(P1) < 1)
{
Det1 <- 999
Det2 <- 999
temprank <- 0
NoDataFlg <<- 0
print("one matrix has no variance!")
}
Z <- 0
while ((((Det1 <= 0) || (Det2 <= 0))&&(NoDataFlg))&&(Z<200))
{
scntrhldr <- 1
if (length(P1) == 4)
{
Det1 <- P1[-1, -1]
Det2 <- P2[-1, -1]
for (scntr in 2:length(P1[,1]+1))
{
sub1 <- P1[-scntr, -scntr]
sub2 <- P2[-scntr, -scntr]
if (length(sub1) <1) {print("sub1 > 1, 1")}
if (length(Det1) <1) {print("Det1 > 1, 1")}
if ((sub1 >= Det1) && (sub2 >= Det2))
{
scntrhldr <- scntr
Det1 <- P1[-scntr, -scntr]
Det2 <- P2[-scntr, -scntr]
}
}
P1 <- P1[-scntrhldr, -scntrhldr]
P2 <- P2[-scntrhldr, -scntrhldr]
temprank <- 1
}
if (length(P1) > 4)
{
Det1 <- det(P1[-1, -1])
Det2 <- det(P2[-1, -1])
for (scntr in 2:length(P1[,1]+1))
{
sub1 <- det(P1[-scntr, -scntr])
sub2 <- det(P2[-scntr, -scntr])
if (length(sub1) <1) {print("sub1 > 1, 2")}
if (length(Det1) <1) {print("Det1 > 1, 2")}
if ((sub1 >= Det1) && (sub2 >= Det2))
{
scntrhldr <- scntr
Det1 <- det(P1[-scntr, -scntr])
Det2 <- det(P2[-scntr, -scntr])
}
}
P1 <- P1[-scntrhldr, -scntrhldr]
P2 <- P2[-scntrhldr, -scntrhldr]
temprank <- length(P1[,1])
Z<-Z+1
if (Z>180)
{print(Z)}
}
}
# third, test that the Gcomb is positive.
#################################################
Gcomb <- ((Df1* P1) + (Df2* P2))/( Df1+ Df2)
if (length(Gcomb) > 1) {detG <- det(Gcomb)}
else {detG <- Gcomb}
if (length(detG)==0) {print(" gotcha A")}
#if (length(detG)==0) {detG <- 0} # commenting this out kills program on failure.
if (abs(detG) < 0.00001) {detG <- 0} # it is possible to get occasional weird results.
while (detG < 0)
{
#print("your innit 1")
scntrhldr <- 1
if (length(P1) == 4)
{
detG <- Gcomb[-1,-1]
for (scntr in 2:length(Gcomb[,1]+1))
{
subG1 <- Gcomb [-scntr, -scntr]
if ((subG1 >= detG)&&(length(subG1)==1)&&(length(detG)==1))
{
scntrhldr <- scntr
detG <- Gcomb[-scntr, -scntr]
}
}
P1 <- P1[-scntrhldr, -scntrhldr]
P2 <- P2[-scntrhldr, -scntrhldr]
temprank <- 1
}
if (length(P1) > 4)
{
detG <- det(Gcomb[-1, -1])
for (scntr in 2:length(P1[,1]+1))
{
subG1 <- det(Gcomb[-scntr, -scntr])
if (subG1 >= detG)
{
scntrhldr <- scntr
detG <- det(Gcomb[-scntr, -scntr])
}
}
P1 <- P1[-scntrhldr, -scntrhldr]
P2 <- P2[-scntrhldr, -scntrhldr]
temprank <- length(P1[,1])
}
}
##################################################
Matrix1 <<- P1
Matrix2 <<- P2
JointRank <<- temprank
}
#### The following function performs the bartletts test.
BartlettTest <- function(DF1, Ma1, DF2, Ma2, Rank)
{
if (Rank==1)
{
# *****************
Gcomb <- ((DF1* Ma1) + (DF2* Ma2))/( DF1+ DF2)
if (length(Ma1)>1) {print("Ma1 bad")}
if (length(Ma2)>1) {print("Ma2 bad")}
if (Ma1>Ma2)
{M <- (DF1+ DF2) * log(Gcomb) - (DF1) * log(Ma1) - (DF2) * log(Ma2)}
else
{M <- (-1)*((DF1+ DF2) * log(Gcomb) - (DF1) * log(Ma1) - (DF2) * log(Ma2))}
OneOverC <- 1 - 1/3 * (1/ DF1+ 1/ DF2 - 1/( DF1+ DF2))
Bartlett <<- M * OneOverC
# *****************
}
else
{
Gcomb <- ((DF1* Ma1) + (DF2* Ma2))/( DF1+ DF2)
if (det(Ma1) < 0) { Ma1 <- matrix(0, nrow=2, ncol=2)}
if (det(Ma2) < 0) { Ma1 <- matrix(0, nrow=2, ncol=2)}
if (det(Ma1) > det(Ma2))
{ if ((det(Gcomb)<=0) | (det(Ma1)<=0) | (det(Ma2)<=0))
{M=0}
else
{M <- (DF1+ DF2) * log(det(Gcomb)) - (DF1) * log(det(Ma1)) - (DF2) * log(det(Ma2))}
}
else
{ if ((det(Gcomb)<=0) | (det(Ma1)<=0) | (det(Ma2)<=0))
{M=0}
else
{M <- (-1)*((DF1+ DF2) * log(det(Gcomb)) - (DF1) * log(det(Ma1)) - (DF2) * log(det(Ma2)))}
}
OneOverC <- 1 - (2 * Rank^2 + 3* Rank - 1)/(6 * (Rank + 1)) * (1/ DF1+ 1/ DF2 - 1/( DF1+ DF2))
Bartlett <<- M * OneOverC
}
}
# Manteltest calculates the Mantel correlation for a pair of covariance matrices
Manteltest <- function(DF1, matrix1, DF2, matrix2, p)
{
ma1M <- c();
ma2M <- c()
if (p==1) {Mantel <<- 0}
else
{
Gcomb <<- ((DF1* matrix1) + (DF2* matrix2))/( DF1+ DF2)
for (i in 1:p)
{
for (j in i:p)
{
ma1M <- cbind(ma1M, (matrix1[i, j]/sqrt(Gcomb[i,i]* Gcomb[j,j])))
ma2M <- cbind(ma2M, (matrix2[i, j]/sqrt(Gcomb[i,i]* Gcomb[j,j])))
}
}
A <- ma1M
B <- ma2M
SumA2 <- sum(A^2)
SumB2 <- sum(B^2)
leadingfactor <- 1/(p*(p-1)/2 + p)
M <- (sum(A*B) - leadingfactor* sum(A)* sum(B))
Mantel <<- M/sqrt((sum(A^2) - leadingfactor* sum(A)^2)*(sum(B^2) - leadingfactor* sum(B)^2))
if (Mantel > 1)
{
ma1Mdebug <<- ma1M
ma2Mdebug <<- ma2M
leadingfactordebug <<- leadingfactor
Mdebug <<- M
print("Super Mantel to the rescue - or maybe not")
}
}
}
#### This function finds the rank of a matrix
#### such that the matrix is non-singular
RankOfMatrix <- function(P1)
{
# first test for negative variances
BadVar <- c()
BadVarFlag <- F
for (scntr in 1:length(P1[,1]))
{
if ((P1[scntr,scntr]<=0))
{
BadVar <- cbind(BadVar, scntr)
BadVarFlag <- T
}
}
if (BadVarFlag)
{
#
print(BadVar)
P1<- P1[-BadVar,-BadVar]
}
#second test for negative determinants
if (length(P1) > 1)
{Det1 <- det(P1)
temprank <- length(P1[,1])
}
else
{Det1 <- P1
temprank <- 1
}
if (length(P1) == 0)
{print (c("matrix P1 has no valid variances"))
Det1 <- 999
temprank <- 0
NoDataFlg <<- 0
}
Z <-1
while ((Det1 <= 0) && (Z < 200))
{
scntrhldr <- 1
if (length(P1) ==4)
{
Det1 <- P1[-1, -1]
for (scntr in 2:length(P1[,1]+1))
{
#
print(cbind(Det1, scntr))
sub1 <- P1[-scntr, -scntr]
if (length(sub1) <1) {print("sub1 > 1, 3")}
if (length(Det1) <1) {print("Det1 > 1, 3")}
if (sub1 >= Det1)
{
scntrhldr <- scntr
Det1 <- P1[-scntr, -scntr]
}
}
P1 <- P1[-scntrhldr, -scntrhldr]
temprank <- 1
Z<-Z+1
}
if (length(P1) > 4)
{
Det1 <- det(P1[-1, -1])
for (scntr in 2:length(P1[,1]+1))
{
#
print(cbind(Det1, scntr))
sub1 <- det(P1[-scntr, -scntr])
if (length(sub1) <1) {print("sub1 > 1, 4")}
if (length(Det1) <1) {print("Det1 > 1, 4")}
if ((sub1 >= Det1))
{
scntrhldr <- scntr
Det1 <- det(P1[-scntr, -scntr])
}
}
P1 <- P1[-scntrhldr, -scntrhldr]
temprank <- length(P1[,1])
Z<-Z+1
}
}
Matrix1 <<- P1
MatrixRank <<- temprank
}
#######################
# Random Skewers stuff
########################
Corr<-function(v1,v2){ correlation <- (t(v1)%*%v2)/sqrt((sum(v1^2))*(sum(v2^2))) ; correlation}
##############################################
# This generates a matrix with zeros for the variables
# that are not in the legitimate covariance matrix
#################################################
skewerRankOfMatrix <- function(P1)
{
# first test for negative variances
BadVar <- c(999)
BadVarFlag <- F
for (scntr in 1:length(P1[,1]))
{
if ((P1[scntr,scntr]<=0))
{
BadVar <- cbind(BadVar, scntr)
BadVarFlag <- T
}
}
#second test for negative determinants
if (length(P1[-BadVar,- BadVar]) > 1)
{
Det1 <- det(P1[-BadVar,- BadVar])
temprank <- length(P1[-BadVar,1])
}
else
{
Det1 <- P1[-BadVar,- BadVar]
temprank <- 1
}
if (length(P1[-BadVar,- BadVar]) == 0)
{print ("loser")
Det1 <- 999
temprank <- 0
}
Z <-1
while ((Det1 <= 0) && (Z < 20))
{
scntrhldr <- 1
if (length(P1[-c(BadVar,1), -c(BadVar,1)]) ==1)
{Det1 <- (P1[-c(BadVar,1), -c(BadVar,1)])}
else
{Det1 <- det(P1[-c(BadVar,1), -c(BadVar,1)])}
###########
for (scntr in 2:length(P1[,1]+1))
{
if (length(P1[-c(BadVar,scntr), - c(BadVar,scntr)])==0)
{sub1 <- (-99)}
else {
if (length(P1[-c(BadVar,scntr), - c(BadVar,scntr)])==1)
{sub1 <- P1[-c(BadVar,scntr), - c(BadVar,scntr)]}
else
{sub1 <- det(P1[-c(BadVar,scntr), - c(BadVar,scntr)])}
}
if (sub1 >= Det1)
{
scntrhldr <- scntr
if (length(P1[-c(BadVar,scntr), - c(BadVar,scntr)]) ==1)
{Det1 <- P1[-c(BadVar,scntr), - c(BadVar,scntr)]
}
else
{Det1 <- det(P1[-c(BadVar,scntr), - c(BadVar,scntr)])
}
}
temprank <- 1
} #does this really belong here? Yes it does
BadVar <- c(BadVar, scntrhldr)
Z<-Z+1
}
################
BadVarOut <<- BadVar[-1]
P1[BadVar[-1],] <-0
P1[,BadVar[-1]] <-0
SKMatrix <<- P1
MatrixRank <<- temprank
}
##############################################
# This does "skewnum" random skewers for two additive
# genetic covariance matrices, "VA1" and "VA2"
#################################################
Compareskew <- function(skewnum, DF1, VA1, DF2, VA2)
{
skewset <- c()
if (length(VA1) > length(VA2))
{skewsize <- length(VA1[,1])}
else
{if (length(VA2) == 1)
{ skewsize <- 1}
else
{skewsize <- length(VA2[,1])}}
stdskewset <- c()
skewtemp <- c()
for (iSK in 1:(skewsize))
{skewtemp <- cbind(skewtemp, runif(skewnum, min=-1, max=1))}
skew <<- skewtemp /sqrt(rowSums(skewtemp ^2))
for (iSk in 1:length(skew[,1]))
{
if (sqrt(length(VA1)) != length(skew[1,]))
{ print ("thar she blows")
VA1oops <<- VA1
VA2oops <<- VA2
skewoops <<- skew[iSK,]
}
M1skew <- VA1%*%skew[iSk,]
M2skew <- VA2%*%skew[iSk,]
skewsamp <- Corr(M1skew, M2skew)
skewset <- rbind(skewset, skewsamp)
}
RSkewers <<-mean(skewset)
# standardization method number 1
#VAcomb <- ((DF1* VA1) + (DF2* VA2))/( DF1+ DF2)
###### replace 0s with 1s in combined matrix
#if (length(VAcomb) > 1)
#{for (iSK in 1:length(VAcomb[,1]))
# { for (jSK in 1:length(VAcomb[1,]))
#
{if (VAcomb[iSK, jSK] == 0) { VAcomb[iSK, jSK] = 1}}
#}}
#VA1 <- VA1/VAcomb
#VA2 <- VA2/VAcomb
# standardization method number 2
VAcomb <- ((DF1* VA1) + (DF2* VA2))/( DF1+ DF2)
if (length(VAcomb)>1)
{p <- length(VAcomb [,1])
for (i in 1:p)
{
for (j in i:p)
{
if (VAcomb[i,i]==0){ VAcomb[i,i]<-1}
if (VAcomb[j,j]==0){ VAcomb[j,j]<-1}
VA1[i,j] <- VA1[i,j] /sqrt(VAcomb [i,i]* VAcomb [j,j])
VA2[i,j] <- VA2[i,j] /sqrt(VAcomb [i,i]* VAcomb [j,j])
}
}
}
else {p <- 1
if (length(VAcomb)==1)
{if (VAcomb==0){ VAcomb <-1}}
VA1 <- VA1 /sqrt(VAcomb * VAcomb)
VA2<- VA2/sqrt(VAcomb* VAcomb)
}
for (iSk in 1:length(skew[,1]))
{
M1skew <- VA1%*%skew[iSk,]
M2skew <- VA2%*%skew[iSk,]
#Standardization method 3, the one that Brittny advocates.
#for (jSk in 1:length(skew[1,]))
# {
#
if (VA1[jSk, jSk] > 0)
#
{M1skew <- M1skew[jSk]/sqrt(VA1[jSk, jSk])}
#
if (VA2[jSk, jSk] > 0)
#
{M2skew <- M2skew[jSk]/sqrt(VA2[jSk, jSk])}
# }
skewsamp <- Corr(M1skew, M2skew)
stdskewset <- rbind(stdskewset, skewsamp)
}
Std_RSkewers <<- mean(stdskewset)
}
###############################
# This does the selection skewers
###############################
#### This function compares two matrices and finds a subset of the
#### matrices such that both have non-singular matrices
SSKMatrixSize <- function(Df1, P1, Df2, P2)
{
# first test for negative variances
BadVar <- c(999)
BadVarFlag <- F
for (scntr in 1:length(P1[,1]))
{
if ((P1[scntr,scntr]<=0)|| (P2[scntr,scntr]<=0))
{
BadVar <- cbind(BadVar, scntr)
BadVarFlag <-T
}
}
#
if (BadVarFlag)
#
{
#
P1<- P1[-BadVar,- BadVar]
#
#
P2<- P2[-BadVar,- BadVar]
}
#second test for negative determinants
if (length(P1[-BadVar,- BadVar]) > 1)
{
Det1 <- det(P1[-BadVar,- BadVar])
Det2 <- det(P2[-BadVar,- BadVar])
temprank <- length(P1[,-BadVar])
}
if (length(P1[-BadVar,- BadVar]) == 1)
{
Det1 <- P1[-BadVar,- BadVar]
Det2 <- P2[-BadVar,- BadVar]
temprank <- 1
}
if (length(P1[-BadVar,- BadVar]) < 1)
{
Det1 <- 999
Det2 <- 999
temprank <- 0
NoDataFlg <<- 0
print("one matrix has no variance!")
}
Z <- 0
while ((((Det1 <= 0) || (Det2 <= 0))&&(NoDataFlg) &&(Z<200)))
{
Z <- Z+1
scntrhldr <- 1
if (length(P1[-BadVar, -BadVar]) == 4)
{
Det1 <- P1[-c(1, BadVar), -c(1,BadVar)]
Det2 <- P2[-c(1, BadVar), -c(1,BadVar)]
for (scntr in 2:length(P1[,1]+1))
{
sub1 <- P1[-c(scntr, BadVar), -c(scntr,BadVar)]
sub2 <- P2[-c(scntr, BadVar), -c(scntr,BadVar)]
if ((sub1 >= Det1) && (sub2 >= Det2))
{
scntrhldr <- scntr
Det1 <- P1[-c(scntr, BadVar), -c(scntr,BadVar)]
Det2 <- P2[-c(scntr, BadVar), -c(scntr,BadVar)]
}
}
BadVar <- c(BadVar, scntr)
temprank <- 1
}
if (length(P1[-BadVar, -BadVar]) > 4)
{
Det1 <- det(P1[-c(1, BadVar), -c(1,BadVar)])
Det2 <- det(P2[-c(1, BadVar), -c(1,BadVar)])
for (scntr in 2:length(P1[,1]+1))
{
sub1 <- det(P1[-c(scntr, BadVar), -c(scntr,BadVar)])
sub2 <- det(P2[-c(scntr, BadVar), -c(scntr,BadVar)])
if (length(sub1) <1) {print("sub1 > 1, 2")}
if (length(Det1) <1) {print("Det1 > 1, 2")}
if ((sub1 >= Det1) && (sub2 >= Det2))
{
scntrhldr <- scntr
Det1 <- det(P1[-c(scntr, BadVar), -c(scntr,BadVar)])
Det2 <- det(P2[-c(scntr, BadVar), -c(scntr,BadVar)])
}
}
BadVar <- c(BadVar, scntrhldr)
tempP1 <- P1[-BadVar, -BadVar]
temprank <- length(tempP1 [,1])
Z<-Z+1
}
}
# third, test that the Gcomb is positive.
#################################################
Gcomb <- ((Df1* P1) + (Df2* P2))/( Df1+ Df2)
if (length(Gcomb[-BadVar, -BadVar]) > 1) {detG <- det(Gcomb[-BadVar, -BadVar])}
else {detG <- Gcomb[-BadVar, -BadVar]}
if (length(detG)==0) {print(" gotcha B")}
if (length(detG)==0) {detG <- 0}
if (abs(detG) < 0.00001) {detG <- 0} # it is possible to get occasional weird results.
if (NoDataFlg==0)
{ detG <- 999}
Z <- 0
while ((detG < 0) && (NoDataFlg == 1) &&(Z<200))
{
Z<- Z+1
scntrhldr <- 1
if (length(P1[-BadVar, -BadVar]) == 4)
{
if (sum(BadVar ==1)==0)
{ detG <- Gcomb[-c(1, BadVar), -c(1, BadVar)]}
else
{ detG <- det(Gcomb[-c(BadVar), -c(BadVar)])}
for (scntr in 2:length(Gcomb[,1]+1))
{
if (sum(BadVar == scntr)==0)
{ subG1 <- Gcomb [-c(BadVar, scntr), -c(BadVar, scntr)]}
else
{ subG1 <- det(Gcomb [-c(BadVar), -c(BadVar)])}
if (subG1 >= detG)
{
scntrhldr <- scntr
if (sum(BadVar == scntr)==0)
{detG <- Gcomb [-c(BadVar, scntr), -c(BadVar, scntr)]}
}
}
BadVar <- c(BadVar, scntrhldr)
temprank <- 1
}
if (length(P1[-BadVar, -BadVar]) > 4)
{
detG <- det(Gcomb[-c(BadVar, scntr), -c(BadVar, scntr)])
for (scntr in 2:length(P1[,1]+1))
{
subG1 <- det(Gcomb[-c(scntr, BadVar), -c(scntr, BadVar) ])
if (subG1 >= detG)
{
scntrhldr <- scntr
detG <- det(Gcomb[-c(BadVar, scntr), -c(BadVar, scntr)])
}
}
BadVar <- c(BadVar, scntrhldr)
tempP1 <- P1[-BadVar, -BadVar]
temprank <- length(tempP1 [,1])
}
}
##################################################
tempskMatrix1 <- P1
tempskMatrix1 [BadVar[-1],] <-0
tempskMatrix1 [,BadVar[-1]] <-0
skMatrix1 <<- tempskMatrix1
tempskMatrix2 <- P2
tempskMatrix2 [BadVar[-1],] <-0
tempskMatrix2 [,BadVar[-1]] <-0
skMatrix2 <<- tempskMatrix2
skJointRank <<- temprank
}
##################################################
SelSkewers <- function(skewers, Sktraits, VAMatrix1, VA1DF, VPMatrix1, VAMatrix2, VA2DF, VPMatrix2, Skpop1, Skpop2,
SelIntensity)
{
# the relevant data sets are Dest1 and Dest2 (I think)
# trait vector is Dependents
skewtemp <- c()
Svector1<- array(c(0),dim=c(length(skewers[1,]), length(skewers[,1])))
Svector2 <- array(c(0),dim=c(length(skewers[1,]), length(skewers[,1])))
GPinv1 <<- VAMatrix1%*% solve(VPMatrix1)
GPinv2 <<- VAMatrix2%*% solve(VPMatrix2)
Gcomb <- (VAMatrix1 * VA1DF + VAMatrix2 * VA2DF)/( VA1DF + VA2DF)
for (iskew in 1:length(skewers[1,]))
{ index1 <- 0
index2 <- 0
for (jskew in 1:length(Sktraits))
{
index1 <- index1 + Skpop1[,Sktraits[jskew]]*skewers[jskew, iskew]
index2 <- index2 + Skpop2[,Sktraits[jskew]]*skewers[jskew, iskew]
}
IndexName <- paste("index", iskew, sep="")
Skpop1[,IndexName] <- index1
Skpop2[,IndexName] <- index2
}
for (iskew in 1:length(skewers[1,]))
{
IndexName <- paste("index", iskew, sep="")
pop1order <- order (Skpop1[[IndexName]], na.last=NA, decreasing=T)
pop2order <- order (Skpop2[[IndexName]], na.last=NA, decreasing=T)
for (jskew in 1:length(skewers[,1]))
{
temptrait <- Skpop1[[Sktraits[jskew]]][pop1order]
Svector1[iskew, jskew] <- mean(temptrait[1:(length(temptrait) * SelIntensity)]) - mean(temptrait)
temptrait <- Skpop2[[Sktraits[jskew]]][pop2order]
Svector2[iskew, jskew] <- mean(temptrait[1:(length(temptrait) * SelIntensity)]) - mean(temptrait)
}
}
for (iskew in 1:length(skewers[1,]))
{
Rvector1 <- GPinv1%*% Svector1[iskew,]
Rvector2 <- GPinv2%*% Svector2[iskew,]
# skewtemp <- cbind(skewtemp, Corr(Rvector1, Rvector2))
for (jskew in 1:length(skewers[,1]))
{
if (Gcomb[jskew, jskew] == 0)
{ Rvector1[jskew] <- 0
Rvector2[jskew] <- 0
}
else
{ Rvector1[jskew] <- Rvector1[jskew] /sqrt(Gcomb[jskew,jskew])
Rvector2[jskew] <- Rvector2[jskew] /sqrt(Gcomb[jskew,jskew])
}
}
skewtemp <- cbind(skewtemp, Corr(Rvector1, Rvector2))
}
Selection_Skewers <<- skewtemp
}
###############################
#### This is the master program that calls the other routines.
BootCompare <- function(Source, Dest1, Dest2, Sourcefacts, Dest1facts, Dest2facts, Dependents)
{
BootSetUp(Dependents)
TempBootResults <- c()
TempSelectionSkewers <- c()
debugcntr <- 1;
for (bcnter in 1: NumberOfBootStraps)
{
print(bcnter)
GenBootstrap (Source, Dest1, Sourcefacts)
if (length(Dest1facts) == 2)
{ BootFacts <- c("sireNum", "damNum")
SDManova(BootData, BootFacts, Dependents)
}
else
{ BootFacts <- c("sireNum")
SManova(BootData, BootFacts, Dependents)}
SSKBoot1 <- BootData # this saves the bootstrap data for the selection skewers
Pop1Sires <<- VA
Pop1SireDF <<- sireDF
Pop1VP <<- Pop1Sires + CovResid
GenBootstrap (Source, Dest2, Sourcefacts)
if (length(Dest2facts) == 2)
{ BootFacts <- c("sireNum", "damNum")
SDManova(BootData, BootFacts, Dependents)
}
else
{ BootFacts <- c("sireNum")
SManova(BootData, BootFacts, Dependents)}
SSKBoot2 <- BootData # this saves the bootstrap data for the selection skewers
Pop2Sires <<- VA
Pop2SireDF <<- sireDF
Pop2VP <<- Pop2Sires + CovResid
RankOfMatrix(Pop1Sires)
Pop1Rank <- MatrixRank
RankOfMatrix(Pop2Sires)
Pop2Rank <- MatrixRank
MatrixSize (Pop1SireDF, Pop1Sires, Pop2SireDF, Pop2Sires)
barttemp <- c()
if (NoDataFlg)
{
BartlettTest(Pop1SireDF, Matrix1, Pop2SireDF, Matrix2, JointRank)
Manteltest(Pop1SireDF, Matrix1, Pop2SireDF, Matrix2, JointRank)
skewerRankOfMatrix(Pop1Sires)
SkMatrix1 <- SKMatrix
skewerRankOfMatrix(Pop2Sires)
SkMatrix2 <- SKMatrix
Compareskew(SkewerNumber, Pop1SireDF, SkMatrix1, Pop2SireDF, SkMatrix2)
RawRSkewers <- RSkewers
StdRSkewers <- Std_RSkewers
# This is the skewers call. These are standardized so that the skewers have a length of one.
SelSkewers (Standard_Skewers, Dependents, SkMatrix1, Pop1SireDF, Pop1VP, SkMatrix2, Pop2SireDF, Pop2VP, SSKBoot1,
SSKBoot2, SelSkewSelectIntensity)
stdSelSkewers <- Selection_Skewers
# this does random skewers using only the variables present in both populations
# it uses the matrices generated in MatrixSize
Compareskew(SkewerNumber, Pop1SireDF, Matrix1, Pop2SireDF, Matrix2)
subsetRSkewers <- RSkewers
subsetStdRSkewers <- Std_RSkewers
SSKMatrixSize(Pop1SireDF, Pop1Sires, Pop2SireDF, Pop2Sires)
}
else
{ print("into wierdness")
Bartlett <- -999
Mantel <- -999
RawRSkewers <- -999
StdRSkewers <- -999
subsetRSkewers <- -999
subsetStdRSkewers <- -999
}
TempBootResults <- rbind(TempBootResults, cbind(Pop1Rank, Pop2Rank, JointRank, Bartlett, Mantel, RawRSkewers,
StdRSkewers, subsetRSkewers, subsetStdRSkewers)) #, rawSelSkewers, stdSelSkewers, subsetSelSkewers, subsetStdSelSkewers))
TempSelectionSkewers <- rbind(TempSelectionSkewers, stdSelSkewers)
}
BootResults <<- TempBootResults
SelectionSkewers <<- TempSelectionSkewers
}
######################################################
#
# This program does the matrix comparison analyses using the
# original data. This produces the actual results that will be compared
# using the bootstrap data sets.
#
#
#
#######################################################
AnalyzeData <- function(Data1, Data2, Data1facts, Data2facts, Dependents)
{
if (length(Data1facts) == 2)
{SDManova(Data1, Data1facts, Dependents) }
else
{SManova(Data1, Data1facts, Dependents)}
Pop1Sires <- VA
Pop1SireDF <- sireDF
Pop1VP <- Pop1Sires+ CovResid
Pop1VACovMatrix <<- Pop1Sires
Pop1PhenCovMatrix <<- Pop1Sires+ CovResid
if (length(Data2facts) == 2)
{SDManova(Data2, Data2facts, Dependents) }
else
{SManova(Data2, Data2facts, Dependents)}
Pop2Sires <- VA
Pop2SireDF <- sireDF
Pop2VP <- Pop2Sires + CovResid
Pop2VACovMatrix <<- Pop2Sires
Pop2PhenCovMatrix <<- Pop2Sires + CovResid
RankOfMatrix(Pop1Sires)
Pop1Rank <- MatrixRank
RankOfMatrix(Pop2Sires)
Pop2Rank <- MatrixRank
RankOfMatrix(Pop1Sires)
Pop1Rank <- MatrixRank
RankOfMatrix(Pop2Sires)
Pop2Rank <- MatrixRank
MatrixSize (Pop1SireDF, Pop1Sires, Pop2SireDF, Pop2Sires)
barttemp <- c()
if (NoDataFlg)
{
BartlettTest(Pop1SireDF, Matrix1, Pop2SireDF, Matrix2, JointRank)
Manteltest(Pop1SireDF, Matrix1, Pop2SireDF, Matrix2, JointRank)
skewerRankOfMatrix(Pop1Sires)
SkMatrix1 <- SKMatrix
skewerRankOfMatrix(Pop2Sires)
SkMatrix2 <- SKMatrix
Compareskew(SkewerNumber, Pop1SireDF, SkMatrix1, Pop2SireDF, SkMatrix2)
RawRSkewers <- RSkewers
StdRSkewers <- Std_RSkewers
# This is the skewers call. These are standardized so that the skewers have a length of one.
SelSkewers (Standard_Skewers, Dependents, SkMatrix1, Pop1SireDF, Pop1VP, SkMatrix2, Pop2SireDF, Pop2VP, Data1, Data2,
SelSkewSelectIntensity)
stdSelSkewers <- Selection_Skewers
# this does random skewers using only the variables present in both populations
# it uses the matrices generated in MatrixSize
Compareskew(SkewerNumber, Pop1SireDF, Matrix1, Pop2SireDF, Matrix2)
subsetRSkewers <- RSkewers
subsetStdRSkewers <- Std_RSkewers
SSKMatrixSize(Pop1SireDF, Pop1Sires, Pop2SireDF, Pop2Sires)
}
else
{ print("into wierdness")
Bartlett <- -999
Mantel <- -999
RawRSkewers <- -999
StdRSkewers <- -999
subsetRSkewers <- -999
subsetStdRSkewers <- -999
}
AnalysisResults <<- cbind(Pop1Rank, Pop2Rank, JointRank, Bartlett, Mantel, RawRSkewers, StdRSkewers, subsetRSkewers,
subsetStdRSkewers)
AnalysisSelectionSkewers <<- stdSelSkewers
}
########################################################
#
#
#
Below are two programs to analyze the standard stats
#
#
and to analyze the selection skewers
#
#
#
#
#
########################################################
Statistical_Analysis <- function()
{
BootResults2 <- as.data.frame(BootResults)
AnalysisResults2 <- as.data.frame(AnalysisResults)
BLength <- length(BootResults2[,1])
BLengthNoO <- length(BootResults2$ Pop1Rank[BootResults2$JointRank>1])
# Analyze D, the difference in rank
StatName<- "D"
D <- AnalysisResults2$Pop1Rank- AnalysisResults2$Pop2Rank
Statistic <- D
DBoot <- BootResults2 $Pop1Rank- BootResults2 $Pop2Rank
LessThanOrEqual<- length(DBoot[DBoot<=D]) / BLength
GreaterThanOrEqual<- length(DBoot[DBoot>=D]) / BLength
MoreExtremeOrEqual<- length(DBoot[abs(DBoot)>=abs(D)]) / BLength
Stat <- D
Statistic <- Stat
##
D <- AnalysisResults2$Pop1Rank[BootResults2$JointRank>1]- AnalysisResults2$Pop2Rank[BootResults2$JointRank>1]
DBoot <- BootResults2 $Pop1Rank[BootResults2$JointRank>1]- BootResults2 $Pop2Rank[BootResults2$JointRank>1]
LessThanOrEqualNoO <- length(DBoot[DBoot<=D]) / BLengthNoO
GreaterThanOrEqualNoO <- length(DBoot[DBoot>=D]) / BLengthNoO
MoreExtremeorEqualNoO <- length(DBoot[abs(DBoot)>=abs(D)]) / BLengthNoO
##
# End Analyze D
#Start Analyze Bartlett
StatName<- rbind(StatName, "Bartlett")
Stat <- AnalysisResults2$Bartlett
Statistic <- rbind(Statistic, Stat)
TempBoot <- BootResults2$Bartlett
LessThanOrEqual <- rbind(LessThanOrEqual, length(TempBoot [TempBoot <= Stat]) / BLength)
GreaterThanOrEqual<- rbind(GreaterThanOrEqual, length(TempBoot [TempBoot >= Stat]) / BLength)
MoreExtremeOrEqual <- rbind(MoreExtremeOrEqual, length(TempBoot [abs(TempBoot)>=abs(Stat)]) / BLength)
TempBoot <- BootResults2$Bartlett[BootResults2$JointRank>1]
LessThanOrEqualNoO <- rbind(LessThanOrEqualNoO, length(TempBoot [TempBoot <= Stat]) / BLengthNoO)
GreaterThanOrEqualNoO <- rbind(GreaterThanOrEqualNoO, length(TempBoot [TempBoot >= Stat]) / BLengthNoO)
MoreExtremeorEqualNoO <- rbind(MoreExtremeorEqualNoO, length(TempBoot [abs(TempBoot)>=abs(Stat)]) / BLengthNoO)
#End Analyze Bartlett
#Start Analyze Mantel
StatName<- rbind(StatName, "Mantel")
Stat <- AnalysisResults2$Mantel
Statistic <- rbind(Statistic, Stat)
TempBoot <- BootResults2$Mantel
LessThanOrEqual <- rbind(LessThanOrEqual, length(TempBoot [TempBoot <= Stat]) / BLength)
GreaterThanOrEqual<- rbind(GreaterThanOrEqual, length(TempBoot [TempBoot >= Stat]) / BLength)
MoreExtremeOrEqual <- rbind(MoreExtremeOrEqual, length(TempBoot [abs(TempBoot)>=abs(Stat)]) / BLength)
TempBoot <- BootResults2$Mantel[BootResults2$JointRank>1]
LessThanOrEqualNoO <- rbind(LessThanOrEqualNoO, length(TempBoot [TempBoot <= Stat]) / BLengthNoO)
GreaterThanOrEqualNoO <- rbind(GreaterThanOrEqualNoO, length(TempBoot [TempBoot >= Stat]) / BLengthNoO)
MoreExtremeorEqualNoO <- rbind(MoreExtremeorEqualNoO, length(TempBoot [abs(TempBoot)>=abs(Stat)]) / BLengthNoO)
#End Analyze Mantel
#Start Analyze RawRSkewers
StatName<- rbind(StatName, "RawRSkewers")
Stat <- AnalysisResults2$RawRSkewers
Statistic <- rbind(Statistic, Stat)
TempBoot <- BootResults2$RawRSkewers
LessThanOrEqual <- rbind(LessThanOrEqual, length(TempBoot [TempBoot <= Stat]) / BLength)
GreaterThanOrEqual<- rbind(GreaterThanOrEqual, length(TempBoot [TempBoot >= Stat]) / BLength)
MoreExtremeOrEqual <- rbind(MoreExtremeOrEqual, length(TempBoot [abs(TempBoot)>=abs(Stat)]) / BLength)
TempBoot <- BootResults2$RawRSkewers [BootResults2$JointRank>1]
LessThanOrEqualNoO <- rbind(LessThanOrEqualNoO, length(TempBoot [TempBoot <= Stat]) / BLengthNoO)
GreaterThanOrEqualNoO <- rbind(GreaterThanOrEqualNoO, length(TempBoot [TempBoot >= Stat]) / BLengthNoO)
MoreExtremeorEqualNoO <- rbind(MoreExtremeorEqualNoO, length(TempBoot [abs(TempBoot)>=abs(Stat)]) / BLengthNoO)
#End Analyze RawRSkewers
#Start Analyze StdRSkewers
StatName<- rbind(StatName, "StdRSkewers")
Stat <- AnalysisResults2$StdRSkewers
Statistic <- rbind(Statistic, Stat)
TempBoot <- BootResults2$StdRSkewers
LessThanOrEqual <- rbind(LessThanOrEqual, length(TempBoot [TempBoot <= Stat]) / BLength)
GreaterThanOrEqual<- rbind(GreaterThanOrEqual, length(TempBoot [TempBoot >= Stat]) / BLength)
MoreExtremeOrEqual <- rbind(MoreExtremeOrEqual, length(TempBoot [abs(TempBoot)>=abs(Stat)]) / BLength)
TempBoot <- BootResults2$StdRSkewers [BootResults2$JointRank>1]
LessThanOrEqualNoO <- rbind(LessThanOrEqualNoO, length(TempBoot [TempBoot <= Stat]) / BLengthNoO)
GreaterThanOrEqualNoO <- rbind(GreaterThanOrEqualNoO, length(TempBoot [TempBoot >= Stat]) / BLengthNoO)
MoreExtremeorEqualNoO <- rbind(MoreExtremeorEqualNoO, length(TempBoot [abs(TempBoot)>=abs(Stat)]) / BLengthNoO)
#End Analyze StdRSkewers
#Start Analyze subsetRSkewers
StatName<- rbind(StatName, "subsetRSkewers")
Stat <- AnalysisResults2$subsetRSkewers
Statistic <- rbind(Statistic, Stat)
TempBoot <- BootResults2$subsetRSkewers
LessThanOrEqual <- rbind(LessThanOrEqual, length(TempBoot [TempBoot <= Stat]) / BLength)
GreaterThanOrEqual<- rbind(GreaterThanOrEqual, length(TempBoot [TempBoot >= Stat]) / BLength)
MoreExtremeOrEqual <- rbind(MoreExtremeOrEqual, length(TempBoot [abs(TempBoot)>=abs(Stat)]) / BLength)
TempBoot <- BootResults2$subsetRSkewers [BootResults2$JointRank>1]
LessThanOrEqualNoO <- rbind(LessThanOrEqualNoO, length(TempBoot [TempBoot <= Stat]) / BLengthNoO)
GreaterThanOrEqualNoO <- rbind(GreaterThanOrEqualNoO, length(TempBoot [TempBoot >= Stat]) / BLengthNoO)
MoreExtremeorEqualNoO <- rbind(MoreExtremeorEqualNoO, length(TempBoot [abs(TempBoot)>=abs(Stat)]) / BLengthNoO)
#End Analyze subsetRSkewers
#Start Analyze subsetStdRSkewers
StatName<- rbind(StatName, "subsetStdRSkewers")
Stat <- AnalysisResults2$subsetStdRSkewers
Statistic <- rbind(Statistic, Stat)
TempBoot <- BootResults2$subsetStdRSkewers
LessThanOrEqual <- rbind(LessThanOrEqual, length(TempBoot [TempBoot <= Stat]) / BLength)
GreaterThanOrEqual<- rbind(GreaterThanOrEqual, length(TempBoot [TempBoot >= Stat]) / BLength)
MoreExtremeOrEqual <- rbind(MoreExtremeOrEqual, length(TempBoot [abs(TempBoot)>=abs(Stat)]) / BLength)
TempBoot <- BootResults2$subsetStdRSkewers [BootResults2$JointRank>1]
LessThanOrEqualNoO <- rbind(LessThanOrEqualNoO, length(TempBoot [TempBoot <= Stat]) / BLengthNoO)
GreaterThanOrEqualNoO <- rbind(GreaterThanOrEqualNoO, length(TempBoot [TempBoot >= Stat]) / BLengthNoO)
MoreExtremeorEqualNoO <- rbind(MoreExtremeorEqualNoO, length(TempBoot [abs(TempBoot)>=abs(Stat)]) / BLengthNoO)
#End Analyze subsetStdRSkewers
StatOut <<- data.frame(StatName, Statistic, LessThanOrEqual, GreaterThanOrEqual, MoreExtremeOrEqual, LessThanOrEqualNoO,
GreaterThanOrEqualNoO, MoreExtremeorEqualNoO, row.names=NULL)
print("General Statistics")
print("below are the general statistics and the general statistics using the subset with joint matrices of at least size 2")
print("Subset stats have NoO attached to them")
print("Number of bootstraps equals")
print(BLength)
print("Number of bootstraps with joint matrix size of at least 2 is")
print(BLengthNoO)
print("the dataframe name for this is StatOut")
print(" ")
print("this is the raw analysis vector (AnalysisResults)")
print(AnalysisResults)
print(" ")
#format(StatOut, justify=c("left"), scientific=FALSE)
print(StatOut)
}
################################################
#
# This ends the analysis of the standard measures. What
# follows is the analysis of the selection skewers.
################################################
SelSkewers_Analysis <- function()
{
Statistic <- c()
LessThanOrEqual<- c()
GreaterThanOrEqual<- c()
MoreExtremeOrEqual<- c()
LessThanOrEqualNoO <- c()
GreaterThanOrEqualNoO <- c()
MoreExtremeorEqualNoO <- c()
BLength <- length(SelectionSkewers[,1])
skewtemp <- cbind(SelectionSkewers, BootResults[,3])
skewtemp2 <- skewtemp[skewtemp [,length(skewtemp [1,])]>1,]
BLengthNoO <- length(skewtemp2 [,1])
# Analyze Selection Skewers loop
for (j in (1:length(AnalysisSelectionSkewers)))
{
SelSkew <- AnalysisSelectionSkewers [j]
BootSelSkew <- SelectionSkewers[,j]
Statistic <- rbind(Statistic, SelSkew)
LessThanOrEqual<- rbind(LessThanOrEqual, length(BootSelSkew [BootSelSkew <= SelSkew]) / BLength)
GreaterThanOrEqual<- rbind(GreaterThanOrEqual, length(BootSelSkew [BootSelSkew >= SelSkew]) / BLength)
MoreExtremeOrEqual<- rbind(MoreExtremeOrEqual, length(BootSelSkew [abs(BootSelSkew)>=abs(SelSkew)]) / BLength)
BootSelSkew2 <- skewtemp2[,j]
LessThanOrEqualNoO <- rbind(LessThanOrEqualNoO, length(BootSelSkew2 [BootSelSkew2 <= SelSkew]) / BLengthNoO)
GreaterThanOrEqualNoO <- rbind(GreaterThanOrEqualNoO , length(BootSelSkew2 [BootSelSkew2 >= SelSkew]) /
BLengthNoO)
MoreExtremeorEqualNoO <- rbind(MoreExtremeorEqualNoO, length(BootSelSkew2 [abs(BootSelSkew2)>=abs(SelSkew)]) /
BLengthNoO)
}
SelSkewOut <<- data.frame(Statistic, LessThanOrEqual, GreaterThanOrEqual, MoreExtremeOrEqual, LessThanOrEqualNoO,
GreaterThanOrEqualNoO, MoreExtremeorEqualNoO, row.names=NULL)
print("below are the selection skewers and the selection skewers using the subset with joint matrices of at least size 2")
print("Subset skewer stats have NoO attached to them")
print("Number of bootstraps equals")
print(BLength)
print("Number of bootstraps with joint matrix size of at least 2 is")
print(BLengthNoO)
print("the dataframe name for this output is SelSkewOut")
print(" ")
print(SelSkewOut)
}
########################################################
#
#
#
Finally, one program to rule them all
#
#
This is easier to destroy then Frodo’s ring :)
#
#
#
########################################################
RunMatrixAnalysis <- function(SrceData, Data1, Data2, SrceFactorNames, Data1FactorNames, Data2FactorNames, TraitNames)
{
BootCompare(SrceData, Data1, Data2, SrceFactorNames, Data1FactorNames, Data2FactorNames, TraitNames)
AnalyzeData(Data1, Data2, Data1FactorNames, Data2FactorNames, TraitNames)
Statistical_Analysis ()
SelSkewers_Analysis ()
}
########################################################
#
#
#
This completes the functions that need to be loaded
#
#
#
#
#
#
#
########################################################
########################################################
#
#
#
BootSetUp is a convenient place
#
#
to put parameters that change
#
#
#
#
#
########################################################
# This is where general startup issues are addressed.
BootSetUp <- function(TraitNum)
{
NoDataFlg <<- 1
dim(Skewers) <<- c(length(TraitNum), Number_of_Skewers)
## normalize the selection skewers to length 1
Standard_Skewers <<- t(t(Skewers) /colSums(Skewers))
}
########################################################
#
#
#
Load the correct data sets and function names.
#
#
This will change from run to run depending on needs
#
#
#
########################################################
#
#### load the right data sets
stockbal <-read.table("/Research/R Trib Bootstrap/balanced stock females.txt")
stockbal <- data.frame(stockbal, row.names = 1:length(stockbal[,1]))
stock <-read.table("/Research/R Trib Bootstrap/stock data female.txt")
stock <- data.frame(stock, row.names = 1:length(stock[,1]))
Pop3 <-read.table("/Research/R Trib Bootstrap/population 3 females.txt")
Pop3 <- data.frame(Pop3, row.names = 1:length(Pop3 [,1]))
#### various lists that will be used by the functions.
FactorNames <- c("Sire", "Dam")
StockFactorNames <- c("Sire", "Dam")
Pop3FactorNames <- c("Sire")
Traits <- c("Pupal_Mass", "Dev_Time", "Dry_Mass", "Rel_Fit")
### in the following the list must be of length (number of traits X Number of skewers)
### any other result will give an error.
Number_of_Skewers <- 5 #This is the number of selection skewers to be examined
Skewers <- c( 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
0, 0, 1, 1) # These are the selection skewers
#Parameters to adjust
# This is the strength of selection on the selection skewers. This should be the proportion selected
# It is a number between 0 and 1, with the smaller the number the stronger the selection.
# adjust the following for different mating systems. For sires in a half sib design VA = 4*var(half sibs)
# for full sibs adjust VA = 2 * var(full sibs) Adjust as appropriate for your system
VA_multiplier <- 4
#adjust this as appropriate. Typically it will be 4 since var(fullsibs)-var(halfsibs) = 1/4 VD
VD_multiplier <- 4
SelSkewSelectIntensity <- 0.5
#These parameters change the number of bootstraps, and number of skewers in the random skewers
NumberOfBootStraps <- 10 # the number of boot strap samples generated
SkewerNumber <- 1000 # the number of random skewers generated
########################################################
#
#
#
This completes the files that need to be changed.
#
#
to fit the particular circumstances of the data
#
#
#
########################################################
########################################################
#
#
#
Below are the commands to actually run the program
#
#
use them as needed
#
#
#
########################################################
#This command will simply generate the bootstrap statistics
#the command is commented out for convenience
#BootCompare(stockbal, stock, Pop3, FactorNames, StockFactorNames, Pop3FactorNames, Traits)
#This command will simply generate the statistics from the original data
#If this is run without first running the boot compare BootSetUp must be run first
#I have commented these out so that a total cut and paste can be done without running a lot
#of extraneous programs
# BootSetUp(Traits)
#AnalyzeData(stock, Pop3, StockFactorNames, Pop3FactorNames, Traits)
#The following programs will run the analyses comparing the original data
#to the bootstrap results allowing one and two tailed tests. These programs assume that
#BootCompare and AnalyzeData have been run.
#these commands are commented out for convenience
#Statistical_Analysis ()
#SelSkewers_Analysis ()
########################################################
#
# This is the command to do the whole shooting match
# It is the one that typically should be run.
#
########################################################
RunMatrixAnalysis(stockbal, stock, Pop3, FactorNames, StockFactorNames, Pop3FactorNames, Traits)
########################################################
#
#
#
This ends the commands to run the programs .
#
#
#
#
#
########################################################
# The data can be found in the following lists and data frames
# this has the bootstrap statistics for the traditional measures
#BootResults
#This has the bootstrap statistics for the selection skewers
#(Sorry about the clutzy name!)
#SelectionSkewers
#this has the traditional measures on the original data
#AnalysisResults
#This has the selection skewers for the original data
#AnalysisSelectionSkewers
#This has the statistical comparisons for the traditional measures
#StatOut
# use
# format(StatOut, justify=c("left"), scientific=FALSE)
# for a prettier print
# why this format command doesn’t work in the program I have no clue. R can be weird.
#This has the statistical comparisons for the selection skewers
#SelSkewOut
# this is a useful command that clears the work space!
#rm(list=ls())
Download