for循环将变量名称添加到公式中

时间:2013-08-21 14:38:15

标签: r for-loop random-forest

我正在尝试在R中执行随机森林回归并且遇到了几个问题并且已经修复了大部分问题但是我无法解决这个问题。 我有一个我希望阅读的文件列表,这是没问题的(我使用for循环)。

library(randomForest)
set.seed(51)

file<- c("file1","file2","file3")
targets<- c("X1.ts","ts2","ts3")

for (i in 1:length(file)){
d_names<-paste("C:\\location\folder\",drugs[i],".txt",sep="")
dataset<- read.table(d_names, header=TRUE, row.names=1)
ind<-sample(2,nrow(dataset), replace=TRUE)

#TRAINING DATASET1 PREDICTING DATASET2
train_one.rf<- randomForest(dataset[ind==1,][[1]] ~ .-targets[i], data=dataset[ind==1,], prob=c(0.7,0.3))
dset2.pred<- predict(train_one.rf, newdata=dataset[ind==2,])

#TRAINING DATASET2 PREDICTING DATASET1
train_two.rf<- randomForest(dataset[ind==2,][[1]] ~ .-targets[i], data=dataset[ind==2,], prob=c(0.7,0.3))
dset1.pred<- predict(train_two.rf, newdata=dataset[ind==1,])

}

随机森林的本质是我必须对除我想要预测的列之外的数据建模。这样做我必须使用:

dataset[ind==1,][[1]] ~ .-target[i]

target [i] 我希望为每次随机森林运行添加列名(来自目标)。我已经尝试将它分配给一个变量,同时也将循环变量分配给它但是无济于事。我想R中的公式部分需要比我更优雅的知识。

提前举手,

Jcrow

1 个答案:

答案 0 :(得分:1)

这是使用分为两个数据集的mtcars数据作为data1和data2的解决方案。 (这里没有R for loop

data1<-mtcars[1:15,]
data2<-mtcars[16:nrow(mtcars),]
mydata<-list(data1,data2)

targets<-list("mpg~.","cyl~.")

Map(function(x) Map(function(y) randomForest(as.formula(y),data=x,importance=TRUE,proximity=TRUE), targets),mydata)

[[1]]
[[1]][[1]]

Call:
 randomForest(formula = as.formula(y), data = x, importance = TRUE,      proximity = TRUE) 
               Type of random forest: regression
                     Number of trees: 500
No. of variables tried at each split: 3

          Mean of squared residuals: 4.637522
                    % Var explained: 63.98

[[1]][[2]]

Call:
 randomForest(formula = as.formula(y), data = x, importance = TRUE,      proximity = TRUE) 
               Type of random forest: regression
                     Number of trees: 500
No. of variables tried at each split: 3

          Mean of squared residuals: 0.2455641
                    % Var explained: 89.04


[[2]]
[[2]][[1]]

Call:
 randomForest(formula = as.formula(y), data = x, importance = TRUE,      proximity = TRUE) 
               Type of random forest: regression
                     Number of trees: 500
No. of variables tried at each split: 3

          Mean of squared residuals: 10.90303
                    % Var explained: 78.93

[[2]][[2]]

Call:
 randomForest(formula = as.formula(y), data = x, importance = TRUE,      proximity = TRUE) 
               Type of random forest: regression
                     Number of trees: 500
No. of variables tried at each split: 3

          Mean of squared residuals: 0.1623937
                    % Var explained: 95.69


Warning messages:
1: In randomForest.default(m, y, ...) :
  The response has five or fewer unique values.  Are you sure you want to do regression?
2: In randomForest.default(m, y, ...) :
  The response has five or fewer unique values.  Are you sure you want to do regression?

注意:内部Map函数针对目标的不同元素重复回归,而外部Map函数重复mydata的不同元素的回归。