R中如何解决“订阅分配中不允许NAs”的问题

时间:2014-12-11 05:52:47

标签: r machine-learning random-forest

我正在使用R做随机森林预测 我的Aggregate_sample.csv数据集。

Company Index,Is Customer,videos,videos
watched,tutorials,workshops,casestudies,productpages,other,totalActivities,youngTitleCount,
oldTitleCount,Median between two Activities,Max between 2 activities,
Time since100thactivity
STPT,0,0,3,0,0,0,0,19,22,0,22,120,64074480,0
STPR,0,0,1,0,1,1,0,61,64,0,64,120,56004420,0
PLNRTKNLJS,0,0,0,0,0,0,0,25,25,25,0,810,4349940,0
ASSSNNSP,0,0,0,0,0,3,0,17,20,0,20,60,2220,0
STPP,1,164,32,25,36,26,0,2525,2808,498,2310,60,2938260,76789992
AJKMPTNKSL,0,0,0,0,0,0,0,1,1,0,1,0,0,0
FNKL,0,0,0,1,0,0,0,21,22,0,22,300,2415900,0
FNKK,0,0,1,0,0,0,0,1,2,2,0,60,60,0
FNKN,1,2,0,1,0,0,0,22,25,0,25,480,150840,0

以下是我的R脚本

 # Install and load required packages for decision trees and forests
library(rpart)
install.packages('randomForest')
library(randomForest)

Aggregate <- read.csv("~/Documents/Machine Lerning/preprocessed data/Aggregate_sample.csv")

# splitdf function
splitdf <- function(dataframe, seed=NULL) {
if (!is.null(seed)) set.seed(seed)
index <- 1:nrow(dataframe)
trainindex <- sample(index, trunc(length(index)*0.7))
trainset <- dataframe[trainindex, ]
testset <- dataframe[-trainindex, ]
list(trainset=trainset,testset=testset)
}

splits <- splitdf(Aggregate, seed=808)

#it returns a list - two data frames called trainset and testset
str(splits)

lapply(splits,nrow)

#view the first few columns in each data frame
lapply(splits,head)

training <- splits$trainset
testing  <- splits$testset

#fit the randomforest model
model <- randomForest(as.factor(Aggregate$Is.Customer) ~ Aggregate$seniorTitleCount +
Aggregate$juniorTitleCount + Aggregate$totalActivities + Aggregate$Max.between.2.activities
+ Aggregate$Time.since.100th.activity + Aggregate$downloads , data=training,
importance=TRUE, ntree=2000)

#print(mode)
# what are the important variables
varImpPlot(model)

但是我一直在跟踪错误并且无法继续。我的IsCustomer列似乎有问题,但它只是一个“0”和“1”的列(我的数据集中没有任何NA) )。

Error in [<-.factor("*tmp*", keep, value = c("0", "0", "0", "0", "1", : NAs are not allowed in subscripted assignments In addition: Warning message: 'newdata' had 3 rows but variables found have 9 rows

我读了下面的问题,似乎与我的问题有关但无法从中找到答案。 Assigning within a lapply "NAs are not allowed in subscripted assignments"

提前致谢。

1 个答案:

答案 0 :(得分:1)

您希望仅从training data.frame中提取数据,因此您不应在公式中引用Aggregate。在测试数据中实际使用变量名称,这似乎工作正常。

randomForest(as.factor(Is.Customer) ~ oldTitleCount +
   youngTitleCount + totalActivities + Max.between.2.activities +
    Time.since100thactivity + videos , 
data=training,
importance=TRUE, ntree=2000)

返回

Call:
 randomForest(formula = as.factor(Is.Customer) ~ oldTitleCount +      youngTitleCount + totalActivities + Max.between.2.activities +      Time.since100thactivity + videos, data = training, importance = TRUE,      ntree = 2000) 
               Type of random forest: classification
                     Number of trees: 2000
No. of variables tried at each split: 2

        OOB estimate of  error rate: 16.67%
Confusion matrix:
  0 1 class.error
0 4 0         0.0
1 1 1         0.5