Description:
This script investigate interaction of individuals in an animal community. We focus one 20 focal animals and 7 types of interaction, there are also 199 other animals in dataset. Pair-wise canberra distance and multidenmensional scaling method is used for this script.

Data Url:
https://jbhender.github.io/Stats506/Stats506_F17_ps2_interactions.csv
https://jbhender.github.io/Stats506/Stats506_F17_ps2_all_names.csv
https://jbhender.github.io/Stats506/Stats506_F17_ps2_focal_names.csv

library(tidyverse)
## -- Attaching packages ------------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 2.2.1     v purrr   0.2.4
## v tibble  1.3.4     v dplyr   0.7.4
## v tidyr   0.7.2     v stringr 1.2.0
## v readr   1.1.1     v forcats 0.2.0
## -- Conflicts ---------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(tidyr)
library(dplyr)
library(stringr)
# read csv file and convert to data frame
interactions = read_delim('./Stats506_F17_ps2_interactions.csv', delim=',',col_names=TRUE)
## Parsed with column specification:
## cols(
##   focal = col_character(),
##   behavior_cat = col_character(),
##   toward = col_character()
## )
focalnames = read_delim('./Stats506_F17_ps2_focal_names.csv', delim=',',col_names=FALSE)
## Parsed with column specification:
## cols(
##   X1 = col_character()
## )
allnames = read_delim('./Stats506_F17_ps2_all_names.csv', delim=',',col_names=FALSE)
## Parsed with column specification:
## cols(
##   X1 = col_character()
## )

(a)

maxcomma = max(str_count(interactions$toward, patter = ",")) # find number of columns to split 3rd column
columnnames = paste('toward', 1:(maxcomma+1), sep = "") # generate column names
interact = interactions %>%
  separate(toward, into = columnnames, sep = "\\,")  %>% # split 3rd column
  gather(toward, person, toward1:toward13,-focal) %>% # convert format from wide to long
  filter(person != ""& person !="?" & person != "  " & person != " ") %>% # filter out non-sense entries
  rename(behavior = behavior_cat) # rename column name to behavior(interaction)
## Warning: Too few values at 9339 locations: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
## 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...
x = as.data.frame(adist(interact$person, allnames$X1)) # compute Levenshtein distance
index = apply(x, 1, function(x){which.min(x)}) # find index of best-match names in allnames
correct.names = allnames$X1[index] # correct misspelled name
interact = interact %>%
  add_column(correct.names) %>%
  select(focal, behavior, toward, correct.names) %>%
  rename(person = correct.names)
interact3 = interact %>%
  group_by(focal, behavior, person) %>%
  summarise(counts = n()) %>%
  ungroup() %>%
  complete(focal, behavior,person, fill = list(counts = 0))

head(interact3)
## # A tibble: 6 x 4
##   focal   behavior  person counts
##   <chr>      <chr>   <chr>  <dbl>
## 1  Amen aggression   Abiel      0
## 2  Amen aggression    Acob      0
## 3  Amen aggression Adriana      1
## 4  Amen aggression  Adrick      1
## 5  Amen aggression   Ahlam      0
## 6  Amen aggression   Akbar      0

interact3 is the required formatted data, the 3rd column (“person”) refers to individuals specified in question. For example the 3rd row means Amen has aggression towards Adriana for 1 time.

(b)

In the code below I generate matrix of pair-wise canberra distances (measuring dissimilarity) between each focal animals for each interaction type, the diagonal entries of matrices are zero

interaction = unique(interact3$behavior) # get all possible interaction type
focals = unique(interact3$focal) # get all focal animals

# code to generate pair-wise canberra distances for aggression between focals
aggression = matrix(0, 20, 20) 
for (i in 1:20) {
  for (j in 1:20) {
    focal1 = focals[i]
    focal2 = focals[j]
    counts1 = interact3$counts[interact3$focal == focal1 & interact3$behavior == "aggression"]
    counts2 = interact3$counts[interact3$focal == focal2 & interact3$behavior == "aggression"]
    aggression[i,j] = dist(rbind(counts1, counts2), method = "canberra") 
  }
}

# code to generate pair-wise canberra distances for aapproch between focals
approach = matrix(0, 20, 20)
for (i in 1:20) {
  for (j in 1:20) {
    focal1 = focals[i]
    focal2 = focals[j]
    counts1 = interact3$counts[interact3$focal == focal1 & interact3$behavior == "approach"]
    counts2 = interact3$counts[interact3$focal == focal2 & interact3$behavior == "approach"]
    approach[i,j] = dist(rbind(counts1, counts2), method = "canberra") 
  }
}

# code to generate pair-wise canberra distances for carry between focals
carry = matrix(0, 20, 20)
for (i in 1:20) {
  for (j in 1:20) {
    focal1 = focals[i]
    focal2 = focals[j]
    counts1 = interact3$counts[interact3$focal == focal1 & interact3$behavior == "carry"]
    counts2 = interact3$counts[interact3$focal == focal2 & interact3$behavior == "carry"]
    carry[i,j] = dist(rbind(counts1, counts2), method = "canberra") 
  }
}

# code to generate pair-wise canberra distances for groom between focals
groom = matrix(0, 20, 20)
for (i in 1:20) {
  for (j in 1:20) {
    focal1 = focals[i]
    focal2 = focals[j]
    counts1 = interact3$counts[interact3$focal == focal1 & interact3$behavior == "groom"]
    counts2 = interact3$counts[interact3$focal == focal2 & interact3$behavior == "groom"]
    groom[i,j] = dist(rbind(counts1, counts2), method = "canberra") 
  }
}

# code to generate pair-wise canberra distances for mate between focals
mate = matrix(0, 20, 20)
for (i in 1:20) {
  for (j in 1:20) {
    focal1 = focals[i]
    focal2 = focals[j]
    counts1 = interact3$counts[interact3$focal == focal1 & interact3$behavior == "mate"]
    counts2 = interact3$counts[interact3$focal == focal2 & interact3$behavior == "mate"]
    mate[i,j] = dist(rbind(counts1, counts2), method = "canberra") 
  }
}

# code to generate pair-wise canberra distances for play between focals
play = matrix(0, 20, 20)
for (i in 1:20) {
  for (j in 1:20) {
    focal1 = focals[i]
    focal2 = focals[j]
    counts1 = interact3$counts[interact3$focal == focal1 & interact3$behavior == "play"]
    counts2 = interact3$counts[interact3$focal == focal2 & interact3$behavior == "play"]
    play[i,j] = dist(rbind(counts1, counts2), method = "canberra") 
  }
}

# code to generate pair-wise canberra distances for share between focals
share = matrix(0, 20, 20)
for (i in 1:20) {
  for (j in 1:20) {
    focal1 = focals[i]
    focal2 = focals[j]
    counts1 = interact3$counts[interact3$focal == focal1 & interact3$behavior == "share"]
    counts2 = interact3$counts[interact3$focal == focal2 & interact3$behavior == "share"]
    share[i,j] = dist(rbind(counts1, counts2), method = "canberra") 
  }
}

(c)

# codes to use multidimensional scaling to pairwise distances for each interaction type
aggressionMDS = cmdscale(aggression)
approachMDS = cmdscale(approach)
carryMDS = cmdscale(carry)
groomMDS = cmdscale(groom)
mateMDS = cmdscale(mate)
playMDS = cmdscale(play)
shareMDS = cmdscale(share)
# codes to generate plot showing relations among animals for each interaction type
plot(aggressionMDS, pch = '', bg='grey', xlab = "", ylab = "", main = "Aggression")
text(aggressionMDS, attr(aggression, 'Labels'))

plot(approachMDS, pch = '', bg='grey', xlab = "", ylab = "", main = "Approach")
text(approachMDS, attr(approach, 'Labels'))

plot(carryMDS, pch = '', bg='grey', xlab = "", ylab = "", main = "Carry")
text(carryMDS, attr(carry, 'Labels'))

plot(groomMDS, pch = '', bg='grey', xlab = "", ylab = "", main = "groom")
text(groomMDS, attr(groom, 'Labels'))

plot(mateMDS, pch = '', bg='grey', xlab = "", ylab = "", main = "mate")
text(mateMDS, attr(mate, 'Labels'))

plot(playMDS, pch = '', bg='grey', xlab = "", ylab = "", main = "play")
text(playMDS, attr(play, 'Labels'))

plot(shareMDS, pch = '', bg='grey', xlab = "", ylab = "", main = "share")
text(shareMDS, attr(share, 'Labels'))

The number in the plot shows each individual focal animals, for example 1 in all 7 plots refers to focal animal named amen. We can see for aggression, carry, groom, play and share there are two distinct clusters.