gpt4 book ai didi

r - 如何解决R中的 "NAs are not allowed in subscripted assignments"问题

转载 作者:行者123 更新时间:2023-11-30 09:12:49 25 4
gpt4 key购买 nike

我正在使用 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"

提前致谢。

最佳答案

看起来您只想从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

关于r - 如何解决R中的 "NAs are not allowed in subscripted assignments"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27416250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com