I study song learning in birds. I have one matrix for each male bird and his father, comparing all the songs of the 2 individuals, the male has the rows and the father has the columns. Each song has up to 10 renditions and the matrices contain the similarity score for the comparisons between each rendition of each song of the male and his father. It looks a bit like this:
Father > SongA1 SongA2 ... SongC1 SongC2
Male v
SongA1 0.256 0.453 ... 0.358 0.298
SongA2 0.359 0.485 ... 0.502 0.675
... ... ... ... ... ...
SongB1 0.703 0.286 ... 0.438 0.865
SongB2 0.457 0.365 ... 0.187 0.265
I am trying to find which song of the male is learned from which song of his father. To do so, I try to extract submatrices for each pair of song (Male A and Father A, Male A and Father B, ...) and calculate the mean similarity score in the submatrices. I will eventually keep the highest value (most similar songs) as learning measure. To be able to run this code for all the songs of all the individuals, I have a few levels of loops.
The loop works, but the work is incomplete. I get the results for comparisons between the songs of the male and his father, but the loop doesn't do all the comparisons. Let's say that I have a male with song A, B and i, and his father has A, B, C. The loop compares Male A with Father A, Male B with Father B, and Male i with Father C. But I want the comparison of Male A with all the songs of the father, the same for Male B and so on.
Here is my code so far. This isn't the full code, I have simplified the way I extract the submatrices. I'm just trying to find a way to do all the comparisons I need.
First, for each male, I import the file (Corr
) and prepare lists of the songs of the male (MaleTypes
) and his father (FatherTypes
). Then, I run this loop:
for (typeMale in 1:length(MaleTypes)) {
for (typeFather in 1:length(FatherTypes)) {
TypeSCCMatrix <- Corr[grepl(typeMale, rownames(Corr)), grepl(typeFather, colnames(Corr))]
TypeSCCMatrix <- as.matrix(sapply(TypeSCCMatrix, as.numeric))
runningAVG <- 0
counter <- 0
for (i in 1:dim(TypeSCCMatrix)[1]) {
for (j in 1:dim(TypeSCCMatrix)[2]) {
if (j>i) {
counter <- counter+1
runningAVG <- runningAVG + TypeSCCMatrix[i,j]
}
}
}
myLearning[typeMale] <- runningAVG/counter
myTypesMale[typeMale] <- MaleTypes[typeMale]
myTypesFather[typeFather] <- FatherTypes[typeFather]
}
}
With this loop, I get:
> myLearning
[1] 0.1784444 0.3553333 0.1364222
> myTypesMale
[1] "A" "B" "i"
> myTypesFather
[1] "A" "B" "C"
What I need would be something like that I guess:
> myLearning
[1] 0.1784444 0.3553333 0.1364222 0.2365487 0.1845976 0.5362478 ...
> myTypesMale
[1] "A" "A" "A" "B" "B" "B" "i" "i" "i"
> myTypesFather
[1] "A" "B" "C" "A" "B" "C" "A" "B" "C"
My question might be a bit specific, but I have been struggling with this loop for days now and I could really use your help!