# Outputting indicative variables within a specific factor

I have a dataset on bugs that has been processed to incorporate a series of traits (e.g. size; feeding habitats) through displaying proportional values of different properties (e.g. small, medium, large; herbivore, carnivore). Each trait adds to 1. As a broad example, if a bug is very likely to be 'small', rarely 'medium' in size and never 'big', it could possess values of 0.9, 0.1, 0, respectively. Bugs were sampled from different habitats. Here is an example dataset.

gradient <- 1:99
Small <- gradient * 0.005
Medium <- gradient * 0.004
Large <- 1 - (Small + Medium)
Herbivore <- gradient * 0.005
Carnivore <- 1 - (Herbivore)
Habitat <- rep(c("Grass","Sand","Gravel"), each = 3)
df <- data.frame(Habitat = Habitat,
Small = Small,
Medium = Medium,
Large = Large,
Herbivore = Herbivore,
Carnivore = Carnivore)


Is there an effective way to characterise the dominant properties within each habitat? Potentially a list of the top three properties related to each habitat, e.g. (in order of likelihood)

large, herbivore, small. Within grass.
small, large, carnivore. Within sand.
large, carnivore, medium. Within gravel.


I originally looked into 'multipatt' within the indicspecies package, I get NAs for a lot of traits because they a most likely to be related to ALL habitats. I didn't get nice outputs for specific habitats. SIMPER analysis seems difficult to interpret for several factors (i.e. if there are numerous habitats). I read this blog, that outlined a function based on the 'bioenv' function in the vegan package, but I'm not sure of its validity.

## 1 Answer

I'm not sure if I've got the wrong end of the stick here, but how I read it is that you want the top three variables for each row (or aggregated by habitat, I couldn't decipher).

To get the top three variables for each row

for (i in 1:99)
{
print(as.character(df[i,1]))
print(names(rev(sort (df [i,])) [c(2:4) ] ))
}

#OR

for (i in 1:99)
{
print(as.character(df[i,1]))
print(rev(sort (df [i,])) [c(2:4) ] )
}


The top will give you the column names in order of the data values in each row (top 3) and the bottom will give you the numbers too so you can double check. Apologies that I haven't had the time to recode this in to a new data frame.

If you want this aggregated by habitat I would use either tapply or aggregate

summary<-aggregate(df[,c(2:length(names(df)))], by=list(df\$Habitat), FUN=max)
for (i in 1:3)
{
print(as.character(summary[i,1]))
print(names (sort (summary [i,]) [c(1:3) ] ))
}


With the example data given this aggregation obviously does not appear to provide any variation, this is simply a function of the flat distribution

• Jordan, thank you for the comment. The example output was supposed to represent specific properties statistically most likely to be within a certain habitat. So as a crude example, if I try and ask 'what properties are most likely to be found in sand' how would I test this. Essentially, how can I statistically display what characteristics of various sets of proportional data (the traits and properties here) are most likely to be found within a specific factor (habitat in this instance). I hope this is clearer now, but thank you for the response. – James White Sep 17 '15 at 21:33