I am currently taking part in research utilizing Naked Mole Rats. I am utilizing data analysis techniques to answer the overall question, "Can we detect certain types of naked mole rat behavior utilizing statistical techniques such as hamming distance and similarity graphs?".
Certain statistical techniques, such as hamming distance have been utilized to detect sleeping patterns, however, that is a pretty blatant type of behavior in my opinion. Whereas, analyzing particular behavior after the birth of a new pup is much harder to detect.
My main programming language is Java and I am new to R. I have written two scripts that are particularly important to the research. The first one, which I will post here, reads in a state matrix with 59 columns and an undetermined amount of rows per file.
Each column signifies a rat, each row signifies one time window, and each data point represents the location the animal is found at
One row of the data looks like this written on one line (an example of a file can be found here:
2 6 3 1 5 2 4 5 7 1 1 1 1 1 2 1 2 1 2 3
1 2 2 2 1 1 2 1 1 3 2 2 2 1 2 2 1 1 2 3
7 2 2 2 2 2 1 2 1 2 2 1 1 2 2 4 1 3 4
The program below takes in this matrix, creates an adjacency matrix for the specific file (ie a matrix 59 x 59) that counts how many time windows rat 1 has been with rat 2, and how many time windows rat 1 has been with rat 3, etc. Additionally, the program then creates a thresholded matrix to see which animals spend more then, for example, 50% percent of their time together.
#This version of code works with multiple data files in a particular folder,
#performs some matrix calculations on them and outputs the new matrices with their corresponding titles
#into that same folder - currently there are 165 stateFiles within this particular folder which
#means 330 additional files were created in that same folder in about a minutes worth of time.
stateFiles <- list.files("/home/zackymo/Desktop/birth_data_zach_calcs_75/", pattern="*state.txt", full.names=TRUE)
thres <- .75
for(file in stateFiles){
rawData = read.table(file)
df <- data.frame(rawData)
incidence <- matrix(rep(0, ncol(df)*ncol(df)), nrow=ncol(df))
thresholded <- matrix(rep(0, ncol(df)*ncol(df)), nrow=ncol(df))
#Set the diagonal = to constants due to the fact that both are symmetric matrices
diag(incidence) <- nrow(df)
diag(thresholded) <- 1
#These loops turn the state matrix into a boolean matrix, and then counts up the true and falses, adds them to the incidence matrix, and then creates the thresholded matrix.
for (i in 1:(ncol(df)-1)) {
for (j in (i+1):ncol(df)) {
incidence[i,j] = incidence[j,i] = sum(df[,i] == df[,j])
if(incidence[i,j]/nrow(df) >= thres){
thresholded[i,j] = thresholded[j,i] = 1
}#should these conditions be reversed for effiency? How can I check that?
else{
thresholded[i,j] = thresholded[j,i] = 0
}
}
}
#Data preparation for output file - transposing matrices
tincidence <- t(incidence)
tthresholded <- t(thresholded)
#Manipulating the fileName to be able to create a new file w/ a meaningful file name
#I may be able to split by / then edit the paste below f[1] + f[2] + f[3] + f[4] + f[5]
f <- strsplit(file, split='.', fixed=TRUE)
f <- unlist(f)
#This will not work -> because you end up
#Setting the current directory for the output files to be placed, and never
#setwd("/home/zackymo/Desktop/birth_data1_out_files")
#Creating the various output files
write(tincidence, file = paste(f[1],"toInc", sep = ""),
ncolumns = ncol(df),
sep = " ")
write(tthresholded, file = paste(f[1],"toThresholded", sep = ""),
ncolumns = ncol(df),
sep = " ")
}
I was wondering if someone would be able to critique this code in terms of following R programming standards, along with the general logic and efficiency behind the program.