社区矩阵上的多变量随机森林

问题描述 投票:0回答:1

我想使用随机森林建模来理解社区装配的变量重要性 - 我的响应数据是一个社区矩阵。

library(randomForestSRC)

# simulated species matrix
species 
# site       species 1    species2     species 3
# 1             1            1            0
# 2             1            0            1
# 3             1            1            1
# 4             1            0            1
# 5             1            0            0
# 6             1            1            0
# 7             1            1            0
# 8             1            0            0
# 9             1            0            0
# 10            1            1            0


# environmental data
data
# site   elevation_m     PRECIPITATION_mm  
# 1        500                28
# 2        140                37
# 3        445                15
# 4        340                45
# 5        448                20
# 6        55                 70
# 7        320                18
# 8        200                42
# 9        420                22
# 10       180                8


# adding my species matrix into the environmental data frame
data[["species"]] <-(species)

# running the model
rf_model <- rfsrc(Multivar(species) ~.,data = data, importance = T)

但我收到一条错误消息:

Error in parseFormula(formula, data, ytry) : 
  the y-outcome must be either real or a factor.

我猜这个问题是存在/不存在的数据,但我不确定如何超越它。这是功能的限制吗?

r matrix random-forest
1个回答
1
投票

我认为这可能与您构建“数据”数据框架的方式有关。使用data[["species"]] <- (species)时,数据框内有数据框。如果你在我刚才提到的步骤之后你str(data),输出是这样的:

> str(data)
'data.frame':   10 obs. of  4 variables:
$ site     : int  1 2 3 4 5 6 7 8 9 10
$ elevation: num  500 140 445 340 448 55 320 200 420 180
$ precip   : num  28 37 15 45 20 70 18 42 22 8
$ species  :'data.frame':      10 obs. of  4 variables: #2nd data frame
..$ site     : int  1 2 3 4 5 6 7 8 9 10
..$ species.1: num  1 1 1 1 1 1 1 1 1 1
..$ species2 : num  1 0 1 0 0 1 1 0 0 1
..$ species.3: num  0 1 1 1 0 0 0 0 0 0

如果您将数据框架构建为data2 <- as.data.frame(cbind(data,species)),那么

rfsrc(Multivar(species.1,species2,species.3)~.,data = data2, importance=T)

似乎工作,因为我没有收到错误消息,而是我获得了一些合理的输出:

Sample size: 10
Number of trees: 1000
Forest terminal node size: 5
Average no. of terminal nodes: 2
No. of variables tried at each split: 2
Total no. of variables: 4
Total no. of responses: 3
User has requested response: species.1
Resampling used to grow trees: swr
Resample size used to grow trees: 10
Analysis: mRF-R
Family: regr+
Splitting rule: mv.mse *random*
Number of random split points: 10
% variance explained: NaN
Error rate: 0   

我不认为你建立数据框架的方法是习惯的方式,但我可能是错的。我认为rfsrc()不知道如何读取嵌套数据帧。我怀疑大多数建模功能没有额外的自定义代码。

© www.soinside.com 2019 - 2024. All rights reserved.