# Read test dataset using the function read.table() URL <- #the location of the file on your computer, e.g. C://Rfiles//2pop.txt data <- read.table("URL", header=T) data[1:2,] #Create genotype # X: dataframe with fragment lengths x of differenct loci (columns)observed at N individuals (rows) X <- data[,3:8]# if your data is differently organised, change the column assignment here. X # check, if the columns you selected really give the loci data G <- X[,1] if(ncol(X)>1) for(i in 2:(ncol(X))) { G <- paste(G,X[,i],sep=".") } G <- factor(G,levels=unique(G)) data$G <- G #attach G to data as column "G" data[1,] # look at data file attach(data) #attach data file # This resampling function is needed to create a file containing no recurrent # multilocus genotypes resample <- function(d=data) { Trees <- split(1:nrow(d), G) Trees[[1]] # first data point a <- rep(NA,length(Trees)) for(i in 1:length(Trees)) { a[i] <- Trees[[i]][1] if(length(Trees[[i]]) > 1) { a[i] <- sample(Trees[[i]], 1, replace=FALSE) } } d[a,] } # In the following step, the object "data.res" without recurrent # multilocus-genotypes is construced data.res <- resample(data) dim(data.res) # 23 13 # Make chi2 test and write chi2 and p-value into matrix # X.matrix: p.value in the lower triangle, chi2 value in upper triangle # Chi2: Sokal & Rohlf (2001)Biometry. Freeman, New York. # T.matrix: p.value in the lower triangle, T in the upper triangle # T is the Tschuproff contingency coefficient # T: Legendre & Legendre(1998)Numerical ecology, p.221 # V.matrix: p.value in the lower triangle, Cramer's V2 in the upper triangle # V: Agresti, A. (1984)Analysis of ordinal categorical data. Wiley, New York, p. 23-24 # without recurrent multilocus genotypes Loci.res <- data.res[,3:8] # enter column numbers of your loci in "data.res" L <- Loci.res #define this new if needed, e.g. if you enter an area or population # X.matrix <- matrix(NA, ncol(L), ncol(L), dimnames=list(names(L), names(L))) T.matrix <- matrix(NA, ncol(L), ncol(L), dimnames=list(names(L), names(L))) V.matrix <- matrix(NA, ncol(L), ncol(L), dimnames=list(names(L), names(L))) for(i in 1:(ncol(L)-1)) { for(j in (i+1):ncol(L)) { chi <- c() tab <- table(L[,i], L[,j]) chi <- chisq.test(x=tab, simulate.p.value=TRUE, B=2000) n <- sum(tab) d.f <- (ncol(tab)-1)*(nrow(tab)-1) X.2 <- chi$statistic T <- sqrt(X.2/(n*sqrt(d.f))) V.2 <- c() V.2 <- X.2/(n*(min(dim(tab)-1))) X.matrix[j,i] <- round(chi$p.value, 3) X.matrix[i,j] <- round(chi$statistic, 3) T.matrix[j,i] <- round(chi$p.value, 3) T.matrix[i,j] <- round(T ,3) V.matrix[j,i] <- round(chi$p.value, 3) V.matrix[i,j] <- round(V.2 ,3) } } X.matrix T.matrix V.matrix # check the results. LPu03 and LPu20 are completely linked in the # "2pop.txt" data example (p=0.000, V2=1.000). The other loci do not # exhibit significant linkage disequilibrium. # Save the object "V.matrix" as a text file for later importing into Excel write.table(V.matrix, "C://Program Files//V.matrix.txt", sep=",")