I have the following dataset. I would like to know which bacteria contribute more when comparing the location of the bacteria(categorical) and its pH(numeric).

For instance at the end I would like to say for example that a certain bacterial type is more frequently found in a certain location when looking at the temperature.

             Bacillus Lactobacillus Janibacter Brevibacterium Lawsonella     Location temperature
Sample1              2          30    164          8             21         48 bedroom    27
Sample2              0         211      0        996            195        108 bedroom   35
Sample3              1         938      1         21             38         43 pool   45
Sample4              0          95     17          1              4        334 pool   10
Sample5              0         192     91         25           1207       1659 soil    14
Sample6              0          12     33          6             12        119 soil   21
Sample7              0          16      3          0              0        805 soil    12

The idea is to run randomforest to select those features (bacteria) that are more important when looking at both the location and the temperature.

Is a random forest suitable for this? When I run the following command I get the following error:

randomForest(Location+Temperature ~.,data=mydf)
Error in Location + Temperature : non-numeric argument to binary operator. 

From the error it looks that i cannot use a continuous and categorical variable together. How can I fix this?

Is for exemple convert the numeric temperature variable to ranges of temperatures as a categorical variables would be a solution ?

In fact I have tried and it worked by converting the numeric temperature to ranges and pasting the location so that i have a combination of location and temperature.

randomForest(Location_temperature ~.,data=dat)

I get the list of important bacteria which is what I was looking for. Now how can I know which one contributes more to one location or another since my model i was using all sites? For example how to check that your important variables(let´s say Bacillus is the most important from the randomforest model ) is important in the pool location (how much variation it explains in the pool)?

  • $\begingroup$ a minimum data would be helpful $\endgroup$
    – stas g
    Commented Jun 7, 2018 at 23:00

1 Answer 1


I think the basic problem you're facing is that you are asking your random forest to predict Location + Temperature, i.e. a compound of two variables. This is not a standard random forest; the problem is a multivariate random forest. I don't think the randomForest package can perform those. There is, however, a MultivariateRandomForest package that can do exactly what you're after.

If you want to learn more about multivariate random forests, Segal & Xiao (2011) seems like the place to start.

Segal, M., & Xiao, Y. (2011). Multivariate random forests. Wiley Interdisciplinary Reviews: Data Mining and Knowledge Discovery, 1(1), 80-87.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.