【发布时间】:2023-08-12 09:46:02
【问题描述】:
在我的数据集中,有一列包含 NPS 数据,其中数字从 0 变为 10。10 和 9 称为“促进者”,8 和 7 称为“被动者”,6-0 称为“批评者” .我计划通过将变量 NPS 视为二进制(启动者和非启动者)来拟合模型。因此,我想在执行 glm 之前将我的数据拆分为训练集和测试集。为了使训练集具有代表性,我尝试在数据集中包含 50% 的推广者和 50% 的非推广者。
我的第一个问题:这是一种有效的方法吗?
第二个问题:我尝试了下面的代码来拆分数据集,并且能够生成训练集。
table(mydata$NPS)
# 0 1 2 3 4 5 6 7 8 9 10
# 18 31 49 62 90 217 514 2332 10600 6557 2003
nrow(mydata) # = 22473
#total number of promoters = 8560
#total number of non-promoters = 13913
8560*0.8 #=6848 80% of promoters count
#all the promoters
data_promoters<-mydata[(mydata$NPS==10 | mydata$NPS==9),]
#all the non promoters
data_nonPromoters<-mydata[(mydata$NPS!=10 & mydata$NPS!=9),]
dim(data_promoters) #8560 32
dim(data_nonPromoters) #13913 32
set.seed(100)
sample_promoters <- data_promoters[sample(1:nrow(data_promoters),6848),]
sample_nonPromoters <- data_nonPromoters[sample(1:nrow(data_nonPromoters),6848),]
#train dataset
train_mydata <- rbind(sample_promoters, sample_nonPromoters)
head(train_mydata)
tail(train_mydata)
dim(train_mydata) #13696 32
#test dataset
test_mydata<-mydata[-train_mydata, ]
如上所述形成测试集时,出现以下错误。你能帮我解决这个问题吗?非常感谢!
Error in `[.default`(xj, i) : invalid subscript type 'list'
In addition: Warning messages:
1: In Ops.factor(left) : ‘-’ not meaningful for factors
2: In Ops.factor(left) : ‘-’ not meaningful for factors
3: In Ops.factor(left) : ‘-’ not meaningful for factors
4: In Ops.factor(left) : ‘-’ not meaningful for factors
5: In Ops.factor(left) : ‘-’ not meaningful for factors
【问题讨论】:
-
您的第一个问题听起来像是一个统计分析问题,最好在交叉验证(统计堆栈交换站点)上进行。
标签: r split dataset glm sample